1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ParamArray.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Message;
4
5use Bitrix\Main\ORM;
6use Bitrix\Im\Model\EO_MessageParam;
7use Bitrix\Im\Model\EO_MessageParam_Collection;
8use Bitrix\Im\Model\MessageParamTable;
9use Bitrix\Im\V2\Collection;
10use Bitrix\Im\V2\Result;
11use Bitrix\Im\V2\Service\Context;
12use Bitrix\Im\V2\RegistryEntry;
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())
196 ->setType($this->type == Param::TYPE_INT_ARRAY ? Param::TYPE_INT : Param::TYPE_STRING)
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 if ($this->messageId !== $messageId)
298 {
299 $this->markChanged();
300 }
301
302 $this->messageId = $messageId;
303
304 foreach ($this as $value)
305 {
306 $value->setMessageId($this->messageId);
307 }
308
309 return $this;
310 }
311
312 public function getMessageId(): ?int
313 {
314 return $this->messageId;
315 }
316
321 public function setName(string $name): self
322 {
323 if ($this->name = $name)
324 {
325 $this->markChanged();
326 }
327 $this->name = $name;
328 $this->detectType();
329 foreach ($this as $param)
330 {
331 $param->setName($this->name);
332 }
333
334 return $this;
335 }
336
337 public function getName(): ?string
338 {
339 return $this->name;
340 }
341
342 public function setType(string $type): self
343 {
344 if ($this->type != $type)
345 {
346 $this->markChanged();
347 }
348 $this->type = $type;
349 foreach ($this as $param)
350 {
351 switch ($this->type)
352 {
354 $param->setType(Param::TYPE_INT);
355 break;
356
358 $param->setType(Param::TYPE_STRING);
359 }
360 }
361
362 return $this;
363 }
364
365 public function getType(): string
366 {
367 return $this->type ?? Param::TYPE_STRING_ARRAY;
368 }
369
370 public function isValid(): Result
371 {
372 return new Result();
373 }
374
375 public function detectType(): self
376 {
377 if (!empty($this->name))
378 {
379 $type = Params::getType($this->name);
380 $this->setType($type['type'] ?? Param::TYPE_STRING_ARRAY);
381 }
382
383 return $this;
384 }
385
386 //endregion
387
388 //region Data storage
389
394 public function markChanged(?bool $state = null): self
395 {
396 if ($state === null)
397 {
398 $this->isChanged = true;
399 }
400 else
401 {
402 $this->isChanged = $state;
403 }
404 if ($this->isChanged)
405 {
406 $this->markedDrop = false;
407 }
408 return $this;
409 }
410
415 public function isChanged(): bool
416 {
417 return $this->isChanged;
418 }
419
424 public function markDrop(): self
425 {
426 $this->markedDrop = true;
427 return $this;
428 }
429
434 public function isDeleted(): bool
435 {
436 if ($this->markedDrop)
437 {
438 return true;
439 }
440
441 foreach ($this as $param)
442 {
443 if (!$param->isDeleted())
444 {
445 return false;
446 }
447 }
448
449 return true;
450 }
451
452
453 public static function getCollectionElementClass(): string
454 {
455 return Param::class;
456 }
457
458 public static function find(array $filter, array $order, ?int $limit = null, ?Context $context = null): Collection
459 {
460 $query = MessageParamTable::query()
461 ->setFilter($filter)
462 ->setOrder($order)
463 ;
464
465 if (isset($limit))
466 {
467 $query->setLimit($limit);
468 }
469
470 return new static($query->fetchCollection());
471 }
472
473 //endregion
474
475 public function __clone()
476 {
477 foreach ($this as $key => $param)
478 {
479 $this[$key] = clone $param;
480 if ($this[$key] instanceof RegistryEntry)
481 {
482 $this->setRegistry($this);
483 }
484 }
485 }
486}
static find(array $filter, array $order, ?int $limit=null, ?Context $context=null)
Определения ParamArray.php:458
setName(string $name)
Определения ParamArray.php:321
static getCollectionElementClass()
Определения ParamArray.php:453
setValue($values)
Определения ParamArray.php:66
addValue($value)
Определения ParamArray.php:173
load($source)
Определения ParamArray.php:40
unsetValue($values=[])
Определения ParamArray.php:223
setMessageId(int $messageId)
Определения ParamArray.php:295
markChanged(?bool $state=null)
Определения ParamArray.php:394
setType(string $type)
Определения ParamArray.php:342
static getType(string $paramName)
Определения Params.php:288
Определения result.php:20
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
$filter
Определения iblock_catalog_list.php:54
Определения RegistryEntry.php:6
getRegistry()
setRegistry(Registry $registry)
$context
Определения csv_new_setup.php:223
const TYPE_INT_ARRAY
Определения Param.php:22
const TYPE_STRING
Определения Param.php:17
const TYPE_STRING_ARRAY
Определения Param.php:21
Определения culture.php:9
$order
Определения payment.php:8
if(empty($signedUserToken)) $key
Определения quickway.php:257