Passed
Branch release/v2.0.0 (680694)
by Anatoly
06:01 queued 02:13
created

AbstractAnnotation::toArray()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 9.2222
cc 6
nc 4
nop 0
crap 42
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\Annotation\OpenApi;
13
14
/**
15
 * Import functions
16
 */
17
use function is_array;
18
19
/**
20
 * AbstractAnnotation
21
 */
22
abstract class AbstractAnnotation implements AnnotationInterface
23
{
24
25
    /**
26
     * {@inheritDoc}
27
     */
28
    public function toArray() : array
29
    {
30
        $fields = $this->getFields();
31
32
        $result = [];
33
        foreach ($fields as $field => $value) {
34
            if (!is_array($value)) {
35
                $result[$field] = ($value instanceof AnnotationInterface) ? $value->toArray() : $value;
36
                continue;
37
            }
38
39
            foreach ($value as $key => $item) {
40
                $result[$field][$key] = ($item instanceof AnnotationInterface) ? $item->toArray() : $item;
41
            }
42
        }
43
44
        return $result;
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    private function getFields() : array
51
    {
52
        $result = [];
53
        foreach ($this as $field => $value) {
54
            if (isset($value)) {
55
                $result[$field] = $value;
56
            }
57
        }
58
59
        return $result;
60
    }
61
}
62