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

ClassStructureTrait   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 91.07%

Importance

Changes 0
Metric Value
wmc 23
dl 0
loc 142
ccs 51
cts 56
cp 0.9107
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
B schema() 0 23 4
B jsonSerialize() 0 21 7
A __set() 0 9 3
A properties() 0 3 1
A className() 0 3 1
A pick() 0 4 1
A import() 0 3 1
A names() 0 7 2
A export() 0 3 1
A create() 0 3 1
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 3137
    public static function schema()
19
    {
20 3137
        static $schemas = array();
21 3137
        $className = get_called_class();
22 3137
        $schemaWrapper = &$schemas[$className];
23
24 3137
        if (null === $schemaWrapper) {
25 8
            $schema = new Schema();
26 8
            $properties = new Properties();
27 8
            $schema->properties = $properties;
28 8
            $schema->objectItemClass = $className;
29 8
            $schemaWrapper = new Wrapper($schema);
30 8
            static::setUpProperties($properties, $schema);
31 8
            if (null === $schema->getFromRef()) {
0 ignored issues
show
introduced by
The condition null === $schema->getFromRef() is always false.
Loading history...
32 6
                $schema->setFromRef('#/definitions/' . $className);
33
            }
34 8
            if ($properties->isEmpty()) {
35
                $schema->properties = null;
36
            }
37 8
            $properties->lock();
38
        }
39
40 3137
        return $schemaWrapper;
41
    }
42
43
    /**
44
     * @return Properties|static
45
     */
46 2
    public static function properties()
47
    {
48 2
        return static::schema()->getProperties();
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::schema()->getProperties() also could return the type Swaggest\JsonSchema\Schema[] which is incompatible with the documented return type Swaggest\JsonSchema\Cons...ure\ClassStructureTrait.
Loading history...
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 3122
    public static function import($data, Context $options = null)
59
    {
60 3122
        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 13
    public static function export($data, Context $options = null)
71
    {
72 13
        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 90
    public function jsonSerialize()
96
    {
97 90
        $result = new \stdClass();
98 90
        $schema = static::schema();
99 90
        foreach ($schema->getPropertyNames() as $name) {
100 90
            $value = $this->$name;
101 90
            if ((null !== $value) || array_key_exists($name, $this->__arrayOfData)) {
102 90
                $result->$name = $value;
103
            }
104
        }
105 90
        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 90
        return $result;
116
    }
117
118
    /**
119
     * @return static|NameMirror
120
     */
121 3
    public static function names()
122
    {
123 3
        static $nameflector = null;
124 3
        if (null === $nameflector) {
125 3
            $nameflector = new NameMirror();
126
        }
127 3
        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
    public function validate()
151
    {
152
        static::schema()->out($this);
153
    }
154
}