Passed
Push — master ( be88e3...87c6b6 )
by Fran
04:17
created

Dto   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 121
ccs 0
cts 64
cp 0
rs 9.84
c 0
b 0
f 0
wmc 32

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __toArray() 0 21 6
C parseDtoField() 0 43 17
A fromArray() 0 9 5
A toArray() 0 3 1
A __construct() 0 5 2
A __toString() 0 3 1
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(null !== $value && is_array($value)) {
89
                if($is_array) {
90
                    $this->$key = [];
91
                    foreach($value as $data) {
92
                        if(null !== $data && is_array($data)) {
93
                            $dto = new $type(false);
94
                            $dto->fromArray($data);
95
                            array_push($this->$key, $dto);
96
                        }
97
                    }
98
                } else {
99
                    $this->$key = new $type(false);
100
                    $this->$key->fromArray($value);
101
                }
102
            }
103
        } else {
104
            switch($type) {
105
                default:
106
                case 'string':
107
                    $this->$key = $value;
108
                    break;
109
                case 'integer':
110
                    $this->$key = (integer)$value;
111
                    break;
112
                case 'float':
113
                    $this->$key = (float)$value;
114
                    break;
115
                case 'boolean':
116
                case 'bool':
117
                    $this->$key = (bool)$value;
118
                    break;
119
            }
120
        }
121
    }
122
123
    /**
124
     * Hydrate object from array
125
     * @param array $object
126
     */
127
    public function fromArray(array $object = array())
128
    {
129
        if (count($object) > 0) {
130
            $reflector = new \ReflectionClass($this);
131
            $properties = InjectorHelper::extractProperties($reflector, \ReflectionProperty::IS_PUBLIC, InjectorHelper::VAR_PATTERN);
132
            unset($reflector);
133
            foreach ($object as $key => $value) {
134
                if (property_exists($this, $key) && null !== $value) {
135
                    $this->parseDtoField($properties, $key, $value);
136
                }
137
            }
138
        }
139
    }
140
}