Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mapper.php
1<?php
2
4
5use Bitrix\Calendar\Core;
10
11abstract class Mapper implements BaseMapperInterface
12{
13 public const POSITIVE_ANSWER = 'Y';
14 public const NEGATIVE_ANSWER = 'N';
15
16 protected const DEFAULT_SELECT = ['*'];
17
19 protected static array $cache = [];
20
28 public function getById(int $id): ?object
29 {
30 if ($this->getCacheMap()->has($id))
31 {
32 return $this->getCacheMap()->getItem($id);
33 }
34
35 $entity = $this->getOneEntityByFilter([
36 '=ID' => $id,
37 ]);
38
39 if (!is_null($entity))
40 {
41 $this->getCacheMap()->add($entity, $id);
42 }
43
44 return $entity;
45 }
46
50 public function getByEntityObject($entityObject): ?Core\Base\EntityInterface
51 {
52 if ($this->getCacheMap()->has($entityObject->getId()))
53 {
54 return $this->getCacheMap()->getItem($entityObject->getId());
55 }
56
57 $entity = $this->convertToObject($entityObject);
58 if ($entity !== null)
59 {
60 $this->getCacheMap()->add($entity, $entity->getId());
61 }
62
63 return $entity;
64 }
65
74 public function create(
75 Core\Base\EntityInterface $entity,
76 array $params = []
77 ): ?Core\Base\EntityInterface
78 {
79 if ($entity->getId())
80 {
81 return null;
82 }
83
84 $resultEntity = $this->createEntity($entity, $params);
85 if ($resultEntity && $resultEntity->getId())
86 {
87 $this->getCacheMap()->add($resultEntity, $resultEntity->getId());
88 return $resultEntity;
89 }
90
91 return null;
92 }
93
99 public function update(
100 Core\Base\EntityInterface $entity,
101 array $params = []
102 ): ?Core\Base\EntityInterface
103 {
104 $resultEntity = $this->updateEntity($entity, $params);
105
106 if ($resultEntity && $resultEntity->getId())
107 {
108 $this->getCacheMap()->updateItem($resultEntity, $resultEntity->getId());
109
110 return $resultEntity;
111 }
112
113 return null;
114 }
115
122 public function delete(
124 array $params = ['softDelete' => true]
125 ): ?Core\Base\EntityInterface
126 {
127 $resultEntity = $this->deleteEntity($entity, $params);
128 if ($resultEntity === null)
129 {
130 $this->getCacheMap()->remove($entity->getId());
131
132 return null;
133 }
134
135 if (!empty($resultEntity->getId()))
136 {
137 $this->getCacheMap()->updateItem($resultEntity, $resultEntity->getId());
138
139 return $resultEntity;
140 }
141
142 return null;
143 }
144
153 public function deleteMap(Core\Base\EntityMap $map, array $params = ['softDelete' => true])
154 {
155 while ($entity = $map->fetch())
156 {
157 $this->delete($entity, $params);
158 }
159 }
160
170 public function deleteByFilter($filter, array $params = ['softDelete' => true]): self
171 {
172 $className = $this->getMapClass();
173 $result = new $className();
174 $paramsForSelect = [
175 'filter' => $filter,
176 'select' => ['ID']
177 ];
178
179 $managerResult = $this->getDataManagerResult($paramsForSelect);
180 while ($row = $managerResult->fetchObject())
181 {
182 if ($this->getCacheMap()->has($row->getId()))
183 {
184 $this->delete($this->getCacheMap()->getItem($row->getId()), $params);
185 }
186 else
187 {
188 // TODO: change to more smarty way. Without build entity object and select database.
189 $entity = $this->getById($row->getId());
190 $this->delete($entity, $params);
191 }
192 }
193
194 return $result;
195 }
196
207 public function getMap($filter, int $limit = null, array $order = null): Core\Base\Map
208 {
209 $className = $this->getMapClass();
210 $result = new $className();
211 $params = ['filter' => $filter];
212 if ($limit)
213 {
214 $params['limit'] = $limit;
215 }
216 if ($order)
217 {
218 $params['order'] = $order;
219 }
220 $params['select'] = self::DEFAULT_SELECT;
221
222 $managerResult = $this->getDataManagerResult($params);
223 while ($row = $managerResult->fetchObject())
224 {
225 $link = $this->getByEntityObject($row);
226 if ($link !== null)
227 {
228 $result->add($link, $link->getId());
229 }
230 }
231
232 return $result;
233 }
234
239 public function resetCacheById(int $id): self
240 {
241 $map = $this->getCacheMap();
242 if ($map->has($id))
243 {
244 $map->remove($id);
245 }
246
247 return $this;
248 }
249
253 final protected function getCacheMap(): Core\Base\Map
254 {
255 if (empty(self::$cache[static::class]))
256 {
257 $this->initCacheMap(static::class);
258 }
259
260 return self::$cache[static::class];
261 }
262
268 protected function initCacheMap(string $class)
269 {
270 $object = (get_class($this) === $class)
271 ? $this
272 : new $class()
273 ;
274 $mapClassName = $object->getMapClass();
275
276 self::$cache[$class] = new $mapClassName();
277 }
278
283 protected function getMapClass(): string
284 {
285 return Core\Base\EntityMap::class;
286 }
287
291 abstract protected function getEntityClass(): string;
292
298 abstract protected function getDataManagerResult(array $params): Result;
299
305 abstract protected function getOneEntityByFilter(array $filter): ?object;
306
307 abstract protected function convertToObject($objectEO): ?Core\Base\EntityInterface;
308
312 abstract protected function getEntityName(): string;
313
319 abstract protected function createEntity($entity, array $params = []): ?Core\Base\EntityInterface;
320
326 abstract protected function updateEntity($entity, array $params = []): ?Core\Base\EntityInterface;
327
333 abstract protected function deleteEntity(Core\Base\EntityInterface $entity, array $params): ?Core\Base\EntityInterface;
334}
updateEntity($entity, array $params=[])
getMap($filter, int $limit=null, array $order=null)
Definition mapper.php:207
deleteMap(Core\Base\EntityMap $map, array $params=['softDelete'=> true])
Definition mapper.php:153
deleteByFilter($filter, array $params=['softDelete'=> true])
Definition mapper.php:170
update(Core\Base\EntityInterface $entity, array $params=[])
Definition mapper.php:99
deleteEntity(Core\Base\EntityInterface $entity, array $params)
create(Core\Base\EntityInterface $entity, array $params=[])
Definition mapper.php:74
createEntity($entity, array $params=[])