Completed
Pull Request — develop (#291)
by Armando
13:31
created

Entity   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 240
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 54%

Importance

Changes 6
Bugs 4 Features 0
Metric Value
wmc 39
c 6
b 4
f 0
lcom 1
cbo 0
dl 0
loc 240
ccs 27
cts 50
cp 0.54
rs 8.2857

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getBotName() 0 4 1
A __construct() 0 6 1
A toJson() 0 4 1
C reflect() 0 69 20
A __toString() 0 4 1
A assignMemberVariables() 0 6 2
A subEntities() 0 4 1
A validate() 0 3 1
A getProperty() 0 8 2
B __call() 0 22 4
B makePrettyObjectArray() 0 18 5
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 1
38
    /**
39 1
     * Entity constructor.
40 1
     *
41
     * @todo Get rid of the $bot_name, it shouldn't be here!
42 1
     *
43
     * @param        $data
44
     * @param string $bot_name
45
     *
46
     * @throws \Longman\TelegramBot\Exception\TelegramException
47
     */
48
    public function __construct($data, $bot_name = '')
49
    {
50
        $this->assignMemberVariables($data);
51 1
        $this->validate();
52
        $this->bot_name = $bot_name;
53 1
    }
54
55
    /**
56
     * Perform to json
57 1
     *
58 1
     * @return string
59
     */
60 1
    public function toJson()
61
    {
62 1
        return json_encode($this->reflect($this));
63 1
    }
64 1
65 1
    /**
66
     * Reflect
67
     *
68 1
     * @param null $object
69 1
     *
70 1
     * @return array
71 1
     */
72
    public function reflect($object = null)
73
    {
74
        if ($object === null) {
75
            $object = $this;
76
        }
77
78
        $reflection = new ReflectionObject($object);
79
        $properties = $reflection->getProperties();
80
81
        $fields = [];
82
83
        foreach ($properties as $property) {
84
            $name = $property->getName();
85
            if ($name === 'bot_name') {
86
                continue;
87
            }
88
89
            if (!$property->isPrivate()) {
90 1
                $array_of_obj       = false;
91 1
                $array_of_array_obj = false;
92 1
                if (is_array($object->$name)) {
93
                    $array_of_obj       = true;
94
                    $array_of_array_obj = true;
95
                    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
                            $array_of_obj = false;
99
                            //break;
100
                        }
101
                        if (is_array($elm)) {
102
                            foreach ($elm as $more_net) {
103
                                if (!is_object($more_net)) {
104
                                    $array_of_array_obj = false;
105
                                }
106
                            }
107
                        }
108 1
                    }
109 1
                }
110 1
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
                    $property->setAccessible(true);
130
                    $value = $property->getValue($object);
131
                    if (null === $value) {
132
                        continue;
133
                    }
134
                    $fields[$name] = $value;
135
                }
136
            }
137
        }
138
139
        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
    protected function assignMemberVariables(array $data)
158
    {
159
        foreach ($data as $key => $value) {
160
            $this->$key = $value;
161
        }
162
    }
163
164
    /**
165
     * Get the list of the properties that are themselves Entities
166
     *
167
     * @return array
168
     */
169
    protected function subEntities()
170
    {
171
        return [];
172
    }
173
174
    /**
175
     * Perform any special entity validation
176
     *
177
     * @throws \Longman\TelegramBot\Exception\TelegramException
178
     */
179
    protected function validate()
180
    {
181
    }
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
    public function getProperty($property, $default = null)
192
    {
193
        if (isset($this->$property)) {
194
            return $this->$property;
195
        }
196
197
        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
    public function __call($method, $args)
209
    {
210
        $action = substr($method, 0, 3);
211
        if ($action === 'get') {
212
            //Convert method to snake_case (which is the name of the property)
213
            $property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_');
214
            $property      = $this->getProperty($property_name);
215
216
            if ($property !== null) {
217
                //Get all sub-Entities of the current Entity
218
                $sub_entities = $this->subEntities();
219
220
                if (isset($sub_entities[$property_name])) {
221
                    return new $sub_entities[$property_name]($property);
222
                }
223
224
                return $property;
225
            }
226
        }
227
228
        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
    protected function makePrettyObjectArray($class, $property)
243
    {
244
        $new_objects = [];
245
246
        try {
247
            if ($objects = $this->getProperty($property)) {
248
                foreach ($objects as $object) {
249
                    if (!empty($object)) {
250
                        $new_objects[] = new $class($object);
251
                    }
252
                }
253
            }
254
        } catch (Exception $e) {
255
            $new_objects = [];
256
        }
257
258
        return $new_objects;
259
    }
260
}
261