AbstractModel::parseArray()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 14

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.7998
c 0
b 0
f 0
cc 4
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
 * @SuppressWarnings(PHPMD.NumberOfChildren)
18
 */
19
abstract class AbstractModel implements ModelInterface
20
{
21
    /**
22
     * @return array
23
     */
24 26
    public function toArray()
25
    {
26 26
        $properties = get_object_vars($this);
27
28 26
        return $this->parseArray($properties);
29
    }
30
31
    /**
32
     * @return string
33
     */
34 1
    public function toJson()
35
    {
36 1
        return json_encode($this->toArray());
37
    }
38
39 26
    private function parseArray(array $values)
40
    {
41 26
        foreach ($values as $key => $value) {
42 26
            if (is_array($value)) {
43 2
                $values[$key] = $this->parseArray($value);
44
            }
45
46 26
            if ($value instanceof ModelInterface) {
47 26
                $values[$key] = $value->toArray();
48
            }
49
        }
50
51 26
        return $values;
52
    }
53
}
54