Passed
Pull Request — master (#75)
by
unknown
03:05
created

ObjectItemTrait::getFromRef()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 1
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Swaggest\JsonSchema\Structure;
4
5
use Swaggest\JsonSchema\MagicMapTrait;
6
7
/**
8
 * Trait ObjectItemTrait
9
 * @package Swaggest\JsonSchema\Structure
10
 * @see ObjectItemContract
11
 */
12
trait ObjectItemTrait
13
{
14
    use MagicMapTrait;
15
16
    /** @var ObjectItem[] */
17
    protected $__nestedObjects;
18
    protected $__documentPath;
19
    protected $__fromRef;
20
21 2
    public function getNestedObject($className) {
22 2
        if (isset($this->__nestedObjects[$className])) {
23 2
            return $this->__nestedObjects[$className];
24
        }
25
        return null;
26
    }
27
28 5
    public function setNestedProperty($propertyName, $value, Egg $nestedEgg)
29
    {
30 5
        $nestedName = $nestedEgg->name;
31 5
        $nested = &$this->__nestedObjects[$nestedName];
32 5
        if (null === $nested) {
33 5
            $nested = $nestedEgg->classSchema->makeObjectItem();
34 5
            $this->__nestedObjects[$nestedName] = $nested;
35 5
            if ($nestedName !== $nestedEgg->classSchema->getObjectItemClass()) {
36 3
                $this->$nestedName = $nested;
37
            }
38
        }
39 5
        $nested->$propertyName = $value;
40 5
        $this->__arrayOfData[$propertyName] = &$nested->$propertyName;
41 5
    }
42
43
    protected $__additionalPropertyNames;
44 1012
    public function addAdditionalPropertyName($name)
45
    {
46 1012
        $this->__additionalPropertyNames[$name] = true;
47 1012
    }
48
49
    /**
50
     * @return null|string[]
51
     */
52
    public function getAdditionalPropertyNames()
53
    {
54
        if(is_null($this->__additionalPropertyNames))
55
            return null;
56
        return array_keys($this->__additionalPropertyNames);
57
    }
58
59
    protected $__patternPropertyNames;
60
61 99
    public function addPatternPropertyName($pattern, $name)
62
    {
63 99
        $this->__patternPropertyNames[$pattern][$name] = true;
64 99
    }
65
66
    /**
67
     * @param string $pattern
68
     * @return null|string[]
69
     */
70
    public function getPatternPropertyNames($pattern)
71
    {
72
        if (isset($this->__patternPropertyNames[$pattern])) {
73
            return array_keys($this->__patternPropertyNames[$pattern]);
74
        } else {
75
            return null;
76
        }
77
    }
78
79 785
    public function jsonSerialize()
80
    {
81 785
        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...
82 1
            $result = $this->__arrayOfData;
83 1
            foreach ($this->__nestedObjects as $object) {
84 1
                foreach ($object->toArray() as $key => $value) {
85 1
                    $result[$key] = $value;
86
                }
87
            }
88 1
            return (object)$result;
89
        } else {
90 784
            return (object)$this->__arrayOfData;
91
        }
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getDocumentPath()
98
    {
99
        return $this->__documentPath;
100
    }
101
    
102 3250
    public function setDocumentPath($path)
103
    {
104 3250
        $this->__documentPath = $path;
105 3250
        return $this;
106
    }
107
108
    /**
109
     * @see ObjectItemContract::getFromRef
110
     * @deprecated use ObjectItemContract::getFromRefs
111
     * @see ObjectItemContract::getFromRefs
112
     * @todo remove
113
     * @return string
114
     */
115
    public function getFromRef()
116
    {
117
        return null === $this->__fromRef ? null : $this->__fromRef[0];
118
    }
119
120
    /**
121
     * @see ObjectItemContract::getFromRef
122
     * @return string[]|null
123
     */
124 448
    public function getFromRefs()
125
    {
126 448
        return $this->__fromRef;
127
    }
128
129
    /**
130
     * @see ObjectItemContract::setFromRef
131
     * @param string $ref
132
     * @return $this
133
     */
134 422
    public function setFromRef($ref)
135
    {
136 422
        if (null === $this->__fromRef) {
137 422
            $this->__fromRef = array($ref);
138
        } else {
139 48
            if (false !== $this->__fromRef[0]) {
140 48
                $this->__fromRef[] = $ref;
141
            }
142
        }
143 422
        return $this;
144
    }
145
146
    private $__refPath;
147 3316
    protected function getFromRefPath() {
148 3316
        if ($this->__refPath === null) {
149 3225
            $this->__refPath = '';
150 3225
            if ($this->__fromRef) {
151 391
                foreach ($this->__fromRef as $ref) {
152 391
                    if ($ref) {
153 389
                        $this->__refPath = '->$ref[' . strtr($ref, array('~' => '~1', ':' => '~2')) . ']' . $this->__refPath;
154
                    }
155
                }
156
            }
157
        }
158 3316
        return $this->__refPath;
159
    }
160
}