Passed
Push — master ( 941273...52f3c0 )
by Fran
08:30
created

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