Bitrix-D7  20.0.0
entitycollection.php
См. документацию.
1 <?php
2 namespace Bitrix\Sale\Internals;
3 
4 use Bitrix\Sale;
5 use Bitrix\Main;
6 
7 /**
8  * Class EntityCollection
9  * @package Bitrix\Sale\Internals
10  */
11 abstract class EntityCollection
12  extends CollectionBase
13 {
14  private $index = -1;
15 
16  protected $isClone = false;
17 
18  protected $anyItemDeleted = false;
19 
20  /**
21  * EntityCollection constructor.
22  */
23  protected function __construct() {}
24 
25  /**
26  * @internal
27  *
28  * @param CollectableEntity $item
29  * @param null $name
30  * @param null $oldValue
31  * @param null $value
32  * @return Sale\Result
33  */
34  public function onItemModify(CollectableEntity $item, $name = null, $oldValue = null, $value = null)
35  {
36  return new Sale\Result();
37  }
38 
39  /**
40  * @throws Main\NotImplementedException
41  */
42  public static function getRegistryType()
43  {
44  throw new Main\NotImplementedException();
45  }
46 
47  /**
48  * @internal
49  *
50  * @param $index
51  * @return mixed
52  * @throws Main\ArgumentOutOfRangeException
53  */
54  public function deleteItem($index)
55  {
56  if (!isset($this->collection[$index]))
57  {
58  throw new Main\ArgumentOutOfRangeException("collection item index wrong");
59  }
60 
61  $oldItem = $this->collection[$index];
62 
63  $eventManager = Main\EventManager::getInstance();
64  $eventsList = $eventManager->findEventHandlers('sale', 'OnBeforeCollectionDeleteItem');
65  if (!empty($eventsList))
66  {
67  /** @var Main\Entity\Event $event */
68  $event = new Main\Event('sale', 'OnBeforeCollectionDeleteItem', array(
69  'COLLECTION' => $this->collection,
70  'ENTITY' => $oldItem,
71  ));
72  $event->send();
73  }
74 
75  unset($this->collection[$index]);
76  $this->setAnyItemDeleted(true);
77 
78  return $oldItem;
79  }
80 
81  /**
82  * @param CollectableEntity $item
83  * @return CollectableEntity
84  * @throws Main\ArgumentTypeException
85  */
86  protected function addItem(CollectableEntity $item)
87  {
88  $index = $this->createIndex();
89  $item->setInternalIndex($index);
90 
91  $this->collection[$index] = $item;
92 
93  $eventManager = Main\EventManager::getInstance();
94  $eventsList = $eventManager->findEventHandlers('sale', 'OnCollectionAddItem');
95  if (!empty($eventsList))
96  {
97  /** @var Main\Entity\Event $event */
98  $event = new Main\Event('sale', 'OnCollectionAddItem', array(
99  'COLLECTION' => $this->collection,
100  'ENTITY' => $item,
101  ));
102  $event->send();
103  }
104 
105  return $item;
106  }
107 
108  /**
109  * @return int
110  */
111  protected function createIndex()
112  {
113  $this->index++;
114  return $this->index;
115  }
116 
117  /**
118  * @throws Main\ArgumentOutOfRangeException
119  * @throws Main\ObjectNotFoundException
120  */
121  public function clearCollection()
122  {
124 
125  /** @var CollectableEntity $item */
126  foreach ($this->getDeletableItems() as $item)
127  {
128  $item->delete();
129  }
130  }
131 
132  /**
133  * @return array
134  */
135  protected function getDeletableItems()
136  {
137  return $this->collection;
138  }
139 
140 
141  /**
142  * @return void
143  */
144  protected function callEventOnBeforeCollectionClear()
145  {
146  $eventManager = Main\EventManager::getInstance();
147 
148  $eventsList = $eventManager->findEventHandlers('sale', 'OnBeforeCollectionClear');
149  if (!empty($eventsList))
150  {
151  /** @var Main\Entity\Event $event */
152  $event = new Main\Event('sale', 'OnBeforeCollectionClear', array(
153  'COLLECTION' => $this->collection,
154  ));
155  $event->send();
156  }
157  }
158 
159  /**
160  * @param $id
161  *
162  * @return CollectableEntity|bool
163  * @throws Main\ArgumentNullException
164  */
165  public function getItemById($id)
166  {
167  if (intval($id) <= 0)
168  {
169  throw new Main\ArgumentNullException('id');
170  }
171 
172  $index = $this->getIndexById($id);
173  if ($index === null)
174  {
175  return null;
176  }
177 
178  if (isset($this->collection[$index]))
179  {
180  return $this->collection[$index];
181  }
182 
183  return null;
184  }
185 
186 
187  /**
188  * @param $id
189  *
190  * @return bool|int|null
191  * @throws Main\ArgumentNullException
192  */
193  public function getIndexById($id)
194  {
195  if (intval($id) <= 0)
196  {
197  throw new Main\ArgumentNullException('id');
198  }
199 
200  /** @var CollectableEntity $item */
201  foreach ($this->collection as $item)
202  {
203  if ($item->getId() > 0 && $id == $item->getId())
204  {
205  return $item->getInternalIndex();
206  }
207  }
208  return null;
209  }
210 
211  /**
212  * @param $index
213  *
214  * @return CollectableEntity|null
215  * @throws Main\ArgumentNullException
216  */
217  public function getItemByIndex($index)
218  {
219  if (intval($index) < 0)
220  {
221  throw new Main\ArgumentNullException('id');
222  }
223 
224  /** @var CollectableEntity $item */
225  foreach ($this->collection as $item)
226  {
227  if ($item->getInternalIndex() == $index)
228  {
229  return $item;
230  }
231  }
232  return null;
233  }
234 
235  /**
236  * @return Entity
237  */
238  abstract protected function getEntityParent();
239 
240  /**
241  * @param bool $isMeaningfulField
242  * @return bool
243  */
244  public function isStartField($isMeaningfulField = false)
245  {
246  $parent = $this->getEntityParent();
247  if ($parent === null)
248  {
249  return false;
250  }
251 
252  return $parent->isStartField($isMeaningfulField);
253  }
254 
255  /**
256  * @return bool
257  */
258  public function clearStartField()
259  {
260  $parent = $this->getEntityParent();
261  if ($parent === null)
262  {
263  return false;
264  }
265 
266  return $parent->clearStartField();
267  }
268 
269  /**
270  * @return bool
271  */
272  public function hasMeaningfulField()
273  {
274  $parent = $this->getEntityParent();
275  if ($parent === null)
276  {
277  return false;
278  }
279 
280  return $parent->hasMeaningfulField();
281  }
282 
283  /**
284  * @param bool $hasMeaningfulField
285  * @return Sale\Result
286  */
287  public function doFinalAction($hasMeaningfulField = false)
288  {
289  $parent = $this->getEntityParent();
290  if ($parent === null)
291  {
292  return new Sale\Result();
293  }
294 
295  return $parent->doFinalAction($hasMeaningfulField);
296  }
297 
298  /**
299  * @return bool
300  */
301  public function isMathActionOnly()
302  {
303  $parent = $this->getEntityParent();
304  if ($parent === null)
305  {
306  return false;
307  }
308 
309  return $parent->isMathActionOnly();
310  }
311 
312  /**
313  * @param bool|false $value
314  * @return bool
315  */
316  public function setMathActionOnly($value = false)
317  {
318  $parent = $this->getEntityParent();
319  if ($parent == null)
320  {
321  return false;
322  }
323 
324  return $parent->setMathActionOnly($value);
325  }
326 
327  /**
328  * @return bool
329  */
330  public function isChanged()
331  {
332  if (count($this->collection) > 0)
333  {
334  /** @var Entity $item */
335  foreach ($this->collection as $item)
336  {
337  if ($item->isChanged())
338  {
339  return true;
340  }
341  }
342  }
343 
344  return $this->isAnyItemDeleted();
345  }
346 
347  /**
348  * @return Sale\Result
349  */
350  public function verify()
351  {
352  return new Sale\Result();
353  }
354 
355  /**
356  * @return bool
357  */
358  public function isClone()
359  {
360  return $this->isClone;
361  }
362 
363 
364  /**
365  * @return bool
366  */
367  public function isAnyItemDeleted()
368  {
369  return $this->anyItemDeleted;
370  }
371 
372  /**
373  * @param $value
374  *
375  * @return bool
376  */
377  protected function setAnyItemDeleted($value)
378  {
379  return $this->anyItemDeleted = ($value === true);
380  }
381 
382  /**
383  * @internal
384  */
385  public function clearChanged()
386  {
387  if (!empty($this->collection))
388  {
389  foreach ($this->collection as $entityItem)
390  {
391  if ($entityItem instanceof Entity)
392  {
393  $entityItem->clearChanged();
394  }
395  }
396  }
397  }
398 
399  /**
400  * @internal
401  * @param \SplObjectStorage $cloneEntity
402  *
403  * @return EntityCollection
404  */
405  public function createClone(\SplObjectStorage $cloneEntity)
406  {
407  if ($this->isClone() && $cloneEntity->contains($this))
408  {
409  return $cloneEntity[$this];
410  }
411 
412  $entityClone = clone $this;
413  $entityClone->isClone = true;
414 
415  if (!$cloneEntity->contains($this))
416  {
417  $cloneEntity[$this] = $entityClone;
418  }
419 
420  /**
421  * @var int key
422  * @var CollectableEntity $entity
423  */
424  foreach ($entityClone->collection as $key => $entity)
425  {
426  if (!$cloneEntity->contains($entity))
427  {
428  $cloneEntity[$entity] = $entity->createClone($cloneEntity);
429  }
430 
431  $entityClone->collection[$key] = $cloneEntity[$entity];
432  }
433 
434  return $entityClone;
435  }
436 }
Bitrix\Sale\Internals\EntityCollection\getIndexById
getIndexById($id)
Definition: entitycollection.php:193
Bitrix\Sale\Internals\CollectableEntity
Definition: collectableentity.php:12
Bitrix\Sale\Internals\EntityCollection\getDeletableItems
getDeletableItems()
Definition: entitycollection.php:135
Bitrix\Sale\Internals\EntityCollection\setMathActionOnly
setMathActionOnly($value=false)
Definition: entitycollection.php:316
Bitrix\Sale\Internals\EntityCollection\isStartField
isStartField($isMeaningfulField=false)
Definition: entitycollection.php:244
Bitrix\Main
Bitrix\Sale\Internals\CollectionBase
Definition: collectionbase.php:18
Bitrix\Sale\Internals\EntityCollection\clearCollection
clearCollection()
Definition: entitycollection.php:121
Bitrix\Sale\Internals\CollectableEntity\setInternalIndex
setInternalIndex($index)
Definition: collectableentity.php:75
Bitrix\Main\DB\Result
Definition: main/lib/db/result.php:18
Bitrix\Sale\Internals\CollectionBase\count
count()
Count elements of an object.
Definition: collectionbase.php:79
Bitrix\Sale\Internals\CollectionBase\$collection
$collection
Definition: collectionbase.php:21
Bitrix\Sale\Internals
Definition: accountnumber.php:3
Bitrix\Main\EventManager\getInstance
static getInstance()
Definition: main/lib/eventmanager.php:34
Bitrix\Main\ORM\Entity
Base entity.
Definition: main/lib/orm/entity.php:25
Bitrix\Sale\Internals\EntityCollection\isAnyItemDeleted
isAnyItemDeleted()
Definition: entitycollection.php:367
Bitrix\Sale\Internals\EntityCollection\isMathActionOnly
isMathActionOnly()
Definition: entitycollection.php:301
Bitrix\Sale\Internals\EntityCollection\addItem
addItem(CollectableEntity $item)
Definition: entitycollection.php:86
Bitrix\Sale\Internals\EntityCollection\getRegistryType
static getRegistryType()
Definition: entitycollection.php:42
Bitrix\Sale\Internals\EntityCollection\getEntityParent
getEntityParent()
Bitrix\Sale\Internals\EntityCollection\doFinalAction
doFinalAction($hasMeaningfulField=false)
Definition: entitycollection.php:287
Bitrix\Main\Event
Definition: main/lib/event.php:4
Bitrix\Sale\Internals\EntityCollection\verify
verify()
Definition: entitycollection.php:350
Bitrix\Sale\Internals\EntityCollection\createClone
createClone(\SplObjectStorage $cloneEntity)
Definition: entitycollection.php:405
Bitrix\Sale\Internals\EntityCollection\callEventOnBeforeCollectionClear
callEventOnBeforeCollectionClear()
Definition: entitycollection.php:144
Bitrix\Main\ArgumentOutOfRangeException
Exception is thrown when the value of an argument is outside the allowable range of values.
Definition: main/lib/exception.php:66
Bitrix\Sale\Internals\EntityCollection\__construct
__construct()
EntityCollection constructor.
Definition: entitycollection.php:23
Bitrix\Sale
Created by PhpStorm.
Bitrix\Sale\Internals\EntityCollection\clearStartField
clearStartField()
Definition: entitycollection.php:258
Bitrix\Sale\Internals\EntityCollection\isChanged
isChanged()
Definition: entitycollection.php:330
Bitrix\Sale\Internals\EntityCollection\createIndex
createIndex()
Definition: entitycollection.php:111
Bitrix\Sale\Internals\EntityCollection\getItemById
getItemById($id)
Definition: entitycollection.php:165
Bitrix\Main\ArgumentNullException
Exception is thrown when "empty" value is passed to a function that does not accept it as a valid arg...
Definition: main/lib/exception.php:53
Bitrix\Sale\Internals\EntityCollection\hasMeaningfulField
hasMeaningfulField()
Definition: entitycollection.php:272
Bitrix\Sale\Internals\EntityCollection
Definition: entitycollection.php:11
Bitrix\Sale\Internals\EntityCollection\deleteItem
deleteItem($index)
Definition: entitycollection.php:54
Bitrix\Sale\Internals\EntityCollection\setAnyItemDeleted
setAnyItemDeleted($value)
Definition: entitycollection.php:377
Bitrix\Sale\Internals\EntityCollection\clearChanged
clearChanged()
Definition: entitycollection.php:385
Bitrix\Sale\Internals\EntityCollection\$anyItemDeleted
$anyItemDeleted
Definition: entitycollection.php:18
Bitrix\Sale\Internals\EntityCollection\isClone
isClone()
Definition: entitycollection.php:358
Bitrix\Sale\Internals\EntityCollection\getItemByIndex
getItemByIndex($index)
Definition: entitycollection.php:217
Bitrix\Sale\Internals\EntityCollection\onItemModify
onItemModify(CollectableEntity $item, $name=null, $oldValue=null, $value=null)
Definition: entitycollection.php:34
Bitrix\Sale\Internals\EntityCollection\$isClone
$isClone
Definition: entitycollection.php:16
Bitrix\Main\NotImplementedException
Exception is thrown when operation is not implemented but should be.
Definition: main/lib/exception.php:146