ClassStructureTrait   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Test Coverage

Coverage 96.49%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 31
eloc 62
c 3
b 0
f 0
dl 0
loc 170
ccs 55
cts 57
cp 0.9649
rs 9.92

11 Methods

Rating   Name   Duplication   Size   Complexity  
A schema() 0 23 4
A properties() 0 3 1
A pick() 0 4 1
A import() 0 3 1
A export() 0 3 1
A create() 0 3 1
C jsonSerialize() 0 46 15
A __set() 0 9 3
A className() 0 3 1
A names() 0 7 2
A validate() 0 3 1
1
<?php
2
3
namespace Swaggest\JsonSchema\Structure;
4
5
use Swaggest\JsonSchema\Constraint\Properties;
6
use Swaggest\JsonSchema\Context;
7
use Swaggest\JsonSchema\Schema;
8
use Swaggest\JsonSchema\Wrapper;
9
use Swaggest\JsonSchema\NameMirror;
10
11
trait ClassStructureTrait
12
{
13
    use ObjectItemTrait;
14
15
    /**
16
     * @return Wrapper
17
     */
18 3252
    public static function schema()
19
    {
20 3252
        static $schemas = array();
21 3252
        $className = get_called_class();
22 3252
        $schemaWrapper = &$schemas[$className];
23
24 3252
        if (null === $schemaWrapper) {
25 10
            $schema = new Schema();
26 10
            $properties = new Properties();
27 10
            $schema->properties = $properties;
28 10
            $schema->objectItemClass = $className;
29 10
            $schemaWrapper = new Wrapper($schema);
30 10
            static::setUpProperties($properties, $schema);
31 10
            if (null === $schema->getFromRefs()) {
32 8
                $schema->setFromRef('#/definitions/' . $className);
33
            }
34 10
            if ($properties->isEmpty()) {
35 1
                $schema->properties = null;
36
            }
37 10
            $properties->lock();
38
        }
39
40 3252
        return $schemaWrapper;
41
    }
42
43
    /**
44
     * @return Properties|static|null
45
     */
46 2
    public static function properties()
47
    {
48 2
        return static::schema()->getProperties();
49
    }
50
51
    /**
52
     * @param mixed $data
53
     * @param Context $options
54
     * @return static|mixed
55
     * @throws \Swaggest\JsonSchema\Exception
56
     * @throws \Swaggest\JsonSchema\InvalidValue
57
     */
58 3235
    public static function import($data, Context $options = null)
59
    {
60 3235
        return static::schema()->in($data, $options);
61
    }
62
63
    /**
64
     * @param mixed $data
65
     * @param Context $options
66
     * @return mixed
67
     * @throws \Swaggest\JsonSchema\InvalidValue
68
     * @throws \Exception
69
     */
70 18
    public static function export($data, Context $options = null)
71
    {
72 18
        return static::schema()->out($data, $options);
73
    }
74
75
    /**
76
     * @param ObjectItemContract $objectItem
77
     * @return static
78
     */
79 2
    public static function pick(ObjectItemContract $objectItem)
80
    {
81 2
        $className = get_called_class();
82 2
        return $objectItem->getNestedObject($className);
83
    }
84
85
    /**
86
     * @return static
87
     */
88 6
    public static function create()
89
    {
90 6
        return new static;
91
    }
92
93
    protected $__validateOnSet = true; // todo skip validation during import
94
95 97
    /**
96
     * @return \stdClass
97 97
     */
98 97
    public function jsonSerialize()
99 97
    {
100 97
        $result = new \stdClass();
101 97
        $schema = static::schema();
102 57
        $properties = $schema->getProperties();
103
        $processed = array();
104
        if (null !== $properties) {
105 97
            foreach ($properties->getDataKeyMap() as $propertyName => $dataName) {
106
                $value = $this->$propertyName;
107 4
108 4
                // Value is exported if exists.
109 2
                if (null !== $value || array_key_exists($propertyName, $this->__arrayOfData)) {
110 2
                    $result->$dataName = $value;
111
                    $processed[$propertyName] = true;
112
                    continue;
113
                }
114
115 97
                // Non-existent value is only exported if belongs to nullable property (having 'null' in type array).
116
                $property = $schema->getProperty($propertyName);
117
                if ($property instanceof Schema) {
118
                    $types = $property->type;
119
                    if ($types === Schema::NULL || (is_array($types) && in_array(Schema::NULL, $types))) {
120
                        $result->$dataName = $value;
121 4
                    }
122
                }
123 4
            }
124 4
        }
125 4
        foreach ($schema->getNestedPropertyNames() as $name) {
126
            /** @var ObjectItem $nested */
127 4
            $nested = $this->$name;
128
            if (null !== $nested) {
129
                foreach ((array)$nested->jsonSerialize() as $key => $value) {
130 249
                    $result->$key = $value;
131
                }
132 249
            }
133 5
        }
134 4
135
        if (!empty($this->__arrayOfData)) {
136
            foreach ($this->__arrayOfData as $name => $value) {
137 248
                if (!isset($processed[$name])) {
138 248
                    $result->$name = $this->{$name};
139
                }
140
            }
141
        }
142
143
        return $result;
144
    }
145
146
    /**
147
     * @return static|NameMirror
148
     */
149
    public static function names()
150 1
    {
151
        static $nameflector = null;
152 1
        if (null === $nameflector) {
153 1
            $nameflector = new NameMirror();
154
        }
155
        return $nameflector;
156
    }
157
158
    public function __set($name, $column) // todo nested schemas
159
    {
160
        if ($this->__validateOnSet) {
161
            if ($property = static::schema()->getProperty($name)) {
162
                $property->out($column);
163
            }
164
        }
165
        $this->__arrayOfData[$name] = $column;
166
        return $this;
167
    }
168
169
    public static function className()
170
    {
171
        return get_called_class();
172
    }
173
174
    /**
175
     * @throws \Exception
176
     * @throws \Swaggest\JsonSchema\InvalidValue
177
     */
178
    public function validate()
179
    {
180
        static::schema()->out($this);
181
    }
182
}
183