Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ParamArray.php
1<?php
2
4
6use Bitrix\Im\Model\EO_MessageParam;
7use Bitrix\Im\Model\EO_MessageParam_Collection;
13use Bitrix\Im\V2\Common\RegistryEntryImplementation;
14
22{
23 use RegistryEntryImplementation;
24
25 protected ?string $type = null;
26
27 protected ?string $name = null;
28
29 protected ?int $messageId = null;
30
31 // Object changed flag
32 protected bool $isChanged = true;
33
34 // Object marked to drop
35 protected bool $markedDrop = false;
36
40 public function load($source): Result
41 {
42 $result = parent::load($source);
43 if ($result->isSuccess())
44 {
45 foreach ($this as $param)
46 {
47 $this
48 ->setName($param->getName())
49 ->setMessageId($param->getMessageId())
50 ;
51 break;
52 }
53
54 $this->markChanged(false);
55 }
56
57 return $result;
58 }
59
60 //region Param value
61
66 public function setValue($values): self
67 {
68 if (!is_array($values))
69 {
70 $values = [$values];
71 }
72 if (empty($values))
73 {
74 return $this->markDrop();
75 }
76 switch ($this->type)
77 {
79 $values = array_map('intVal', $values);
80 break;
81
83 $values = array_map('strVal', $values);
84 }
85
86 foreach ($this as $param)
87 {
88 if (!$param->isDeleted() && in_array($param->getValue(), $values, true))
89 {
90 $inx = array_search($param->getValue(), $values, true);
91 if ($inx !== false)
92 {
93 unset($values[$inx]);
94 }
95 }
96 else
97 {
98 $param->markDrop();
99 $this->markChanged();
100 }
101 }
102
103 if (!empty($values))
104 {
105 foreach ($values as $value)
106 {
107 $this->addValue($value);
108 }
109
110 $this->markChanged();
111 }
112
113 return $this;
114 }
115
119 public function getDefaultValue(): array
120 {
121 $value = [];
122 $type = Params::getType($this->name);
123 if (isset($type['default']))
124 {
125 $value = $type['default'];
126 }
127
128 return $value;
129 }
130
134 public function hasValue(): bool
135 {
136 return $this->count() > 0;
137 }
138
142 public function getValue(): array
143 {
144 $values = $this->getDefaultValue() ?: [];
145 foreach ($this as $param)
146 {
147 if ($param->isDeleted())
148 {
149 continue;
150 }
151 switch ($this->type)
152 {
154 $values[] = (int)$param->getValue();
155 break;
156
158 $values[] = (string)$param->getValue();
159 break;
160
161 default:
162 $values[] = $param->getValue();
163 }
164 }
165
166 return $values;
167 }
168
173 public function addValue($value): self
174 {
175 switch ($this->type)
176 {
178 $value = (int)$value;
179 break;
180
182 $value = (string)$value;
183 }
184
185 foreach ($this as $param)
186 {
187 if ($param->getValue() === $value)
188 {
189 return $this;
190 }
191 }
192
193 $param = new Param;
194 $param
195 ->setName($this->getName())
197 ->setValue($value)
198 ;
199
200 if ($this->getMessageId())
201 {
202 $param->setMessageId($this->getMessageId());
203 }
204
205 if ($param->getPrimaryId())
206 {
207 $param->setRegistry($this);
208 }
209 else
210 {
211 $this[] = $param;
212 }
213
214 $this->markChanged();
215
216 return $this;
217 }
218
223 public function unsetValue($values = []): self
224 {
225 if (!empty($values))
226 {
227 if (!is_array($values))
228 {
229 $values = [$values];
230 }
231 switch ($this->type)
232 {
234 $values = array_map('intVal', $values);
235 break;
236
238 $values = array_map('strval', $values);
239 }
240
241 foreach ($this as $param)
242 {
243 if (in_array($param->getValue(), $values, true))
244 {
245 $param->markDrop();
246 }
247 }
248
249 $this->markChanged();
250 }
251 else
252 {
253 foreach ($this as $param)
254 {
255 $param->markDrop();
256 }
257
258 if ($this->getRegistry())
259 {
260 unset($this->getRegistry()[$this->getName()]);
261 }
262
263 $this->markDrop();
264 }
265
266 return $this;
267 }
268
269 public function isHidden(): bool
270 {
271 return Params::getType($this->name)['isHidden'] ?? false;
272 }
273
277 public function toRestFormat(): ?array
278 {
279 return array_map('strval', $this->getValue());
280 }
281
285 public function toPullFormat(): ?array
286 {
287 return $this->toRestFormat();
288 }
289
290
291 //endregion
292
293 //region Setters & Getters
294
295 public function setMessageId(int $messageId): self
296 {
297 $this->messageId = $messageId;
298 foreach ($this as $value)
299 {
300 $value->setMessageId($this->messageId);
301 }
302
303 $this->markChanged();
304
305 return $this;
306 }
307
308 public function getMessageId(): ?int
309 {
310 return $this->messageId;
311 }
312
317 public function setName(string $name): self
318 {
319 if ($this->name = $name)
320 {
321 $this->markChanged();
322 }
323 $this->name = $name;
324 $this->detectType();
325 foreach ($this as $param)
326 {
327 $param->setName($this->name);
328 }
329
330 return $this;
331 }
332
333 public function getName(): ?string
334 {
335 return $this->name;
336 }
337
338 public function setType(string $type): self
339 {
340 if ($this->type != $type)
341 {
342 $this->markChanged();
343 }
344 $this->type = $type;
345 foreach ($this as $param)
346 {
347 switch ($this->type)
348 {
350 $param->setType(Param::TYPE_INT);
351 break;
352
354 $param->setType(Param::TYPE_STRING);
355 }
356 }
357
358 return $this;
359 }
360
361 public function getType(): string
362 {
363 return $this->type ?? Param::TYPE_STRING_ARRAY;
364 }
365
366 public function isValid(): Result
367 {
368 return new Result();
369 }
370
371 public function detectType(): self
372 {
373 if (!empty($this->name))
374 {
375 $type = Params::getType($this->name);
376 $this->setType($type['type'] ?? Param::TYPE_STRING_ARRAY);
377 }
378
379 return $this;
380 }
381
382 //endregion
383
384 //region Data storage
385
390 public function markChanged(?bool $state = null): self
391 {
392 if ($state === null)
393 {
394 $this->isChanged = true;
395 }
396 else
397 {
398 $this->isChanged = $state;
399 }
400 if ($this->isChanged)
401 {
402 $this->markedDrop = false;
403 }
404 return $this;
405 }
406
411 public function isChanged(): bool
412 {
413 return $this->isChanged;
414 }
415
420 public function markDrop(): self
421 {
422 $this->markedDrop = true;
423 return $this;
424 }
425
430 public function isDeleted(): bool
431 {
432 if ($this->markedDrop)
433 {
434 return true;
435 }
436
437 foreach ($this as $param)
438 {
439 if (!$param->isDeleted())
440 {
441 return false;
442 }
443 }
444
445 return true;
446 }
447
448
449 public static function getCollectionElementClass(): string
450 {
451 return Param::class;
452 }
453
454 public static function find(array $filter, array $order, ?int $limit = null, ?Context $context = null): Collection
455 {
456 $query = MessageParamTable::query()
457 ->setFilter($filter)
458 ->setOrder($order)
459 ;
460
461 if (isset($limit))
462 {
463 $query->setLimit($limit);
464 }
465
466 return new static($query->fetchCollection());
467 }
468
469 //endregion
470
471 public function __clone()
472 {
473 foreach ($this as $key => $param)
474 {
475 $this[$key] = clone $param;
476 if ($this[$key] instanceof RegistryEntry)
477 {
478 $this->setRegistry($this);
479 }
480 }
481 }
482}
static find(array $filter, array $order, ?int $limit=null, ?Context $context=null)
markChanged(?bool $state=null)
setName(string $name)
Definition Param.php:301
static getType(string $paramName)
Definition Params.php:277
getRegistry()
setRegistry(Registry $registry)