Passed
Push — master ( 72ea7a...b665cb )
by Shahrad
12:25
created

Entity::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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