Completed
Pull Request — master (#75)
by
unknown
03:15
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 (null === $this->__additionalPropertyNames) {
55
            return [];
56
        }
57
        return array_keys($this->__additionalPropertyNames);
58
    }
59
60
    protected $__patternPropertyNames;
61
62 99
    public function addPatternPropertyName($pattern, $name)
63
    {
64 99
        $this->__patternPropertyNames[$pattern][$name] = true;
65 99
    }
66
67
    /**
68
     * @param string $pattern
69
     * @return null|string[]
70
     */
71
    public function getPatternPropertyNames($pattern)
72
    {
73
        if (isset($this->__patternPropertyNames[$pattern])) {
74
            return array_keys($this->__patternPropertyNames[$pattern]);
75
        } else {
76
            return null;
77
        }
78
    }
79
80 785
    public function jsonSerialize()
81
    {
82 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...
83 1
            $result = $this->__arrayOfData;
84 1
            foreach ($this->__nestedObjects as $object) {
85 1
                foreach ($object->toArray() as $key => $value) {
86 1
                    $result[$key] = $value;
87
                }
88
            }
89 1
            return (object)$result;
90
        } else {
91 784
            return (object)$this->__arrayOfData;
92
        }
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getDocumentPath()
99
    {
100
        return $this->__documentPath;
101
    }
102
    
103 3250
    public function setDocumentPath($path)
104
    {
105 3250
        $this->__documentPath = $path;
106 3250
        return $this;
107
    }
108
109
    /**
110
     * @see ObjectItemContract::getFromRef
111
     * @deprecated use ObjectItemContract::getFromRefs
112
     * @see ObjectItemContract::getFromRefs
113
     * @todo remove
114
     * @return string
115
     */
116
    public function getFromRef()
117
    {
118
        return null === $this->__fromRef ? null : $this->__fromRef[0];
119
    }
120
121
    /**
122
     * @see ObjectItemContract::getFromRef
123
     * @return string[]|null
124
     */
125 448
    public function getFromRefs()
126
    {
127 448
        return $this->__fromRef;
128
    }
129
130
    /**
131
     * @see ObjectItemContract::setFromRef
132
     * @param string $ref
133
     * @return $this
134
     */
135 422
    public function setFromRef($ref)
136
    {
137 422
        if (null === $this->__fromRef) {
138 422
            $this->__fromRef = array($ref);
139
        } else {
140 48
            if (false !== $this->__fromRef[0]) {
141 48
                $this->__fromRef[] = $ref;
142
            }
143
        }
144 422
        return $this;
145
    }
146
147
    private $__refPath;
148 3316
    protected function getFromRefPath() {
149 3316
        if ($this->__refPath === null) {
150 3225
            $this->__refPath = '';
151 3225
            if ($this->__fromRef) {
152 391
                foreach ($this->__fromRef as $ref) {
153 391
                    if ($ref) {
154 389
                        $this->__refPath = '->$ref[' . strtr($ref, array('~' => '~1', ':' => '~2')) . ']' . $this->__refPath;
155
                    }
156
                }
157
            }
158
        }
159 3316
        return $this->__refPath;
160
    }
161
}
162