Completed
Pull Request — develop (#291)
by Armando
15:42
created

Entity::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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 1
    }
38
39 1
    /**
40 1
     * Entity constructor.
41
     *
42 1
     * @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
    public function __construct($data, $bot_name = '')
50
    {
51 1
        $this->assignMemberVariables($data);
52
        $this->validate();
53 1
        $this->bot_name = $bot_name;
54
    }
55
56
    /**
57 1
     * Perform to json
58 1
     *
59
     * @return string
60 1
     */
61
    public function toJson()
62 1
    {
63 1
        return json_encode($this->reflect($this));
64 1
    }
65 1
66
    /**
67
     * Reflect
68 1
     *
69 1
     * @param null $object
70 1
     *
71 1
     * @return array
72
     */
73
    public function reflect($object = null)
74
    {
75
        if ($object === null) {
76
            $object = $this;
77
        }
78
79
        $reflection = new ReflectionObject($object);
80
        $properties = $reflection->getProperties();
81
82
        $fields = [];
83
84
        foreach ($properties as $property) {
85
            $name = $property->getName();
86
            if ($name === 'bot_name') {
87
                continue;
88
            }
89
90 1
            if (!$property->isPrivate()) {
91 1
                $array_of_obj       = false;
92 1
                $array_of_array_obj = false;
93
                if (is_array($object->$name)) {
94
                    $array_of_obj       = true;
95
                    $array_of_array_obj = true;
96 1
                    foreach ($object->$name as $elm) {
97
                        if (!is_object($elm)) {
98
                            //echo $name . " not array of object \n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
                            $array_of_obj = false;
100
                            //break;
101
                        }
102
                        if (is_array($elm)) {
103
                            foreach ($elm as $more_net) {
104
                                if (!is_object($more_net)) {
105
                                    $array_of_array_obj = false;
106
                                }
107
                            }
108 1
                        }
109 1
                    }
110 1
                }
111 1
112
                if (is_object($object->$name)) {
113 1
                    $fields[$name] = $this->reflect($object->$name);
114
                } elseif ($array_of_obj) {
115
                    foreach ($object->$name as $elm) {
116
                        $fields[$name][] = $this->reflect($elm);
117 1
                    }
118
                } 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
                    $property->setAccessible(true);
131
                    $value = $property->getValue($object);
132
                    if (null === $value) {
133
                        continue;
134
                    }
135
                    $fields[$name] = $value;
136
                }
137
            }
138
        }
139
140
        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
    protected function assignMemberVariables(array $data)
159
    {
160
        foreach ($data as $key => $value) {
161
            $this->$key = $value;
162
        }
163
    }
164
165
    /**
166
     * Get the list of the properties that are themselves Entities
167
     *
168
     * @return array
169
     */
170
    protected function subEntities()
171
    {
172
        return [];
173
    }
174
175
    /**
176
     * Perform any special entity validation
177
     *
178
     * @throws \Longman\TelegramBot\Exception\TelegramException
179
     */
180
    protected function validate()
181
    {
182
    }
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
    public function getProperty($property, $default = null)
193
    {
194
        if (isset($this->$property)) {
195
            return $this->$property;
196
        }
197
198
        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
    public function __call($method, $args)
210
    {
211
        //Convert method to snake_case (which is the name of the property)
212
        $property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_');
213
214
        $action = substr($method, 0, 3);
215
        if ($action === 'get') {
216
            $property = $this->getProperty($property_name);
217
218
            if ($property !== null) {
219
                //Get all sub-Entities of the current Entity
220
                $sub_entities = $this->subEntities();
221
222
                if (isset($sub_entities[$property_name])) {
223
                    return new $sub_entities[$property_name]($property);
224
                }
225
226
                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
        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
    protected function makePrettyObjectArray($class, $property)
252
    {
253
        $new_objects = [];
254
255
        try {
256
            if ($objects = $this->getProperty($property)) {
257
                foreach ($objects as $object) {
258
                    if (!empty($object)) {
259
                        $new_objects[] = new $class($object);
260
                    }
261
                }
262
            }
263
        } catch (Exception $e) {
264
            $new_objects = [];
265
        }
266
267
        return $new_objects;
268
    }
269
270
    /**
271
     * stripMarkDown
272
     * Gived a  string escape special charactes used in Markdown
273
     *
274
     * @param string $string
275
     *
276
     * @return string
277
     */
278
    public function stripMarkDown($string)
279
    {
280
        return str_replace(
281
            ['[', '`', '*', '_',],
282
            ['\[', '\`', '\*', '\_',],
283
            $string
284
        );
285
    }
286
}
287