Completed
Push — master ( c762b0...684d44 )
by John
02:56
created

ObjectSchema::isComplexType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\ApiDescriptions package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\ApiDescriptions\Description\Schema;
9
10
use KleijnWeb\ApiDescriptions\Description\ComplexType;
11
12
/**
13
 * Represents standard JSON Schema but with support for complex types
14
 *
15
 * @author John Kleijn <[email protected]>
16
 */
17
class ObjectSchema extends Schema
18
{
19
    /**
20
     * @var \stdClass|null
21
     */
22
    protected $propertySchemas;
23
24
    /**
25
     * @var ComplexType
26
     */
27
    protected $complexType;
28
29
    /**
30
     * @var string|null
31
     */
32
    protected $xType;
33
34
    /**
35
     * @var string|null
36
     */
37
    protected $xRefId;
38
39
    /**
40
     * ObjectSchema constructor.
41
     *
42
     * @param \stdClass      $definition
43
     * @param null|\stdClass $propertySchemas
44
     * @param ComplexType    $complexType
45
     * @param null|string    $xType
46
     * @param null|string    $xRefId
47
     */
48
    public function __construct(
49
        \stdClass $definition,
50
        $propertySchemas = null,
51
        ComplexType $complexType = null,
52
        $xType = null,
53
        $xRefId = null
54
    ) {
55
        $definition->type = Schema::TYPE_OBJECT;
56
57
        parent::__construct($definition);
58
59
        $this->propertySchemas = $propertySchemas;
60
        $this->complexType     = $complexType;
61
        $this->xType           = $xType;
62
        $this->xRefId          = $xRefId;
63
    }
64
65
    /**
66
     * @return \stdClass
67
     */
68
    public function getPropertySchemas(): \stdClass
69
    {
70
        return $this->propertySchemas;
71
    }
72
73
    /**
74
     * @param string $name
75
     *
76
     * @return Schema
77
     */
78
    public function getPropertySchema(string $name): Schema
79
    {
80
        if (!isset($this->propertySchemas->$name)) {
81
            throw new \OutOfBoundsException("Property '$name' does not exist");
82
        }
83
84
        return $this->propertySchemas->$name;
85
    }
86
87
    /**
88
     * @param string $name
89
     *
90
     * @return bool
91
     */
92
    public function hasPropertySchema(string $name): bool
93
    {
94
        return isset($this->propertySchemas->$name);
95
    }
96
97
    /**
98
     * @param ComplexType $complexType
99
     *
100
     * @return Schema
101
     */
102
    public function setComplexType(ComplexType $complexType): Schema
103
    {
104
        if ($this->complexType && $this->complexType !== $complexType) {
105
            throw new \LogicException("Cannot change complex type of schema");
106
        }
107
        $this->complexType = $complexType;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param ComplexType $type
114
     *
115
     * @return bool
116
     */
117
    public function isComplexType(ComplexType $type): bool
118
    {
119
        return $type === $this->complexType;
120
    }
121
122
    /**
123
     * @return bool
124
     */
125
    public function hasComplexType(): bool
126
    {
127
        return null !== $this->complexType;
128
    }
129
130
    /**
131
     * @return ComplexType
132
     */
133
    public function getComplexType(): ComplexType
134
    {
135
        return $this->complexType;
136
    }
137
138
    /**
139
     * @return string|null
140
     */
141
    public function getXType()
142
    {
143
        return $this->xType;
144
    }
145
146
    /**
147
     * @return string|null
148
     */
149
    public function getXRefId()
150
    {
151
        return $this->xRefId;
152
    }
153
154
    /**
155
     * @param string|ComplexType $type
156
     *
157
     * @return bool
158
     */
159
    public function isType($type): bool
160
    {
161
        if ($type instanceof ComplexType) {
162
            return $this->isComplexType($type);
163
        }
164
165
        return $this->isPrimitive($type);
166
    }
167
}
168