Completed
Push — refactor_entities ( c38610...2711d6 )
by Armando
03:36
created

Entity::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Entities;
12
13
use Exception;
14
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
15
use ReflectionObject;
16
17
/**
18
 * Class Entity
19
 *
20
 * This is the base class for all entities.
21
 */
22
abstract class Entity
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $bot_name;
28
29
    /**
30
     * Get bot name
31
     *
32
     * @return string
33
     */
34
    public function getBotName()
35
    {
36
        return $this->bot_name;
37
    }
38
39
    /**
40
     * Entity constructor.
41
     *
42
     * @todo Get rid of the $bot_name, it shouldn't be here!
43
     *
44
     * @param        $data
45
     * @param string $bot_name
46
     *
47
     * @throws \Longman\TelegramBot\Exception\TelegramException
48
     */
49 25
    public function __construct($data, $bot_name = '')
50
    {
51 25
        $this->assignMemberVariables($data);
52 25
        $this->validate();
53 22
        $this->bot_name = $bot_name;
54 22
    }
55
56
    /**
57
     * Perform to json
58
     *
59
     * @return string
60
     */
61 1
    public function toJson()
62
    {
63 1
        return json_encode($this->reflect($this));
64
    }
65
66
    /**
67
     * Reflect
68
     *
69
     * @param null $object
70
     *
71
     * @return array
72
     */
73 1
    public function reflect($object = null)
74
    {
75 1
        if ($object === null) {
76
            $object = $this;
77
        }
78
79 1
        $reflection = new ReflectionObject($object);
80 1
        $properties = $reflection->getProperties();
81
82 1
        $fields = [];
83
84 1
        foreach ($properties as $property) {
85 1
            $name = $property->getName();
86 1
            if ($name === 'bot_name') {
87 1
                continue;
88
            }
89
90 1
            if (!$property->isPrivate()) {
91 1
                $array_of_obj       = false;
92 1
                $array_of_array_obj = false;
93 1
                if (is_array($object->$name)) {
94 1
                    $array_of_obj       = true;
95 1
                    $array_of_array_obj = true;
96 1
                    foreach ($object->$name as $elm) {
97 1
                        if (!is_object($elm)) {
98
                            //echo $name . " not array of object \n";
99 1
                            $array_of_obj = false;
100
                            //break;
101
                        }
102 1
                        if (is_array($elm)) {
103 1
                            foreach ($elm as $more_net) {
104 1
                                if (!is_object($more_net)) {
105 1
                                    $array_of_array_obj = false;
106
                                }
107
                            }
108
                        }
109
                    }
110
                }
111
112 1
                if (is_object($object->$name)) {
113
                    $fields[$name] = $this->reflect($object->$name);
114 1
                } elseif ($array_of_obj) {
115
                    foreach ($object->$name as $elm) {
116
                        $fields[$name][] = $this->reflect($elm);
117
                    }
118 1
                } elseif ($array_of_array_obj) {
119
                    foreach ($object->$name as $elm) {
120
                        $temp = null;
121
                        if (!is_array($elm) && !is_object($elm)) {
122
                            continue;
123
                        }
124
                        foreach ($elm as $obj) {
125
                            $temp[] = $this->reflect($obj);
126
                        }
127
                        $fields[$name][] = $temp;
128
                    }
129
                } else {
130 1
                    $property->setAccessible(true);
131 1
                    $value = $property->getValue($object);
132 1
                    if (null === $value) {
133
                        continue;
134
                    }
135 1
                    $fields[$name] = $value;
136
                }
137
            }
138
        }
139
140 1
        return $fields;
141
    }
142
143
    /**
144
     * Perform to string
145
     *
146
     * @return string
147
     */
148
    public function __toString()
149
    {
150
        return $this->toJson();
151
    }
152
153
    /**
154
     * Helper to set member variables
155
     *
156
     * @param array $data
157
     */
158 25
    protected function assignMemberVariables(array $data)
159
    {
160 25
        foreach ($data as $key => $value) {
161 24
            $this->$key = $value;
162
        }
163 25
    }
164
165
    /**
166
     * Get the list of the properties that are themselves Entities
167
     *
168
     * @return array
169
     */
170 14
    protected function subEntities()
171
    {
172 14
        return [];
173
    }
174
175
    /**
176
     * Perform any special entity validation
177
     *
178
     * @throws \Longman\TelegramBot\Exception\TelegramException
179
     */
180 21
    protected function validate()
181
    {
182 21
    }
183
184
    /**
185
     * Get a property from the current Entity
186
     *
187
     * @param mixed $property
188
     * @param mixed $default
189
     *
190
     * @return mixed
191
     */
192 21
    public function getProperty($property, $default = null)
193
    {
194 21
        if (isset($this->$property)) {
195 19
            return $this->$property;
196
        }
197
198 14
        return $default;
199
    }
200
201
    /**
202
     * Return the variable for the called getter or magically set properties dynamically.
203
     *
204
     * @param $method
205
     * @param $args
206
     *
207
     * @return mixed|null
208
     */
209 17
    public function __call($method, $args)
210
    {
211
        //Convert method to snake_case (which is the name of the property)
212 17
        $property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_');
213
214 17
        $action = substr($method, 0, 3);
215 17
        if ($action === 'get') {
216 17
            $property = $this->getProperty($property_name);
217
218 17
            if ($property !== null) {
219
                //Get all sub-Entities of the current Entity
220 17
                $sub_entities = $this->subEntities();
221
222 17
                if (isset($sub_entities[$property_name])) {
223 15
                    return new $sub_entities[$property_name]($property);
224
                }
225
226 16
                return $property;
227
            }
228
        } elseif ($action === 'set') {
229
            // Limit setters to specific classes.
230
            if ($this instanceof InlineEntity || $this instanceof Keyboard || $this instanceof KeyboardButton) {
231
                $this->$property_name = $args[0];
232
233
                return $this;
234
            }
235
        }
236
237 10
        return null;
238
    }
239
240
    /**
241
     * Return an array of nice objects from an array of object arrays
242
     *
243
     * This method is used to generate pretty object arrays
244
     * mainly for PhotoSize and Entities object arrays.
245
     *
246
     * @param string $class
247
     * @param string $property
248
     *
249
     * @return array
250
     */
251 6
    protected function makePrettyObjectArray($class, $property)
252
    {
253 6
        $new_objects = [];
254
255
        try {
256 6
            if ($objects = $this->getProperty($property)) {
257
                foreach ($objects as $object) {
258
                    if (!empty($object)) {
259 6
                        $new_objects[] = new $class($object);
260
                    }
261
                }
262
            }
263
        } catch (Exception $e) {
264
            $new_objects = [];
265
        }
266
267 6
        return $new_objects;
268
    }
269
}
270