Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
ParamArray.php
1
<?php
2
3
namespace
Bitrix\Im\V2\Message
;
4
5
use
Bitrix\Main\ORM
;
6
use Bitrix\Im\Model\EO_MessageParam;
7
use Bitrix\Im\Model\EO_MessageParam_Collection;
8
use
Bitrix\Im\Model\MessageParamTable
;
9
use
Bitrix\Im\V2\Collection
;
10
use
Bitrix\Im\V2\Result
;
11
use
Bitrix\Im\V2\Service\Context
;
12
use
Bitrix\Im\V2\RegistryEntry
;
13
use Bitrix\Im\V2\Common\RegistryEntryImplementation;
14
21
class
ParamArray
extends
Collection
implements
MessageParameter
,
RegistryEntry
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
{
78
case
Param::TYPE_INT_ARRAY
:
79
$values = array_map(
'intVal'
, $values);
80
break
;
81
82
case
Param::TYPE_STRING
:
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
{
153
case
Param::TYPE_INT_ARRAY
:
154
$values[] = (int)$param->getValue();
155
break
;
156
157
case
Param::TYPE_STRING_ARRAY
:
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
{
177
case
Param::TYPE_INT_ARRAY
:
178
$value = (int)$value;
179
break
;
180
181
case
Param::TYPE_STRING_ARRAY
:
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
{
233
case
Param::TYPE_INT_ARRAY
:
234
$values = array_map(
'intVal'
, $values);
235
break
;
236
237
case
Param::TYPE_STRING
:
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
{
349
case
Param::TYPE_INT_ARRAY
:
350
$param->setType(
Param::TYPE_INT
);
351
break
;
352
353
case
Param::TYPE_STRING_ARRAY
:
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
}
Bitrix\Im\Model\MessageParamTable
Definition
messageparam.php:36
Bitrix\Im\V2\Collection
Definition
Collection.php:14
Bitrix\Im\V2\Message\ParamArray
Definition
ParamArray.php:22
Bitrix\Im\V2\Message\ParamArray\find
static find(array $filter, array $order, ?int $limit=null, ?Context $context=null)
Definition
ParamArray.php:454
Bitrix\Im\V2\Message\ParamArray\$markedDrop
bool $markedDrop
Definition
ParamArray.php:35
Bitrix\Im\V2\Message\ParamArray\setName
setName(string $name)
Definition
ParamArray.php:317
Bitrix\Im\V2\Message\ParamArray\getCollectionElementClass
static getCollectionElementClass()
Definition
ParamArray.php:449
Bitrix\Im\V2\Message\ParamArray\isChanged
isChanged()
Definition
ParamArray.php:411
Bitrix\Im\V2\Message\ParamArray\getName
getName()
Definition
ParamArray.php:333
Bitrix\Im\V2\Message\ParamArray\toPullFormat
toPullFormat()
Definition
ParamArray.php:285
Bitrix\Im\V2\Message\ParamArray\detectType
detectType()
Definition
ParamArray.php:371
Bitrix\Im\V2\Message\ParamArray\getMessageId
getMessageId()
Definition
ParamArray.php:308
Bitrix\Im\V2\Message\ParamArray\$name
string $name
Definition
ParamArray.php:27
Bitrix\Im\V2\Message\ParamArray\setValue
setValue($values)
Definition
ParamArray.php:66
Bitrix\Im\V2\Message\ParamArray\addValue
addValue($value)
Definition
ParamArray.php:173
Bitrix\Im\V2\Message\ParamArray\$isChanged
bool $isChanged
Definition
ParamArray.php:32
Bitrix\Im\V2\Message\ParamArray\isValid
isValid()
Definition
ParamArray.php:366
Bitrix\Im\V2\Message\ParamArray\getType
getType()
Definition
ParamArray.php:361
Bitrix\Im\V2\Message\ParamArray\load
load($source)
Definition
ParamArray.php:40
Bitrix\Im\V2\Message\ParamArray\$type
string $type
Definition
ParamArray.php:25
Bitrix\Im\V2\Message\ParamArray\unsetValue
unsetValue($values=[])
Definition
ParamArray.php:223
Bitrix\Im\V2\Message\ParamArray\setMessageId
setMessageId(int $messageId)
Definition
ParamArray.php:295
Bitrix\Im\V2\Message\ParamArray\markChanged
markChanged(?bool $state=null)
Definition
ParamArray.php:390
Bitrix\Im\V2\Message\ParamArray\getValue
getValue()
Definition
ParamArray.php:142
Bitrix\Im\V2\Message\ParamArray\toRestFormat
toRestFormat()
Definition
ParamArray.php:277
Bitrix\Im\V2\Message\ParamArray\isHidden
isHidden()
Definition
ParamArray.php:269
Bitrix\Im\V2\Message\ParamArray\__clone
__clone()
Definition
ParamArray.php:471
Bitrix\Im\V2\Message\ParamArray\hasValue
hasValue()
Definition
ParamArray.php:134
Bitrix\Im\V2\Message\ParamArray\markDrop
markDrop()
Definition
ParamArray.php:420
Bitrix\Im\V2\Message\ParamArray\isDeleted
isDeleted()
Definition
ParamArray.php:430
Bitrix\Im\V2\Message\ParamArray\$messageId
int $messageId
Definition
ParamArray.php:29
Bitrix\Im\V2\Message\ParamArray\getDefaultValue
getDefaultValue()
Definition
ParamArray.php:119
Bitrix\Im\V2\Message\ParamArray\setType
setType(string $type)
Definition
ParamArray.php:338
Bitrix\Im\V2\Message\Param
Definition
Param.php:19
Bitrix\Im\V2\Message\Param\setName
setName(string $name)
Definition
Param.php:301
Bitrix\Im\V2\Message\Param\TYPE_INT_ARRAY
const TYPE_INT_ARRAY
Definition
Param.php:31
Bitrix\Im\V2\Message\Param\TYPE_STRING
const TYPE_STRING
Definition
Param.php:27
Bitrix\Im\V2\Message\Param\TYPE_INT
const TYPE_INT
Definition
Param.php:28
Bitrix\Im\V2\Message\Param\TYPE_STRING_ARRAY
const TYPE_STRING_ARRAY
Definition
Param.php:30
Bitrix\Im\V2\Message\Params\getType
static getType(string $paramName)
Definition
Params.php:277
Bitrix\Im\V2\Result
Definition
Result.php:9
Bitrix\Im\V2\Service\Context
Definition
Context.php:8
Bitrix\Main\DB\Connection\$host
$host
Definition
connection.php:29
Bitrix\Im\V2\Message\MessageParameter
Definition
MessageParameter.php:8
Bitrix\Im\V2\RegistryEntry
Definition
RegistryEntry.php:6
Bitrix\Im\V2\RegistryEntry\getRegistry
getRegistry()
Bitrix\Im\V2\RegistryEntry\setRegistry
setRegistry(Registry $registry)
Bitrix\Im\V2\Message
Definition
AdditionalMessagePopupItem.php:3
Bitrix\Main\ORM
modules
im
lib
V2
Message
ParamArray.php
Создано системой
1.10.0