Passed
Push — master ( e9553e...08b58a )
by Fran
05:36 queued 11s
created

Dto::parseDtoField()   C

Complexity

Conditions 17
Paths 66

Size

Total Lines 43
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
cc 17
eloc 36
nc 66
nop 3
dl 0
loc 43
ccs 0
cts 34
cp 0
crap 306
rs 5.2166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace PSFS\base\dto;
3
4
use PSFS\base\Logger;
5
use PSFS\base\Request;
6
use PSFS\base\Singleton;
7
use PSFS\base\types\helpers\InjectorHelper;
8
9
/**
10
 * Class Dto
11
 * @package PSFS\base\dto
12
 */
13
class Dto extends Singleton implements \JsonSerializable
14
{
15
    public function __construct($hydrate = true)
16
    {
17
        parent::__construct();
18
        if($hydrate) {
19
            $this->fromArray(Request::getInstance()->getData());
20
        }
21
    }
22
23
    /**
24
     * ToArray wrapper
25
     * @return array
26
     */
27
    public function toArray()
28
    {
29
        return $this->__toArray();
30
    }
31
32
    /**
33
     * Convert dto to array representation
34
     * @return array
35
     */
36
    public function __toArray()
37
    {
38
        $dto = array();
39
        try {
40
            $reflectionClass = new \ReflectionClass($this);
41
            $properties = $reflectionClass->getProperties(\ReflectionProperty::IS_PUBLIC);
42
            if (count($properties) > 0) {
43
                /** @var \ReflectionProperty $property */
44
                foreach ($properties as $property) {
45
                    $value = $property->getValue($this);
46
                    if(is_object($value) && method_exists($value, 'toArray')) {
47
                        $dto[$property->getName()] = $value->toArray();
48
                    } elseif(is_array($value)) {
49
                        foreach($value as &$arrValue) {
50
                            if($arrValue instanceof Dto) {
51
                                $arrValue = $arrValue->toArray();
52
                            }
53
                        }
54
                        $dto[$property->getName()] = $value;
55
                    } else {
56
                        $dto[$property->getName()] = $property->getValue($this);
57
                    }
58
                }
59
            }
60
        } catch (\Exception $e) {
61
            Logger::log(get_class($this) . ': ' . $e->getMessage(), LOG_ERR);
62
        }
63
        return $dto;
64
    }
65
66
    /**
67
     * Convert to string representation
68
     * @return string
69
     */
70
    public function __toString()
71
    {
72
        return get_class($this);
73
    }
74
75
    /**
76
     * @param array $properties
77
     * @param string $key
78
     * @param mixed $value
79
     */
80
    protected function parseDtoField(array $properties, $key, $value = null) {
81
        $type = 'string';
82
        $is_array = false;
83
        if(array_key_exists($key, $properties)) {
84
            $type = $properties[$key];
85
            if(preg_match('/(\[\]|Array)/i', $type)) {
86
                $type = preg_replace('/(\[\]|Array)/i', '', $type);
87
                $is_array = true;
88
            }
89
        }
90
        $reflector = (class_exists($type)) ? new \ReflectionClass($type) : null;
91
        if(null !== $reflector && $reflector->isSubclassOf(Dto::class)) {
92
            if(null !== $value && is_array($value)) {
93
                if($is_array) {
94
                    $this->$key = [];
95
                    foreach($value as $data) {
96
                        if(null !== $data && is_array($data)) {
97
                            $dto = new $type(false);
98
                            $dto->fromArray($data);
99
                            array_push($this->$key, $dto);
100
                        }
101
                    }
102
                } else {
103
                    $this->$key = new $type(false);
104
                    $this->$key->fromArray($value);
105
                }
106
            }
107
        } else {
108
            switch($type) {
109
                default:
110
                case 'string':
111
                    $this->$key = $value;
112
                    break;
113
                case 'integer':
114
                    $this->$key = (integer)$value;
115
                    break;
116
                case 'float':
117
                    $this->$key = (float)$value;
118
                    break;
119
                case 'boolean':
120
                case 'bool':
121
                    $this->$key = (bool)$value;
122
                    break;
123
            }
124
        }
125
    }
126
127
    /**
128
     * Hydrate object from array
129
     * @param array $object
130
     */
131
    public function fromArray(array $object = array())
132
    {
133
        if (count($object) > 0) {
134
            $reflector = new \ReflectionClass($this);
135
            $properties = InjectorHelper::extractProperties($reflector, \ReflectionProperty::IS_PUBLIC, InjectorHelper::VAR_PATTERN);
136
            unset($reflector);
137
            foreach ($object as $key => $value) {
138
                if (property_exists($this, $key) && null !== $value) {
139
                    $this->parseDtoField($properties, $key, $value);
140
                }
141
            }
142
        }
143
    }
144
145
    /**
146
     * @return array|mixed
147
     */
148
    public function jsonSerialize()
149
    {
150
        return $this->toArray();
151
    }
152
}