Completed
Push — feature/improve-code ( e986a1...36787d )
by Avtandil
14:14
created

Entity::__toString()   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 ReflectionObject;
14
15
class Entity
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $bot_name;
21
22
    /**
23
     * Get bot name
24
     *
25
     * @return string
26
     */
27
    public function getBotName()
28
    {
29
        return $this->bot_name;
30
    }
31
32
    /**
33
     * Perform to json
34
     *
35
     * @return string
36
     */
37 1
    public function toJson()
38
    {
39 1
        $fields = $this->reflect($this);
40 1
        $json   = json_encode($fields);
41
42 1
        return $json;
43
    }
44
45
    /**
46
     * Reflect
47
     *
48
     * @param null $object
49
     * @return array
50
     */
51 1
    public function reflect($object = null)
52
    {
53 1
        if ($object == null) {
54
            $object = $this;
55
        }
56
57 1
        $reflection = new ReflectionObject($object);
58 1
        $properties = $reflection->getProperties();
59
60 1
        $fields = [];
61
62 1
        foreach ($properties as $property) {
63 1
            $name = $property->getName();
64 1
            if ($name == 'bot_name') {
65 1
                continue;
66
            }
67
68 1
            if (!$property->isPrivate()) {
69 1
                $array_of_obj       = false;
70 1
                $array_of_array_obj = false;
71 1
                if (is_array($object->$name)) {
72
                    $array_of_obj       = true;
73
                    $array_of_array_obj = true;
74
                    foreach ($object->$name as $elm) {
75
                        if (!is_object($elm)) {
76
                            //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...
77
                            $array_of_obj = false;
78
                            //break;
79
                        }
80
                        if (is_array($elm)) {
81
                            foreach ($elm as $more_net) {
82
                                if (!is_object($more_net)) {
83
                                    $array_of_array_obj = false;
84
                                }
85
                            }
86
                        }
87
                    }
88
                }
89
90 1
                if (is_object($object->$name)) {
91 1
                    $fields[$name] = $this->reflect($object->$name);
92 1
                } elseif ($array_of_obj) {
93
                    foreach ($object->$name as $elm) {
94
                        $fields[$name][] = $this->reflect($elm);
95
                    }
96 1
                } elseif ($array_of_array_obj) {
97
                    foreach ($object->$name as $elm) {
98
                        $temp = null;
99
                        if (!is_array($elm) && !is_object($elm)) {
100
                            continue;
101
                        }
102
                        foreach ($elm as $obj) {
103
                            $temp[] = $this->reflect($obj);
104
                        }
105
                        $fields[$name][] = $temp;
106
                    }
107
                } else {
108 1
                    $property->setAccessible(true);
109 1
                    $value = $property->getValue($object);
110 1
                    if (is_null($value)) {
111 1
                        continue;
112
                    }
113 1
                    $fields[$name] = $value;
114
                }
115
            }
116
        }
117 1
        return $fields;
118
    }
119
120
    /**
121
     * Perform to string
122
     *
123
     * @return string
124
     */
125
    public function __toString()
126
    {
127
        return $this->toJson();
128
    }
129
}
130