Completed
Pull Request — master (#1)
by Viacheslav
06:08
created

ClassStructure::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Swaggest\JsonSchema\Structure;
4
5
use Swaggest\JsonSchema\Constraint\Properties;
6
use Swaggest\JsonSchema\Constraint\Type;
7
use Swaggest\JsonSchema\NameMirror;
8
9
abstract class ClassStructure extends ObjectItem implements ClassStructureContract
10
{
11
    /**
12
     * @return ClassSchema
13
     */
14
    public static function schema()
15
    {
16
        static $schemas = array();
17
        $className = get_called_class();
18
        $schema = &$schemas[$className];
19
20
        if (null === $schema) {
21
            $schema = new ClassSchema();
22
            $schema->type = new Type(Type::OBJECT);
23
            $properties = new Properties();
24
            $schema->properties = $properties;
25
            $schema->objectItemClass = get_called_class();
26
            static::setUpProperties($properties, $schema);
27
        }
28
29
        return $schema;
30
    }
31
32
    /**
33
     * @param $data
34
     * @return static
35
     * @throws \Swaggest\JsonSchema\InvalidValue
36
     */
37
    public static function import($data)
38
    {
39
        return static::schema()->import($data);
40
    }
41
42
    /**
43
     * @param $data
44
     * @return mixed
45
     * @throws \Swaggest\JsonSchema\InvalidValue
46
     */
47
    public static function export($data)
48
    {
49
        return static::schema()->export($data);
50
    }
51
52
    /**
53
     * @param ObjectItem $objectItem
54
     * @return static
55
     */
56
    public static function pick(ObjectItem $objectItem)
57
    {
58
        $className = get_called_class();
59
        if (isset($objectItem->__nestedObjects[$className])) {
60
            return $objectItem->__nestedObjects[$className];
61
        }
62
        return null;
63
    }
64
65
    /**
66
     * @return static
67
     */
68
    public static function create()
69
    {
70
        return new static;
71
    }
72
73
    protected $__validateOnSet = true; // todo skip validation during import
74
75
    public function jsonSerialize()
76
    {
77
        $result = new \stdClass();
78
        $properties = static::schema()->properties;
79
        foreach ($properties->toArray() as $name => $schema) {
80
            $value = $this->$name;
81
            if ((null !== $value) || array_key_exists($name, $this->__arrayOfData)) {
82
                $result->$name = $value;
83
            }
84
        }
85
        foreach ($properties->nestedPropertyNames as $name) {
86
            /** @var ObjectItem $nested */
87
            $nested = $this->$name;
88
            foreach ((array)$nested->jsonSerialize() as $key => $value) {
89
                $result->$key = $value;
90
            }
91
        }
92
93
        return $result;
94
    }
95
96
    /**
97
     * @return static
98
     */
99
    public static function names()
100
    {
101
        static $nameflector = null;
102
        if (null === $nameflector) {
103
            $nameflector = new NameMirror();
104
        }
105
        return $nameflector;
106
    }
107
108
    public function __set($name, $column) // todo nested schemas
109
    {
110
        if ($this->__validateOnSet) {
111
            if ($property = static::schema()->properties[$name]) {
112
                $property->export($column);
113
            }
114
        }
115
        $this->__arrayOfData[$name] = $column;
116
        return $this;
117
    }
118
}