@@ -12,155 +12,155 @@ |
||
12 | 12 | abstract class Entity |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * @var array The raw data passed to this entity |
|
17 | - */ |
|
18 | - protected array $raw_data = []; |
|
19 | - |
|
20 | - /** |
|
21 | - * Entity constructor. |
|
22 | - * |
|
23 | - * @param ?array $data The raw data passed to this entity |
|
24 | - */ |
|
25 | - public function __construct(?array $data) |
|
26 | - { |
|
27 | - if (!empty($data)) { |
|
28 | - $this->assignMemberVariables(($this->raw_data = $data)); |
|
29 | - $this->validate(); |
|
30 | - } |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * Get the raw data passed to this entity |
|
35 | - * |
|
36 | - * @param bool $associated |
|
37 | - * @return array|string |
|
38 | - */ |
|
39 | - public function getRawData(bool $associated = true): array|string |
|
40 | - { |
|
41 | - return $associated ? $this->raw_data : json_encode($this->raw_data); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Helper to set member variables |
|
46 | - * |
|
47 | - * @param array $data |
|
48 | - * @return void |
|
49 | - */ |
|
50 | - protected function assignMemberVariables(array $data): void |
|
51 | - { |
|
52 | - foreach ($data as $key => $value) { |
|
53 | - $this->$key = $value; |
|
54 | - } |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Get a property from the current Entity |
|
59 | - * |
|
60 | - * @param string $property |
|
61 | - * @param mixed $default |
|
62 | - * |
|
63 | - * @return mixed |
|
64 | - */ |
|
65 | - public function getProperty(string $property, mixed $default = null): mixed |
|
66 | - { |
|
67 | - return $this->raw_data[$property] ?? $default; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Get the list of the properties that are themselves Entities |
|
72 | - * |
|
73 | - * @return array |
|
74 | - */ |
|
75 | - protected function subEntities(): array |
|
76 | - { |
|
77 | - return []; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Perform any special entity validation |
|
82 | - * |
|
83 | - * @return void |
|
84 | - */ |
|
85 | - protected function validate(): void |
|
86 | - { |
|
87 | - // Do nothing by default |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param string $name The name of the property |
|
92 | - * @param array $arguments The arguments passed to the method |
|
93 | - * @return mixed |
|
94 | - */ |
|
95 | - public function __call(string $name, array $arguments): mixed |
|
96 | - { |
|
97 | - if (method_exists($this, $name)) { |
|
98 | - return $this->{$name}(...$arguments); |
|
99 | - } |
|
100 | - |
|
101 | - if (str_starts_with($name, 'get')) { |
|
102 | - $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
103 | - |
|
104 | - $property = $this->getProperty($property_name); |
|
105 | - $sub_entities = $this->subEntities() ?? []; |
|
106 | - |
|
107 | - if (isset($sub_entities[$property_name])) { |
|
108 | - $class_name = $sub_entities[$property_name]; |
|
109 | - return Factory::resolveEntityClass($class_name, $property); |
|
110 | - } |
|
111 | - |
|
112 | - return $property ?? null; |
|
113 | - } |
|
114 | - |
|
115 | - if (str_starts_with($name, 'set')) { |
|
116 | - $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
117 | - |
|
118 | - if (property_exists($this, $property_name)) { |
|
119 | - $this->{$property_name} = $arguments[0]; |
|
120 | - $this->raw_data[$property_name] = $arguments[0]; |
|
121 | - |
|
122 | - return $this; |
|
123 | - } |
|
124 | - |
|
125 | - } |
|
126 | - |
|
127 | - throw new \BadMethodCallException("Method '$name' does not exist"); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Escape markdown (v1) special characters |
|
132 | - * |
|
133 | - * @see https://core.telegram.org/bots/api#markdown-style |
|
134 | - * |
|
135 | - * @param string $string |
|
136 | - * |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - public static function escapeMarkdown(string $string): string |
|
140 | - { |
|
141 | - return str_replace( |
|
142 | - ['[', '`', '*', '_',], |
|
143 | - ['\[', '\`', '\*', '\_',], |
|
144 | - $string |
|
145 | - ); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Escape markdown (v2) special characters |
|
150 | - * |
|
151 | - * @see https://core.telegram.org/bots/api#markdownv2-style |
|
152 | - * |
|
153 | - * @param string $string |
|
154 | - * |
|
155 | - * @return string |
|
156 | - */ |
|
157 | - public static function escapeMarkdownV2(string $string): string |
|
158 | - { |
|
159 | - return str_replace( |
|
160 | - ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'], |
|
161 | - ['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'], |
|
162 | - $string |
|
163 | - ); |
|
164 | - } |
|
15 | + /** |
|
16 | + * @var array The raw data passed to this entity |
|
17 | + */ |
|
18 | + protected array $raw_data = []; |
|
19 | + |
|
20 | + /** |
|
21 | + * Entity constructor. |
|
22 | + * |
|
23 | + * @param ?array $data The raw data passed to this entity |
|
24 | + */ |
|
25 | + public function __construct(?array $data) |
|
26 | + { |
|
27 | + if (!empty($data)) { |
|
28 | + $this->assignMemberVariables(($this->raw_data = $data)); |
|
29 | + $this->validate(); |
|
30 | + } |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * Get the raw data passed to this entity |
|
35 | + * |
|
36 | + * @param bool $associated |
|
37 | + * @return array|string |
|
38 | + */ |
|
39 | + public function getRawData(bool $associated = true): array|string |
|
40 | + { |
|
41 | + return $associated ? $this->raw_data : json_encode($this->raw_data); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Helper to set member variables |
|
46 | + * |
|
47 | + * @param array $data |
|
48 | + * @return void |
|
49 | + */ |
|
50 | + protected function assignMemberVariables(array $data): void |
|
51 | + { |
|
52 | + foreach ($data as $key => $value) { |
|
53 | + $this->$key = $value; |
|
54 | + } |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Get a property from the current Entity |
|
59 | + * |
|
60 | + * @param string $property |
|
61 | + * @param mixed $default |
|
62 | + * |
|
63 | + * @return mixed |
|
64 | + */ |
|
65 | + public function getProperty(string $property, mixed $default = null): mixed |
|
66 | + { |
|
67 | + return $this->raw_data[$property] ?? $default; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Get the list of the properties that are themselves Entities |
|
72 | + * |
|
73 | + * @return array |
|
74 | + */ |
|
75 | + protected function subEntities(): array |
|
76 | + { |
|
77 | + return []; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Perform any special entity validation |
|
82 | + * |
|
83 | + * @return void |
|
84 | + */ |
|
85 | + protected function validate(): void |
|
86 | + { |
|
87 | + // Do nothing by default |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param string $name The name of the property |
|
92 | + * @param array $arguments The arguments passed to the method |
|
93 | + * @return mixed |
|
94 | + */ |
|
95 | + public function __call(string $name, array $arguments): mixed |
|
96 | + { |
|
97 | + if (method_exists($this, $name)) { |
|
98 | + return $this->{$name}(...$arguments); |
|
99 | + } |
|
100 | + |
|
101 | + if (str_starts_with($name, 'get')) { |
|
102 | + $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
103 | + |
|
104 | + $property = $this->getProperty($property_name); |
|
105 | + $sub_entities = $this->subEntities() ?? []; |
|
106 | + |
|
107 | + if (isset($sub_entities[$property_name])) { |
|
108 | + $class_name = $sub_entities[$property_name]; |
|
109 | + return Factory::resolveEntityClass($class_name, $property); |
|
110 | + } |
|
111 | + |
|
112 | + return $property ?? null; |
|
113 | + } |
|
114 | + |
|
115 | + if (str_starts_with($name, 'set')) { |
|
116 | + $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
117 | + |
|
118 | + if (property_exists($this, $property_name)) { |
|
119 | + $this->{$property_name} = $arguments[0]; |
|
120 | + $this->raw_data[$property_name] = $arguments[0]; |
|
121 | + |
|
122 | + return $this; |
|
123 | + } |
|
124 | + |
|
125 | + } |
|
126 | + |
|
127 | + throw new \BadMethodCallException("Method '$name' does not exist"); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Escape markdown (v1) special characters |
|
132 | + * |
|
133 | + * @see https://core.telegram.org/bots/api#markdown-style |
|
134 | + * |
|
135 | + * @param string $string |
|
136 | + * |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + public static function escapeMarkdown(string $string): string |
|
140 | + { |
|
141 | + return str_replace( |
|
142 | + ['[', '`', '*', '_',], |
|
143 | + ['\[', '\`', '\*', '\_',], |
|
144 | + $string |
|
145 | + ); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Escape markdown (v2) special characters |
|
150 | + * |
|
151 | + * @see https://core.telegram.org/bots/api#markdownv2-style |
|
152 | + * |
|
153 | + * @param string $string |
|
154 | + * |
|
155 | + * @return string |
|
156 | + */ |
|
157 | + public static function escapeMarkdownV2(string $string): string |
|
158 | + { |
|
159 | + return str_replace( |
|
160 | + ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'], |
|
161 | + ['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'], |
|
162 | + $string |
|
163 | + ); |
|
164 | + } |
|
165 | 165 | |
166 | 166 | } |
167 | 167 | \ No newline at end of file |