Entity::set()   A
last analyzed

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