Completed
Push — master ( bca24d...1b0d8b )
by John
03:18
created

Document::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\PhpApi\Descriptions\Description\Document;
9
10
/**
11
 * @author John Kleijn <[email protected]>
12
 */
13
class Document implements \JsonSerializable, \IteratorAggregate
14
{
15
    /**
16
     * @var \stdClass
17
     */
18
    private $definition;
19
20
    /**
21
     * @var string
22
     */
23
    private $uri;
24
25
    /**
26
     * Document constructor.
27
     *
28
     * @param string    $uri
29
     * @param \stdClass $definition
30
     */
31
    public function __construct(string $uri, \stdClass $definition)
32
    {
33
        $this->definition = $definition;
34
        $this->uri        = $uri;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getUri(): string
41
    {
42
        return $this->uri;
43
    }
44
45
    /**
46
     * @param callable $f
47
     *
48
     * @return void
49
     */
50
    public function apply(callable  $f)
51
    {
52
        $recurse = function (&$item, $parent = null, $parentAttribute = null) use ($f, &$recurse) {
53
54
            foreach ($item as $attribute => &$value) {
55
                if (false === $f($value, $attribute, $parent, $parentAttribute)) {
56
                    return false;
57
                }
58
                if ($value === null) {
59
                    return true;
60
                }
61
                if (!is_scalar($value)) {
62
                    if (false === $recurse($value, $item, $attribute)) {
63
                        return false;
64
                    }
65
                }
66
            }
67
68
            return true;
69
        };
70
        $recurse($this->definition);
71
    }
72
73
    /**
74
     * @return \stdClass
75
     */
76
    public function getDefinition(): \stdClass
77
    {
78
        return $this->definition;
79
    }
80
81
    /**
82
     * @param string $attribute
83
     *
84
     * @return mixed
85
     */
86
    public function __get(string $attribute)
87
    {
88
        return isset($this->definition->$attribute) ? $this->definition->$attribute : null;
89
    }
90
91
    /**
92
     * @param string $attribute
93
     *
94
     * @return bool
95
     */
96
    public function __isset(string $attribute)
97
    {
98
        return isset($this->definition->$attribute);
99
    }
100
101
    /**
102
     * @return \stdClass
103
     */
104
    public function jsonSerialize()
105
    {
106
        return $this->definition;
107
    }
108
109
    /**
110
     * @return \ArrayIterator
111
     */
112
    public function getIterator(): \ArrayIterator
113
    {
114
        return new \ArrayIterator((object)$this->definition);
115
    }
116
}
117