Completed
Push — master ( aa438e...dfe67f )
by Viacheslav
11s
created

ObjectItemTrait::getFromRefPath()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 3
nop 0
crap 5
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 985
    public function addAdditionalPropertyName($name)
45
    {
46 985
        $this->__additionalPropertyNames[] = $name;
47 985
    }
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 767
    public function jsonSerialize()
78
    {
79 767
        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 766
            return (object)$this->__arrayOfData;
89
        }
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getDocumentPath()
96
    {
97
        return $this->__documentPath;
98
    }
99
    
100 3137
    public function setDocumentPath($path)
101
    {
102 3137
        $this->__documentPath = $path;
103 3137
        return $this;
104
    }
105
106
    /**
107
     * @return string
108
     */
109 75
    public function getFromRef()
110
    {
111 75
        return null === $this->__fromRef ? null : $this->__fromRef[0];
112
    }
113
114 401
    public function setFromRef($ref)
115
    {
116 401
        if (null === $this->__fromRef) {
117 401
            $this->__fromRef = array($ref);
118
        } else {
119 50
            if (false !== $this->__fromRef[0]) {
120 50
                $this->__fromRef[] = $ref;
121
            }
122
        }
123 401
        return $this;
124
    }
125
126
    private $__refPath;
127 3199
    protected function getFromRefPath() {
128 3199
        if ($this->__refPath === null) {
129 3110
            $this->__refPath = '';
130 3110
            if ($this->__fromRef) {
131 374
                foreach ($this->__fromRef as $ref) {
132 374
                    if ($ref) {
133 374
                        $this->__refPath = '->$ref[' . strtr($ref, array('~' => '~1', ':' => '~2')) . ']' . $this->__refPath;
134
                    }
135
                }
136
            }
137
        }
138 3199
        return $this->__refPath;
139
    }
140
}