Completed
Push — master ( 6986c5...be73e7 )
by Vitalijs
02:50
created

DTOBase::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace CodinPro\DataTransferObject;
4
5
use ArrayAccess;
6
use Countable;
7
use IteratorAggregate;
8
9
class DTOBase implements ArrayAccess, IteratorAggregate, Countable
10
{
11
    protected $data;
12
    protected $default = [];
13
    private $serializer = null;
14
15
    use DTOAccessorTrait;
16
    use DTOIteratorTrait;
17
18
    /**
19
     * DTO constructor.
20
     * @param array $default
21
     * @param array|object|string $data
22
     * @param DTOSerializerInterface $serializer
23
     * @throws \InvalidArgumentException
24
     */
25 40
    public function __construct($default = [], $data = [], DTOSerializerInterface $serializer = null)
26
    {
27 40
        if (count($default) > 0) {
28 38
            $this->default = $default;
29
        }
30
31 40
        $this->serializer = $serializer === null ? new JsonSerializer() : $serializer;
32
33 40
        (new DTOBaseBuilder($this))->build($data);
34 36
    }
35
36
    /**
37
     * Get value by key or nested structure
38
     * @param string $offset
39
     * @return mixed
40
     * @throws \InvalidArgumentException
41
     */
42 23
    public function get($offset)
43
    {
44 23
        if (strpos($offset, '.') === false) {
45 21
            return $this->offsetGetScalar($offset);
46
        } else {
47 5
            return $this->processChain($offset);
48
        }
49
    }
50
51
    /**
52
     * Converts data to string
53
     * @return string
54
     */
55 3
    public function __toString()
56
    {
57 3
        return $this->serialize();
58
    }
59
60
    /**
61
     * Converts data to string
62
     * @return string
63
     */
64 2
    public function toArray()
65
    {
66 2
        if ($this->serializer instanceof JsonSerializer) {
67 1
            return json_decode($this->serialize(), true);
68
        } else {
69 1
            return json_decode((new JsonSerializer())->serialize($this->data), true);
70
        }
71
    }
72
73
    /**
74
     * Serializes the data using serializer
75
     * @return string
76
     */
77 4
    private function serialize()
78
    {
79 4
        return $this->serializer->serialize($this->data);
80
    }
81
82
    /**
83
     * @return DTOSerializerInterface
84
     */
85 4
    public function getSerializer()
86
    {
87 4
        return $this->serializer;
88
    }
89
90
    /**
91
     * @param DTOSerializerInterface $serializer
92
     * @return DTOBase
93
     */
94 1
    public function setSerializer(DTOSerializerInterface $serializer)
95
    {
96 1
        $this->serializer = $serializer;
97
98 1
        return $this;
99
    }
100
101
    /**
102
     * @return array
103
     */
104 36
    public function getDefault()
105
    {
106 36
        return $this->default;
107
    }
108
109
    /**
110
     * Get nested values using "dot" notation
111
     * @param $offset
112
     * @return mixed
113
     * @throws \InvalidArgumentException
114
     */
115 5
    private function processChain($offset)
116
    {
117 5
        $keys = explode('.', $offset);
118 5
        $scope = $this->data;
119 5
        foreach ($keys as $key) {
120 5
            if ((is_array($scope) || $scope instanceof ArrayAccess) && isset($scope[$key])) {
121 3
                $scope = $scope[$key];
122 4
            } elseif (is_object($scope) && isset($scope->{$key})) {
123 2
                $scope = $scope->{$key};
124
            } else {
125 5
                throw new \InvalidArgumentException('Non existent offset given in offset chain: '.$key);
126
            }
127
        }
128
129 3
        return $scope;
130
    }
131
}