Completed
Pull Request — master (#85)
by
unknown
07:24
created

ClassStructureTrait   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 96.49%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 28
eloc 56
dl 0
loc 153
ccs 55
cts 57
cp 0.9649
rs 10
c 2
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A schema() 0 23 4
A properties() 0 3 1
A pick() 0 4 1
A import() 0 3 1
A export() 0 3 1
A create() 0 3 1
C jsonSerialize() 0 32 12
A __set() 0 9 3
A className() 0 3 1
A names() 0 7 2
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 3252
    public static function schema()
19
    {
20 3252
        static $schemas = array();
21 3252
        $className = get_called_class();
22 3252
        $schemaWrapper = &$schemas[$className];
23
24 3252
        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 3252
        return $schemaWrapper;
41
    }
42
43
    /**
44
     * @return Properties|static|null
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 3235
    public static function import($data, Context $options = null)
59
    {
60 3235
        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($includeNullValues = false)
96
    {
97 97
        $result = new \stdClass();
98 97
        $schema = static::schema();
99 97
        $classname = $schema->getObjectItemClass();
100 97
        $classReference = (class_exists($classname)) ? new $classname() : new \stdClass();
101 97
        $properties = $schema->getProperties();
102 57
        if (null !== $properties) {
103
            foreach ($properties->getDataKeyMap() as $propertyName => $dataName) {
104
                $value = $this->$propertyName;
105 97
                if (
106
                    (
107 4
                        null !== $value
108 4
                        ||
109 2
                        ($value === null && property_exists($classReference, $propertyName)) && $includeNullValues)
110 2
                    || array_key_exists($propertyName, $this->__arrayOfData)
111
                ) {
112
                    $result->$dataName = $value;
113
                }
114
            }
115 97
        }
116
        foreach ($schema->getNestedPropertyNames() as $name) {
117
            /** @var ObjectItem $nested */
118
            $nested = $this->$name;
119
            if (null !== $nested) {
120
                foreach ((array)$nested->jsonSerialize() as $key => $value) {
121 4
                    $result->$key = $value;
122
                }
123 4
            }
124 4
        }
125 4
126
        return $result;
127 4
    }
128
129
    /**
130 249
     * @return static|NameMirror
131
     */
132 249
    public static function names()
133 5
    {
134 4
        static $nameflector = null;
135
        if (null === $nameflector) {
136
            $nameflector = new NameMirror();
137 248
        }
138 248
        return $nameflector;
139
    }
140
141
    public function __set($name, $column) // todo nested schemas
142
    {
143
        if ($this->__validateOnSet) {
144
            if ($property = static::schema()->getProperty($name)) {
145
                $property->out($column);
146
            }
147
        }
148
        $this->__arrayOfData[$name] = $column;
149
        return $this;
150 1
    }
151
152 1
    public static function className()
153 1
    {
154
        return get_called_class();
155
    }
156
157
    /**
158
     * @throws \Exception
159
     * @throws \Swaggest\JsonSchema\InvalidValue
160
     */
161
    public function validate()
162
    {
163
        static::schema()->out($this);
164
    }
165
}
166