Completed
Push — master ( 634fb7...2d8b8d )
by Viacheslav
9s
created

ClassStructure   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 114
c 1
b 0
f 0
wmc 19
lcom 1
cbo 5
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A schema() 0 17 2
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
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\DataPreProcessor;
8
use Swaggest\JsonSchema\NameMirror;
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
            $schema->type = new Type(Type::OBJECT);
24
            $properties = new Properties();
25
            $schema->properties = $properties;
26
            $schema->objectItemClass = get_called_class();
27
            static::setUpProperties($properties, $schema);
28
        }
29
30
        return $schema;
31
    }
32
33
    /**
34
     * @param $data
35
     * @param DataPreProcessor $preProcessor
36
     * @return static
37
     * @throws \Swaggest\JsonSchema\InvalidValue
38
     */
39
    public static function import($data, DataPreProcessor $preProcessor = null)
40
    {
41
        return static::schema()->import($data, $preProcessor);
42
    }
43
44
    /**
45
     * @param $data
46
     * @param DataPreProcessor $preProcessor
47
     * @return mixed
48
     * @throws \Swaggest\JsonSchema\InvalidValue
49
     */
50
    public static function export($data, DataPreProcessor $preProcessor = null)
51
    {
52
        return static::schema()->export($data, $preProcessor);
53
    }
54
55
    /**
56
     * @param ObjectItem $objectItem
57
     * @return static
58
     */
59
    public static function pick(ObjectItem $objectItem)
60
    {
61
        $className = get_called_class();
62
        if (isset($objectItem->__nestedObjects[$className])) {
63
            return $objectItem->__nestedObjects[$className];
64
        }
65
        return null;
66
    }
67
68
    /**
69
     * @return static
70
     */
71
    public static function create()
72
    {
73
        return new static;
74
    }
75
76
    protected $__validateOnSet = true; // todo skip validation during import
77
78
    public function jsonSerialize()
79
    {
80
        $result = new \stdClass();
81
        $properties = static::schema()->properties;
82
        foreach ($properties->toArray() as $name => $schema) {
83
            $value = $this->$name;
84
            if ((null !== $value) || array_key_exists($name, $this->__arrayOfData)) {
85
                $result->$name = $value;
86
            }
87
        }
88
        foreach ($properties->nestedPropertyNames as $name) {
89
            /** @var ObjectItem $nested */
90
            $nested = $this->$name;
91
            if (null !== $nested) {
92
                foreach ((array)$nested->jsonSerialize() as $key => $value) {
93
                    $result->$key = $value;
94
                }
95
            }
96
        }
97
98
        return $result;
99
    }
100
101
    /**
102
     * @return static
103
     */
104
    public static function names()
105
    {
106
        static $nameflector = null;
107
        if (null === $nameflector) {
108
            $nameflector = new NameMirror();
109
        }
110
        return $nameflector;
111
    }
112
113
    public function __set($name, $column) // todo nested schemas
114
    {
115
        if ($this->__validateOnSet) {
116
            if ($property = static::schema()->properties[$name]) {
117
                $property->export($column);
118
            }
119
        }
120
        $this->__arrayOfData[$name] = $column;
121
        return $this;
122
    }
123
}