Completed
Push — master ( 1b3946...4711c5 )
by Viacheslav
12s queued 10s
created

ClassStructureTrait   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 22
dl 0
loc 140
ccs 50
cts 55
cp 0.9091
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A schema() 0 21 3
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 3135
    public static function schema()
19
    {
20 3135
        static $schemas = array();
21 3135
        $className = get_called_class();
22 3135
        $schemaWrapper = &$schemas[$className];
23
24 3135
        if (null === $schemaWrapper) {
25 7
            $schema = new Schema();
26 7
            $properties = new Properties();
27 7
            $schema->properties = $properties;
28 7
            $schema->objectItemClass = $className;
29 7
            $schemaWrapper = new Wrapper($schema);
30 7
            static::setUpProperties($properties, $schema);
31 7
            $schema->setFromRef('#/definitions/' . $className);
32 7
            if ($properties->isEmpty()) {
33
                $schema->properties = null;
34
            }
35 7
            $properties->lock();
36
        }
37
38 3135
        return $schemaWrapper;
39
    }
40
41
    /**
42
     * @return Properties|static
43
     */
44 2
    public static function properties()
45
    {
46 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...
47
    }
48
49
    /**
50
     * @param mixed $data
51
     * @param Context $options
52
     * @return static|mixed
53
     * @throws \Swaggest\JsonSchema\Exception
54
     * @throws \Swaggest\JsonSchema\InvalidValue
55
     */
56 3121
    public static function import($data, Context $options = null)
57
    {
58 3121
        return static::schema()->in($data, $options);
59
    }
60
61
    /**
62
     * @param mixed $data
63
     * @param Context $options
64
     * @return mixed
65
     * @throws \Swaggest\JsonSchema\InvalidValue
66
     * @throws \Exception
67
     */
68 12
    public static function export($data, Context $options = null)
69
    {
70 12
        return static::schema()->out($data, $options);
71
    }
72
73
    /**
74
     * @param ObjectItemContract $objectItem
75
     * @return static
76
     */
77 2
    public static function pick(ObjectItemContract $objectItem)
78
    {
79 2
        $className = get_called_class();
80 2
        return $objectItem->getNestedObject($className);
81
    }
82
83
    /**
84
     * @return static
85
     */
86 6
    public static function create()
87
    {
88 6
        return new static;
89
    }
90
91
    protected $__validateOnSet = true; // todo skip validation during import
92
93 89
    public function jsonSerialize()
94
    {
95 89
        $result = new \stdClass();
96 89
        $schema = static::schema();
97 89
        foreach ($schema->getPropertyNames() as $name) {
98 89
            $value = $this->$name;
99 89
            if ((null !== $value) || array_key_exists($name, $this->__arrayOfData)) {
100 89
                $result->$name = $value;
101
            }
102
        }
103 89
        foreach ($schema->getNestedPropertyNames() as $name) {
104
            /** @var ObjectItem $nested */
105 4
            $nested = $this->$name;
106 4
            if (null !== $nested) {
107 2
                foreach ((array)$nested->jsonSerialize() as $key => $value) {
108 4
                    $result->$key = $value;
109
                }
110
            }
111
        }
112
113 89
        return $result;
114
    }
115
116
    /**
117
     * @return static|NameMirror
118
     */
119 2
    public static function names()
120
    {
121 2
        static $nameflector = null;
122 2
        if (null === $nameflector) {
123 2
            $nameflector = new NameMirror();
124
        }
125 2
        return $nameflector;
126
    }
127
128 246
    public function __set($name, $column) // todo nested schemas
129
    {
130 246
        if ($this->__validateOnSet) {
131 5
            if ($property = static::schema()->getProperty($name)) {
132 4
                $property->out($column);
133
            }
134
        }
135 245
        $this->__arrayOfData[$name] = $column;
136 245
        return $this;
137
    }
138
139
    public static function className()
140
    {
141
        return get_called_class();
142
    }
143
144
    /**
145
     * @throws \Exception
146
     * @throws \Swaggest\JsonSchema\InvalidValue
147
     */
148
    public function validate()
149
    {
150
        static::schema()->out($this);
151
    }
152
}