Passed
Push — master ( 506f67...f6d64e )
by Zbigniew
03:41
created

AbstractModel::parseArray()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 5
nop 1
crap 4
1
<?php
2
3
/*
4
 * This file is part of the zibios/wrike-php-jmsserializer package.
5
 *
6
 * (c) Zbigniew Ślązak
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zibios\WrikePhpJmsserializer\Model;
13
14
/**
15
 * Abstract Model.
16
 */
17
abstract class AbstractModel implements ModelInterface
18
{
19
    /**
20
     * @return array
21
     */
22 26
    public function toArray()
23
    {
24 26
        $properties = get_object_vars($this);
25
26 26
        return $this->parseArray($properties);
27
    }
28
29
    /**
30
     * @return string
31
     */
32 1
    public function toJson()
33
    {
34 1
        return json_encode($this->toArray());
35
    }
36
37 26
    private function parseArray(array $values)
38
    {
39 26
        foreach ($values as $key => $value) {
40 26
            if (is_array($value)) {
41 2
                $values[$key] = $this->parseArray($value);
42
            }
43
44 26
            if ($value instanceof ModelInterface) {
45 26
                $values[$key] = $value->toArray();
46
            }
47
        }
48
49 26
        return $values;
50
    }
51
}
52