Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
PriceCollection.php
1<?php
2
4
6
16{
18 protected $factory;
19
21 {
22 $this->factory = $factory;
23 }
24
25 public function findBasePrice(): ?BasePrice
26 {
28 foreach ($this->getIterator() as $price)
29 {
30 if ($price->isPriceBase())
31 {
32 return $price;
33 }
34 }
35
36 return null;
37 }
38
39 public function findByGroupId(int $groupId): ?BasePrice
40 {
42 foreach ($this->getIterator() as $price)
43 {
44 if ($price->getGroupId() === $groupId)
45 {
46 return $price;
47 }
48 }
49
50 return null;
51 }
52
53 public function create(): BasePrice
54 {
55 $price = $this->factory->createEntity();
56
57 $this->add($price);
58
59 return $price;
60 }
61
66 public function setValues(array $values): self
67 {
68 $preparedValues = $this->prepareValues($values);
69
70 foreach ($preparedValues as $id => $fields)
71 {
72 $price = $this->findByGroupId($id);
73
74 if ($price === null)
75 {
76 // ToDo make all collections with factory methods?
77 $price = $this->create()->setGroupId($id);
78 }
79
80 if ($price)
81 {
82 $price->setPrice($fields['PRICE'] ?? null);
83
84 if (isset($fields['CURRENCY']))
85 {
86 $price->setCurrency($fields['CURRENCY']);
87 }
88 }
89 }
90
91 return $this;
92 }
93
94 public function getValues(): array
95 {
96 $values = [];
97
99 foreach ($this->getIterator() as $item)
100 {
101 if ($item->hasField('PRICE') && $item->hasField('CURRENCY'))
102 {
103 $values[$item->getGroupId()] = [
104 'PRICE' => $item->getPrice(),
105 'CURRENCY' => $item->getCurrency(),
106 ];
107 }
108 }
109
110 return $values;
111 }
112
113 public function hasBasePrice(): bool
114 {
115 $basePrice = $this->findBasePrice();
116
117 return $basePrice && $basePrice->hasPrice();
118 }
119
120 public function hasAnyPrice(): bool
121 {
122 return !empty($this->getValues());
123 }
124
125 private function prepareValues(array $values): array
126 {
127 // ToDo check required properties + QUANTITY_FROM etc
128 $prepared = [];
129
130 foreach ($values as $id => $fields)
131 {
132 if (!is_array($fields))
133 {
134 if (is_numeric($fields) && is_finite($fields))
135 {
136 $fields = ['PRICE' => $fields];
137 }
138 else
139 {
140 continue;
141 }
142 }
143
144 $fields = array_intersect_key($fields, ['PRICE' => true, 'CURRENCY' => true]);
145 if (!empty($fields))
146 {
147 if (isset($fields['PRICE']))
148 {
149 if (is_numeric($fields['PRICE']) && is_finite($fields['PRICE']))
150 {
151 $fields['PRICE'] = (float)$fields['PRICE'];
152 }
153 else
154 {
155 $fields['PRICE'] = null;
156 }
157 }
158
159 if (isset($fields['CURRENCY']) && !is_string($fields['CURRENCY']))
160 {
161 unset($fields['CURRENCY']);
162 }
163 }
164
165 if (!empty($fields))
166 {
167 if (is_numeric($id))
168 {
169 $id = (int)$id;
170 $prepared[$id] = $fields;
171 }
172 elseif ($id === 'BASE')
173 {
174 $basePrice = \CCatalogGroup::GetBaseGroup();
175
176 if (!empty($basePrice['ID']))
177 {
178 $id = (int)$basePrice['ID'];
179 $prepared[$id] = $fields;
180 }
181 }
182 }
183 }
184
185 return $prepared;
186 }
187}