Completed
Pull Request — develop (#291)
by Armando
03:51
created

Entity::getBotName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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