Completed
Push — master ( dfe67f...98ffcb )
by Viacheslav
12s
created

ClassStructureTrait   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
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
B schema() 0 23 4
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 3139
    public static function schema()
19
    {
20 3139
        static $schemas = array();
21 3139
        $className = get_called_class();
22 3139
        $schemaWrapper = &$schemas[$className];
23
24 3139
        if (null === $schemaWrapper) {
25 9
            $schema = new Schema();
26 9
            $properties = new Properties();
27 9
            $schema->properties = $properties;
28 9
            $schema->objectItemClass = $className;
29 9
            $schemaWrapper = new Wrapper($schema);
30 9
            static::setUpProperties($properties, $schema);
31 9
            if (null === $schema->getFromRef()) {
0 ignored issues
show
introduced by
The condition null === $schema->getFromRef() is always false.
Loading history...
Deprecated Code introduced by
The function Swaggest\JsonSchema\Stru...Structure::getFromRef() has been deprecated: use ObjectItemContract::getFromRefs ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

31
            if (null === /** @scrutinizer ignore-deprecated */ $schema->getFromRef()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
32 7
                $schema->setFromRef('#/definitions/' . $className);
33
            }
34 9
            if ($properties->isEmpty()) {
35 1
                $schema->properties = null;
36
            }
37 9
            $properties->lock();
38
        }
39
40 3139
        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 3123
    public static function import($data, Context $options = null)
59
    {
60 3123
        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 15
    public static function export($data, Context $options = null)
71
    {
72 15
        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 92
    public function jsonSerialize()
96
    {
97 92
        $result = new \stdClass();
98 92
        $schema = static::schema();
99 92
        foreach ($schema->getPropertyNames() as $name) {
100 92
            $value = $this->$name;
101 92
            if ((null !== $value) || array_key_exists($name, $this->__arrayOfData)) {
102 92
                $result->$name = $value;
103
            }
104
        }
105 92
        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 92
        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
}