Passed
Pull Request — master (#41)
by Viacheslav
03:02
created

ObjectItemTrait   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 83.93%

Importance

Changes 0
Metric Value
wmc 27
dl 0
loc 145
ccs 47
cts 56
cp 0.8393
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getPatternPropertyNames() 0 6 2
A addPatternPropertyName() 0 3 1
A getDocumentPath() 0 3 1
A getAdditionalPropertyNames() 0 3 1
A setDocumentPath() 0 4 1
A getNestedObject() 0 5 2
A addAdditionalPropertyName() 0 3 1
A setNestedProperty() 0 13 3
A jsonSerialize() 0 12 4
A setFromRef() 0 10 3
A getFromRefs() 0 3 1
B getFromRefPath() 0 12 5
A getFromRef() 0 3 2
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 986
    public function addAdditionalPropertyName($name)
45
    {
46 986
        $this->__additionalPropertyNames[] = $name;
47 986
    }
48
49
    /**
50
     * @return null|string[]
51
     */
52
    public function getAdditionalPropertyNames()
53
    {
54
        return $this->__additionalPropertyNames;
55
    }
56
57
    protected $__patternPropertyNames;
58
59 93
    public function addPatternPropertyName($pattern, $name)
60
    {
61 93
        $this->__patternPropertyNames[$pattern][] = $name;
62 93
    }
63
64
    /**
65
     * @param string $pattern
66
     * @return null|string[]
67
     */
68
    public function getPatternPropertyNames($pattern)
69
    {
70
        if (isset($this->__patternPropertyNames[$pattern])) {
71
            return $this->__patternPropertyNames[$pattern];
72
        } else {
73
            return null;
74
        }
75
    }
76
77 768
    public function jsonSerialize()
78
    {
79 768
        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...
80 1
            $result = $this->__arrayOfData;
81 1
            foreach ($this->__nestedObjects as $object) {
82 1
                foreach ($object->toArray() as $key => $value) {
83 1
                    $result[$key] = $value;
84
                }
85
            }
86 1
            return (object)$result;
87
        } else {
88 767
            return (object)$this->__arrayOfData;
89
        }
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getDocumentPath()
96
    {
97
        return $this->__documentPath;
98
    }
99
    
100 3138
    public function setDocumentPath($path)
101
    {
102 3138
        $this->__documentPath = $path;
103 3138
        return $this;
104
    }
105
106
    /**
107
     * @see ObjectItemContract::getFromRef
108
     * @deprecated use ObjectItemContract::getFromRefs
109
     * @see ObjectItemContract::getFromRefs
110
     * @todo remove
111
     * @return string
112
     */
113
    public function getFromRef()
114
    {
115
        return null === $this->__fromRef ? null : $this->__fromRef[0];
116
    }
117
118
    /**
119
     * @see ObjectItemContract::getFromRef
120
     * @return string
121
     */
122 74
    public function getFromRefs()
123
    {
124 74
        return $this->__fromRef;
125
    }
126
127
    /**
128
     * @see ObjectItemContract::setFromRef
129
     * @param string $ref
130
     * @return $this
131
     */
132 403
    public function setFromRef($ref)
133
    {
134 403
        if (null === $this->__fromRef) {
135 403
            $this->__fromRef = array($ref);
136
        } else {
137 52
            if (false !== $this->__fromRef[0]) {
138 52
                $this->__fromRef[] = $ref;
139
            }
140
        }
141 403
        return $this;
142
    }
143
144
    private $__refPath;
145 3201
    protected function getFromRefPath() {
146 3201
        if ($this->__refPath === null) {
147 3111
            $this->__refPath = '';
148 3111
            if ($this->__fromRef) {
149 374
                foreach ($this->__fromRef as $ref) {
150 374
                    if ($ref) {
151 374
                        $this->__refPath = '->$ref[' . strtr($ref, array('~' => '~1', ':' => '~2')) . ']' . $this->__refPath;
152
                    }
153
                }
154
            }
155
        }
156 3201
        return $this->__refPath;
157
    }
158
}