Passed
Pull Request — master (#20)
by Viacheslav
02:54
created

ClassStructureTrait   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 21
dl 0
loc 137
ccs 48
cts 52
cp 0.9231
rs 10
c 0
b 0
f 0

11 Methods

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