Passed
Push — master ( e941dd...34caab )
by Fran
06:00 queued 02:19
created

Dto::parseDtoField()   D

Complexity

Conditions 16
Paths 72

Size

Total Lines 48
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
cc 16
eloc 39
nc 72
nop 3
dl 0
loc 48
ccs 0
cts 36
cp 0
crap 272
rs 4.9765
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 Propel\Runtime\ActiveRecord\ActiveRecordInterface;
5
use PSFS\base\config\Config;
6
use PSFS\base\Logger;
7
use PSFS\base\Request;
8
use PSFS\base\Singleton;
9
use PSFS\base\types\helpers\InjectorHelper;
10
11
/**
12
 * Class Dto
13
 * @package PSFS\base\dto
14
 */
15
class Dto extends Singleton
16
{
17
18
    public function __construct($hydrate = true)
19
    {
20
        parent::__construct();
21
        if($hydrate) {
22
            $this->fromArray(Request::getInstance()->getData());
23
        }
24
    }
25
26
    /**
27
     * ToArray wrapper
28
     * @return array
29
     */
30
    public function toArray()
31
    {
32
        return $this->__toArray();
33
    }
34
35
    /**
36
     * Convert dto to array representation
37
     * @return array
38
     */
39
    public function __toArray()
40
    {
41
        $dto = array();
42
        try {
43
            $reflectionClass = new \ReflectionClass($this);
44
            $properties = $reflectionClass->getProperties(\ReflectionProperty::IS_PUBLIC);
45
            if (count($properties) > 0) {
46
                /** @var \ReflectionProperty $property */
47
                foreach ($properties as $property) {
48
                    $value = $property->getValue($this);
49
                    if(is_object($value) && method_exists($value, 'toArray')) {
50
                        $dto[$property->getName()] = $value->toArray();
51
                    } else {
52
                        $dto[$property->getName()] = $property->getValue($this);
53
                    }
54
                }
55
            }
56
        } catch (\Exception $e) {
57
            Logger::log(get_class($this) . ': ' . $e->getMessage(), LOG_ERR);
58
        }
59
        return $dto;
60
    }
61
62
    /**
63
     * Convert to string representation
64
     * @return string
65
     */
66
    public function __toString()
67
    {
68
        return get_class($this);
69
    }
70
71
    /**
72
     * @param array $properties
73
     * @param string $key
74
     * @param mixed $value
75
     */
76
    protected function parseDtoField(array $properties, $key, $value = null) {
77
        $type = 'string';
78
        $is_array = false;
79
        if(array_key_exists($key, $properties)) {
80
            $type = $properties[$key];
81
            if(preg_match('/(\[\]|Array)/i', $type)) {
82
                $type = preg_replace('/(\[\]|Array)/i', '', $type);
83
                $is_array = true;
84
            }
85
        }
86
        $reflector = (class_exists($type)) ? new \ReflectionClass($type) : null;
87
        if(null !== $reflector && $reflector->isSubclassOf(Dto::class)) {
88
            if($is_array) {
89
                $this->$key = [];
90
                foreach($value as $data) {
91
                    if(null !== $data) {
92
                        $dto = new $type(false);
93
                        $dto->fromArray($data);
94
                        array_push($this->$key, $dto);
95
                    }
96
                }
97
            } else {
98
                if(null !== $value) {
99
                    $this->$key = new $type(false);
100
                    $this->$key->fromArray($value);
101
                } elseif(Config::getParam('api.default.null', true)) {
102
                    $this->$key = null;
103
                }
104
            }
105
        } else {
106
            switch($type) {
107
                default:
108
                case 'string':
109
                    $this->$key = $value;
110
                    break;
111
                case 'integer':
112
                    $this->$key = (integer)$value;
113
                    break;
114
                case 'float':
115
                    $this->$key = (float)$value;
116
                    break;
117
                case 'boolean':
118
                case 'bool':
119
                    $this->$key = (bool)$value;
120
                    break;
121
            }
122
        }
123
    }
124
125
    /**
126
     * Hydrate object from array
127
     * @param array $object
128
     */
129
    public function fromArray(array $object = array())
130
    {
131
        if (count($object) > 0) {
132
            $reflector = new \ReflectionClass($this);
133
            $properties = InjectorHelper::extractProperties($reflector, \ReflectionProperty::IS_PUBLIC, InjectorHelper::VAR_PATTERN);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
134
            unset($reflector);
135
            foreach ($object as $key => $value) {
136
                if (property_exists($this, $key)) {
137
                    $this->parseDtoField($properties, $key, $value);
138
                }
139
            }
140
        }
141
    }
142
}