Passed
Push — master ( 2d7948...93347a )
by Viacheslav
03:31
created

ClassStructureTrait::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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 3249
    public static function schema()
19
    {
20 3249
        static $schemas = array();
21 3249
        $className = get_called_class();
22 3249
        $schemaWrapper = &$schemas[$className];
23
24 3249
        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 3249
        return $schemaWrapper;
41
    }
42
43
    /**
44
     * @return Properties|static
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 3232
    public static function import($data, Context $options = null)
59
    {
60 3232
        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
    public function jsonSerialize()
96
    {
97 97
        $result = new \stdClass();
98 97
        $schema = static::schema();
99 97
        foreach ($schema->getProperties()->getDataKeyMap() as $propertyName => $dataName) {
100 97
            $value = $this->$propertyName;
101 97
            if ((null !== $value) || array_key_exists($propertyName, $this->__arrayOfData)) {
102 97
                $result->$dataName = $value;
103
            }
104
        }
105 97
        foreach ($schema->getNestedPropertyNames() as $name) {
106
            /** @var ObjectItem $nested */
107 4
            $nested = $this->$name;
108 4
            if (null !== $nested) {
109 2
                foreach ((array)$nested->jsonSerialize() as $key => $value) {
110 4
                    $result->$key = $value;
111
                }
112
            }
113
        }
114
115 97
        return $result;
116
    }
117
118
    /**
119
     * @return static|NameMirror
120
     */
121 4
    public static function names()
122
    {
123 4
        static $nameflector = null;
124 4
        if (null === $nameflector) {
125 4
            $nameflector = new NameMirror();
126
        }
127 4
        return $nameflector;
128
    }
129
130 247
    public function __set($name, $column) // todo nested schemas
131
    {
132 247
        if ($this->__validateOnSet) {
133 5
            if ($property = static::schema()->getProperty($name)) {
134 4
                $property->out($column);
135
            }
136
        }
137 246
        $this->__arrayOfData[$name] = $column;
138 246
        return $this;
139
    }
140
141
    public static function className()
142
    {
143
        return get_called_class();
144
    }
145
146
    /**
147
     * @throws \Exception
148
     * @throws \Swaggest\JsonSchema\InvalidValue
149
     */
150 1
    public function validate()
151
    {
152 1
        static::schema()->out($this);
153
    }
154
}