Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ImageCollection.php
1<?php
2
4
7
17{
19 protected $factory;
20
22 {
23 $this->factory = $factory;
24 }
25
26 public function create(string $type = null): BaseImage
27 {
29 {
30 $entity = $this->findByType($type);
31 if ($entity)
32 {
33 throw new NotSupportedException(sprintf(
34 'Collection {%s} already contains {%s} entity.', static::class, $type
35 ));
36 }
37 }
38
39 $image = $this->factory->createEntity($type);
40
41 $this->add($image);
42
43 return $image;
44 }
45
46 public function getDetailImage(): BaseImage
47 {
48 $detailImage = $this->findByType(ImageFactory::DETAIL_IMAGE);
49
50 if (!$detailImage)
51 {
52 $detailImage = $this->create(ImageFactory::DETAIL_IMAGE);
53 }
54
55 return $detailImage;
56 }
57
58 public function getPreviewImage(): BaseImage
59 {
60 $previewImage = $this->findByType(ImageFactory::PREVIEW_IMAGE);
61
62 if (!$previewImage)
63 {
64 $previewImage = $this->create(ImageFactory::PREVIEW_IMAGE);
65 }
66
67 return $previewImage;
68 }
69
70 protected function findByType(string $type): ?BaseImage
71 {
73 foreach ($this->getIterator() as $item)
74 {
75 if ($item instanceof $type)
76 {
77 return $item;
78 }
79 }
80
81 return null;
82 }
83
87 public function getMorePhotos(): array
88 {
89 $morePhotos = [];
90 foreach ($this->getIterator() as $item)
91 {
92 if ($item instanceof MorePhotoImage)
93 {
94 $morePhotos[] = $item;
95 }
96 }
97
98 return $morePhotos;
99 }
100
101 public function getFrontImage(): ?BaseImage
102 {
103 $picture = $this->getDetailImage();
104 if (!$picture->isNew())
105 {
106 return $picture;
107 }
108
109 $picture = $this->getPreviewImage();
110 if (!$picture->isNew())
111 {
112 return $picture;
113 }
114
116 $picture = $this->getFirst();
117
118 return !$picture->isNew() ? $picture : null;
119 }
120
121 public function getValues(): array
122 {
123 $values = [];
124
126 foreach ($this->getIterator() as $image)
127 {
128 $values[] = $image->isNew() ? $image->getFileStructure() : $image->getId();
129 }
130
131 return $values;
132 }
133
138 public function setValues(array $values): self
139 {
140 $this->removeOldValues($values);
141 $this->addValues($values);
142
143 return $this;
144 }
145
146 public function addValues(array $values): self
147 {
148 foreach ($this->prepareValues($values) as $value)
149 {
150 if (is_array($value))
151 {
152 $this->addValue($value);
153 }
154 }
155
156 return $this;
157 }
158
159 public function addValue(array $value): void
160 {
161 if (!$value)
162 {
163 return;
164 }
165
166 $entity = $this->create();
167 $entity->setFileStructure($value);
168 }
169
170 private function prepareValues(array $values): array
171 {
172 if (isset($values['name']) || isset($values['tmp_name']))
173 {
174 $values = [$values];
175 }
176
177 return $values;
178 }
179
180 private function removeOldValues(array $values): void
181 {
182 $valuesToSave = [];
183
184 foreach ($this->prepareValues($values) as $value)
185 {
186 if (!empty($value) && is_numeric($value))
187 {
188 $valuesToSave[] = (int)$value;
189 }
190 }
191
192 foreach ($this->getIterator() as $entity)
193 {
194 if ($entity->isNew() || !in_array($entity->getId(), $valuesToSave, true))
195 {
196 $entity->remove();
197 }
198 }
199 }
200}
getFirst(callable $callback=null)