1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
AbstractCommand.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
6
7use BackedEnum;
18use ReflectionAttribute;
19use ReflectionClass;
20use ReflectionProperty;
21use TypeError;
22
23abstract 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
Определения options.php:106
getOverrideClass(ReflectionProperty $reflectionProperty)
Определения AbstractCommand.php:199
static createFromArray(array|Arrayable $data)
Определения AbstractCommand.php:62
setProperty(string $property, array $args)
Определения AbstractCommand.php:169
__call(string $name, array $args)
Определения AbstractCommand.php:151
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$name
Определения menu_edit.php:35
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$instance
Определения ps_b24_final.php:14
if(empty($signedUserToken)) $key
Определения quickway.php:257