Completed
Pull Request — master (#85)
by
unknown
13:42 queued 03:47
created

ClassStructureTrait::pick()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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
    /**
96
     * @param bool $exportNullValues
97 97
     * @return \stdClass
98 97
     */
99 97
    public function jsonSerialize($exportNullValues = false)
100 97
    {
101 97
        $result = new \stdClass();
102 57
        $schema = static::schema();
103
        $classname = $schema->getObjectItemClass();
104
        $classReference = (class_exists($classname)) ? new $classname() : new \stdClass();
105 97
        $properties = $schema->getProperties();
106
        if (null !== $properties) {
107 4
            foreach ($properties->getDataKeyMap() as $propertyName => $dataName) {
108 4
                $value = $this->$propertyName;
109 2
                if (
110 2
                    (
111
                        null !== $value
112
                        ||
113
                        ($value === null && property_exists($classReference, $propertyName)) && $exportNullValues)
114
                    || array_key_exists($propertyName, $this->__arrayOfData)
115 97
                ) {
116
                    $result->$dataName = $value;
117
                }
118
            }
119
        }
120
        foreach ($schema->getNestedPropertyNames() as $name) {
121 4
            /** @var ObjectItem $nested */
122
            $nested = $this->$name;
123 4
            if (null !== $nested) {
124 4
                foreach ((array)$nested->jsonSerialize() as $key => $value) {
125 4
                    $result->$key = $value;
126
                }
127 4
            }
128
        }
129
130 249
        return $result;
131
    }
132 249
133 5
    /**
134 4
     * @return static|NameMirror
135
     */
136
    public static function names()
137 248
    {
138 248
        static $nameflector = null;
139
        if (null === $nameflector) {
140
            $nameflector = new NameMirror();
141
        }
142
        return $nameflector;
143
    }
144
145
    public function __set($name, $column) // todo nested schemas
146
    {
147
        if ($this->__validateOnSet) {
148
            if ($property = static::schema()->getProperty($name)) {
149
                $property->out($column);
150 1
            }
151
        }
152 1
        $this->__arrayOfData[$name] = $column;
153 1
        return $this;
154
    }
155
156
    public static function className()
157
    {
158
        return get_called_class();
159
    }
160
161
    /**
162
     * @throws \Exception
163
     * @throws \Swaggest\JsonSchema\InvalidValue
164
     */
165
    public function validate()
166
    {
167
        static::schema()->out($this);
168
    }
169
}
170