Passed
Branch release/v2.0.0 (f3d8ba)
by Anatoly
03:59
created

AbstractObject   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 15
c 1
b 0
f 0
dl 0
loc 43
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFields() 0 13 3
A toArray() 0 17 6
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router\OpenApi;
13
14
/**
15
 * Import functions
16
 */
17
use function is_array;
18
19
/**
20
 * AbstractObject
21
 */
22
abstract class AbstractObject implements ObjectInterface
23
{
24
25
    /**
26
     * {@inheritDoc}
27
     */
28 1
    public function toArray() : array
29
    {
30 1
        $fields = $this->getFields();
31
32 1
        $result = [];
33 1
        foreach ($fields as $field => $value) {
34 1
            if (!is_array($value)) {
35 1
                $result[$field] = ($value instanceof ObjectInterface) ? $value->toArray() : $value;
36 1
                continue;
37
            }
38
39 1
            foreach ($value as $key => $item) {
40 1
                $result[$field][$key] = ($item instanceof ObjectInterface) ? $item->toArray() : $item;
41
            }
42
        }
43
44 1
        return $result;
45
    }
46
47
    /**
48
     * Gets only filled fields from the object
49
     *
50
     * @return array
51
     */
52 1
    private function getFields() : array
53
    {
54 1
        $result = [];
55 1
        foreach ($this as $field => $value) {
56
            // not set value...
57 1
            if (null === $value) {
58 1
                continue;
59
            }
60
61 1
            $result[$field] = $value;
62
        }
63
64 1
        return $result;
65
    }
66
}
67