Completed
Push — master ( 691f40...2dee47 )
by Viacheslav
01:40
created

ClassStructure   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 22
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 129
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A schema() 0 16 2
A properties() 0 4 1
A import() 0 4 1
A export() 0 4 1
A pick() 0 8 2
A create() 0 4 1
C jsonSerialize() 0 22 7
A names() 0 8 2
A __set() 0 10 3
A className() 0 4 1
A validate() 0 4 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\NameMirror;
8
use Swaggest\JsonSchema\Schema;
9
10
abstract class ClassStructure extends ObjectItem implements ClassStructureContract
11
{
12
    /**
13
     * @return ClassSchema
14
     */
15
    public static function schema()
16
    {
17
        static $schemas = array();
18
        $className = get_called_class();
19
        $schema = &$schemas[$className];
20
21
        if (null === $schema) {
22
            $schema = new ClassSchema();
23
            $properties = new Properties();
24
            $schema->properties = $properties;
25
            $schema->objectItemClass = $className;
26
            static::setUpProperties($properties, $schema);
27
        }
28
29
        return $schema;
30
    }
31
32
    /**
33
     * @return Properties|static
34
     */
35
    public static function properties()
36
    {
37
        return static::schema()->properties;
38
    }
39
40
    /**
41
     * @param $data
42
     * @param \Swaggest\JsonSchema\Context $options
43
     * @return static
44
     */
45
    public static function import($data, Context $options = null)
46
    {
47
        return static::schema()->in($data, $options);
48
    }
49
50
    /**
51
     * @param $data
52
     * @param Context $options
53
     * @return mixed
54
     */
55
    public static function export($data, Context $options = null)
56
    {
57
        return static::schema()->out($data, $options);
58
    }
59
60
    /**
61
     * @param ObjectItem $objectItem
62
     * @return static
63
     */
64
    public static function pick(ObjectItem $objectItem)
65
    {
66
        $className = get_called_class();
67
        if (isset($objectItem->__nestedObjects[$className])) {
68
            return $objectItem->__nestedObjects[$className];
69
        }
70
        return null;
71
    }
72
73
    /**
74
     * @return static
75
     */
76
    public static function create()
77
    {
78
        return new static;
79
    }
80
81
    protected $__validateOnSet = true; // todo skip validation during import
82
83
    public function jsonSerialize()
84
    {
85
        $result = new \stdClass();
86
        $properties = static::schema()->properties;
87
        foreach ($properties->toArray() as $name => $schema) {
88
            $value = $this->$name;
89
            if ((null !== $value) || array_key_exists($name, $this->__arrayOfData)) {
90
                $result->$name = $value;
91
            }
92
        }
93
        foreach ($properties->nestedPropertyNames as $name) {
94
            /** @var ObjectItem $nested */
95
            $nested = $this->$name;
96
            if (null !== $nested) {
97
                foreach ((array)$nested->jsonSerialize() as $key => $value) {
98
                    $result->$key = $value;
99
                }
100
            }
101
        }
102
103
        return $result;
104
    }
105
106
    /**
107
     * @return static
108
     */
109
    public static function names()
110
    {
111
        static $nameflector = null;
112
        if (null === $nameflector) {
113
            $nameflector = new NameMirror();
114
        }
115
        return $nameflector;
116
    }
117
118
    public function __set($name, $column) // todo nested schemas
119
    {
120
        if ($this->__validateOnSet) {
121
            if ($property = static::schema()->properties[$name]) {
122
                $property->out($column);
123
            }
124
        }
125
        $this->__arrayOfData[$name] = $column;
126
        return $this;
127
    }
128
129
    public static function className()
130
    {
131
        return get_called_class();
132
    }
133
134
    public function validate()
135
    {
136
        static::schema()->out($this);
137
    }
138
}