Completed
Pull Request — master (#13)
by Viacheslav
08:26
created

ClassStructureTrait::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
rs 10
c 0
b 0
f 0
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\Context;
7
use Swaggest\JsonSchema\NameMirror;
8
use Swaggest\JsonSchema\Schema;
9
10
trait ClassStructureTrait
11
{
12
    use ObjectItemTrait;
13
14
    /**
15
     * @return ClassSchema
16
     */
17
    public static function schema()
18
    {
19
        static $schemas = array();
20
        $className = get_called_class();
21
        $schema = &$schemas[$className];
22
23
        if (null === $schema) {
24
            $schema = new ClassSchema();
25
            $properties = new Properties();
26
            $schema->properties = $properties;
27
            $schema->objectItemClass = $className;
28
            static::setUpProperties($properties, $schema);
0 ignored issues
show
Bug introduced by
The method setUpProperties() does not exist on Swaggest\JsonSchema\Structure\ClassStructureTrait. Did you maybe mean properties()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
29
        }
30
31
        return $schema;
32
    }
33
34
    /**
35
     * @return Properties|static
36
     */
37
    public static function properties()
38
    {
39
        return static::schema()->properties;
40
    }
41
42
    /**
43
     * @param $data
44
     * @param \Swaggest\JsonSchema\Context $options
45
     * @return static
46
     */
47
    public static function import($data, Context $options = null)
48
    {
49
        return static::schema()->in($data, $options);
50
    }
51
52
    /**
53
     * @param $data
54
     * @param Context $options
55
     * @return mixed
56
     * @throws \Swaggest\JsonSchema\InvalidValue
57
     */
58
    public static function export($data, Context $options = null)
59
    {
60
        return static::schema()->out($data, $options);
61
    }
62
63
    /**
64
     * @param ObjectItemContract $objectItem
65
     * @return static
66
     */
67
    public static function pick(ObjectItemContract $objectItem)
68
    {
69
        $className = get_called_class();
70
        return $objectItem->getNestedObject($className);
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
}