Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
PropertyCollection.php
1<?php
2
4
7
17{
19 protected $repository;
20
22 {
23 $this->repository = $repository;
24 }
25
26 public function findBySetting(string $field, $value): ?Property
27 {
29 foreach ($this->getIterator() as $item)
30 {
31 if ($item->getSetting($field) == $value)
32 {
33 return $item;
34 }
35 }
36
37 return null;
38 }
39
40 public function findByCode(string $code): ?Property
41 {
43 foreach ($this->getIterator() as $item)
44 {
45 if ($item->getCode() === $code)
46 {
47 return $item;
48 }
49 }
50
51 return null;
52 }
53
58 public function setValues(array $propertyValues): self
59 {
60 foreach ($propertyValues as $index => $values)
61 {
62 $property = null;
63
64 if (is_numeric($index))
65 {
66 $property = $this->findById((int)$index);
67 }
68
69 if (!$property)
70 {
71 $property = $this->findByCode($index);
72 }
73
74 if ($property)
75 {
76 $property->getPropertyValueCollection()->setValues($values);
77 }
78 }
79
80 return $this;
81 }
82
83 public function getValues(): array
84 {
85 $values = [];
86
88 foreach ($this->getIterator() as $property)
89 {
90 $values[$property->getId()] = $property->getPropertyValueCollection()->toArray();
91 }
92
93 return $values;
94 }
95
96 public function saveInternal(): Result
97 {
98 $result = new Result();
99
100 // ToDo make lazyLoad for getPropertyCollection() to not load collection everytime
101 if ($this->isChanged())
102 {
103 // ToDo re-initialize saved ids from database after file save - check in \CIBlockElement::SetPropertyValues
104 // property collection can't be saved one by one, all simultaneously
105 $res = $this->repository->save(...$this->getIterator());
106
107 if ($res->isSuccess())
108 {
109 $this->clearChanged();
110 }
111 else
112 {
113 $result->addErrors($res->getErrors());
114 }
115 }
116
117 return $result;
118 }
119
120 public function deleteInternal(): Result
121 {
122 // properties deletes with iblock element entity by CIBlockElement api
123 return new Result();
124 }
125}
__construct(PropertyRepositoryContract $repository)