1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
AbstractCommand.php
См. документацию.
1
<?php
2
3
declare(strict_types=1);
4
5
namespace
Bitrix\Socialnetwork\Control\Command
;
6
7
use BackedEnum;
8
use
Bitrix\Main\ArgumentException
;
9
use
Bitrix\Main\ObjectException
;
10
use
Bitrix\Main\ObjectNotFoundException
;
11
use
Bitrix\Main\Type\Contract\Arrayable
;
12
use
Bitrix\Main\Type\DateTime
;
13
use
Bitrix\Socialnetwork\Control\Command\Attribute\AccessController
;
14
use
Bitrix\Socialnetwork\Control\Command\Attribute\Override
;
15
use
Bitrix\Socialnetwork\Control\Command\ValueObject\CreateObjectInterface
;
16
use
Bitrix\Socialnetwork\Control\Command\ValueObject\CreateWithDefaultValueInterface
;
17
use
Bitrix\Socialnetwork\ValueObjectInterface
;
18
use ReflectionAttribute;
19
use ReflectionClass;
20
use ReflectionProperty;
21
use TypeError;
22
23
abstract
class
AbstractCommand
implements
Arrayable
24
{
28
public
function
__construct
()
29
{
30
$properties = (
new
ReflectionClass($this))->getProperties();
31
32
foreach
($properties as $property)
33
{
34
$name
= $property->getName();
35
$type
= $property->getType()?->getName();
36
37
$overrideType = $this->
getOverrideClass
($property);
38
39
if
($overrideType !==
null
)
40
{
41
if
(!is_subclass_of($overrideType,
$type
))
42
{
43
throw
new
ObjectException
(
'Cannot override property'
);
44
}
45
46
$type
= $overrideType;
47
}
48
49
if
(
50
$this instanceof
DefaultValueCommandInterface
51
&& is_subclass_of(
$type
, CreateWithDefaultValueInterface::class)
52
)
53
{
54
$this->{
$name
} = $type::createWithDefaultValue();
55
}
56
}
57
}
58
62
public
static
function
createFromArray
(
array
|
Arrayable
$data
): static
63
{
64
if
(
$data
instanceof
Arrayable
)
65
{
66
$data
=
$data
->toArray();
67
}
68
69
$instance
=
new
static
();
70
$reflection =
new
ReflectionClass(
$instance
);
71
72
foreach
(
$data
as
$key
=> $value)
73
{
74
if
($value ===
null
)
75
{
76
continue
;
77
}
78
79
if
(!$reflection->hasProperty(
$key
))
80
{
81
continue
;
82
}
83
84
$reflectionProperty = $reflection->getProperty(
$key
);
85
$type
= $reflectionProperty->getType();
86
$typeName =
$type
?->getName();
87
try
88
{
89
if
(
$type
===
null
||
$type
->isBuiltin())
90
{
91
$instance
->{
$key
} = $value;
92
}
93
elseif
(is_subclass_of($typeName, BackedEnum::class))
94
{
95
$instance
->{
$key
} = $typeName::tryFrom($value);
96
}
97
elseif
(is_subclass_of($typeName, DateTime::class))
98
{
99
$instance
->{
$key
} = $typeName::createFromUserTime($value);
100
}
101
elseif
(is_subclass_of($typeName, CreateObjectInterface::class))
102
{
103
$instance
->{
$key
} = $typeName::create($value);
104
}
105
}
106
catch
(TypeError)
107
{
108
}
109
catch
(ObjectNotFoundException $e)
110
{
111
throw
new
ArgumentException($e->getMessage(), $e->getCode());
112
}
113
}
114
115
return
$instance
;
116
}
117
118
public
function
toArray
():
array
119
{
120
$properties = (
new
ReflectionClass($this))->getProperties();
121
122
$data
= [];
123
124
foreach
($properties as $property)
125
{
126
if
(!$property->isInitialized($this))
127
{
128
continue
;
129
}
130
131
$name
= $property->getName();
132
$value = $property->getValue($this);
133
134
if
($value instanceof
ValueObjectInterface
)
135
{
136
$data
[
$name
] = $value->getValue();
137
}
138
elseif
($value instanceof BackedEnum)
139
{
140
$data
[
$name
] = $value->value;
141
}
142
else
143
{
144
$data
[
$name
] = $value;
145
}
146
}
147
148
return
$data
;
149
}
150
151
public
function
__call
(
string
$name
,
array
$args)
152
{
153
$operation = substr(
$name
, 0, 3);
154
$property = lcfirst(substr(
$name
, 3));
155
156
if
($operation ===
'set'
)
157
{
158
return
$this->
setProperty
($property, $args);
159
}
160
161
if
($operation ===
'get'
)
162
{
163
return
$this->{$property} ??
null
;
164
}
165
166
return
null
;
167
}
168
169
protected
function
setProperty
(
string
$property,
array
$args): static
170
{
171
$reflection =
new
ReflectionClass($this);
172
if
(!$reflection->hasProperty($property))
173
{
174
return
$this;
175
}
176
177
$reflectionProperty = $reflection->getProperty($property);
178
if
($reflectionProperty->isReadOnly())
179
{
180
return
$this;
181
}
182
183
$this->{$property} = $args[0];
184
185
return
$this;
186
}
187
188
protected
function
getAccessControllerClass
(): ?string
189
{
190
$attributes = (
new
ReflectionClass($this))->getAttributes(AccessController::class, ReflectionAttribute::IS_INSTANCEOF);
191
foreach
($attributes as $attributeReflection)
192
{
193
return
$attributeReflection->newInstance()->class;
194
}
195
196
return
null
;
197
}
198
199
protected
function
getOverrideClass
(ReflectionProperty $reflectionProperty): ?string
200
{
201
$attributes = $reflectionProperty->getAttributes(Override::class, ReflectionAttribute::IS_INSTANCEOF);
202
foreach
($attributes as $attributeReflection)
203
{
204
return
$attributeReflection->newInstance()->class;
205
}
206
207
return
null
;
208
}
209
}
$type
$type
Определения
options.php:106
Bitrix\Main\ArgumentException
Определения
ArgumentException.php:9
Bitrix\Main\ObjectException
Определения
ObjectException.php:9
Bitrix\Main\ObjectNotFoundException
Определения
ObjectNotFoundException.php:9
Bitrix\Main\Type\DateTime
Определения
datetime.php:9
Bitrix\Socialnetwork\Control\Command\AbstractCommand
Определения
AbstractCommand.php:24
Bitrix\Socialnetwork\Control\Command\AbstractCommand\__construct
__construct()
Определения
AbstractCommand.php:28
Bitrix\Socialnetwork\Control\Command\AbstractCommand\toArray
toArray()
Определения
AbstractCommand.php:118
Bitrix\Socialnetwork\Control\Command\AbstractCommand\getOverrideClass
getOverrideClass(ReflectionProperty $reflectionProperty)
Определения
AbstractCommand.php:199
Bitrix\Socialnetwork\Control\Command\AbstractCommand\createFromArray
static createFromArray(array|Arrayable $data)
Определения
AbstractCommand.php:62
Bitrix\Socialnetwork\Control\Command\AbstractCommand\setProperty
setProperty(string $property, array $args)
Определения
AbstractCommand.php:169
Bitrix\Socialnetwork\Control\Command\AbstractCommand\__call
__call(string $name, array $args)
Определения
AbstractCommand.php:151
Bitrix\Socialnetwork\Control\Command\AbstractCommand\getAccessControllerClass
getAccessControllerClass()
Определения
AbstractCommand.php:188
Bitrix\Socialnetwork\Control\Command\Attribute\AccessController
Определения
AccessController.php:13
Bitrix\Socialnetwork\Control\Command\Attribute\Override
Определения
Override.php:11
$data
$data['IS_AVAILABLE']
Определения
.description.php:13
array
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения
file_new.php:804
Bitrix\Main\Type\Contract\Arrayable
Определения
arrayable.php:6
Bitrix\Socialnetwork\Control\Command\DefaultValueCommandInterface
Определения
DefaultValueCommandInterface.php:6
Bitrix\Socialnetwork\Control\Command\ValueObject\CreateObjectInterface
Определения
CreateObjectInterface.php:6
Bitrix\Socialnetwork\Control\Command\ValueObject\CreateWithDefaultValueInterface
Определения
CreateWithDefaultValueInterface.php:6
Bitrix\Socialnetwork\ValueObjectInterface
Определения
ValueObjectInterface.php:6
$name
$name
Определения
menu_edit.php:35
Bitrix\Socialnetwork\Control\Command
Определения
AbstractCommand.php:5
elseif
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения
prolog_main_admin.php:393
$instance
$instance
Определения
ps_b24_final.php:14
$key
if(empty($signedUserToken)) $key
Определения
quickway.php:257
bitrix
modules
socialnetwork
lib
Control
Command
AbstractCommand.php
Создано системой
1.14.0