Completed
Pull Request — master (#1)
by Viacheslav
06:08
created

ObjectItem::setNestedProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 3
1
<?php
2
3
namespace Swaggest\JsonSchema\Structure;
4
5
6
use Swaggest\JsonSchema\MagicMap;
7
use Swaggest\JsonSchema\Schema;
8
9
class ObjectItem extends MagicMap
10
{
11
    /** @var ObjectItem[] */
12
    protected $__nestedObjects;
13
14
    public function setNestedProperty($propertyName, $value, Egg $nestedEgg)
15
    {
16
        $nestedName = $nestedEgg->name;
17
        $nested = &$this->__nestedObjects[$nestedName];
18
        if (null === $nested) {
19
            $nested = $nestedEgg->classSchema->makeObjectItem();
20
            $this->__nestedObjects[$nestedName] = $nested;
21
            if ($nestedName !== $nestedEgg->classSchema->objectItemClass) {
22
                $this->$nestedName = $nested;
23
            }
24
        }
25
        $nested->$propertyName = $value;
26
        $this->__arrayOfData[$propertyName] = &$nested->$propertyName;
27
    }
28
29
    protected $__additionalPropertyNames;
30
    public function addAdditionalPropertyName($name)
31
    {
32
        $this->__additionalPropertyNames[] = $name;
33
    }
34
35
    /**
36
     * @return null|string[]
37
     */
38
    public function getAdditionalPropertyNames()
39
    {
40
        return $this->__additionalPropertyNames;
41
    }
42
43
    protected $__patternPropertyNames;
44
45
    public function addPatternPropertyName($pattern, $name)
46
    {
47
        $this->__additionalPropertyNames[$pattern][] = $name;
48
    }
49
50
    /**
51
     * @param $pattern
52
     * @return null|string[]
53
     */
54
    public function getPatternPropertyNames($pattern)
55
    {
56
        if (isset($this->__patternPropertyNames[$pattern])) {
57
            return $this->__patternPropertyNames[$pattern];
58
        } else {
59
            return null;
60
        }
61
    }
62
63
    public function jsonSerialize()
64
    {
65
        if ($this->__nestedObjects) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->__nestedObjects of type Swaggest\JsonSchema\Structure\ObjectItem[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
66
            $result = $this->__arrayOfData;
67
            foreach ($this->__nestedObjects as $object) {
68
                foreach ($object->__arrayOfData as $key => $value) {
69
                    $result[$key] = $value;
70
                }
71
            }
72
            return (object)$result;
73
        } else {
74
            return (object)$this->__arrayOfData;
75
        }
76
    }
77
78
79
}