Completed
Pull Request — master (#13)
by Viacheslav
12:16 queued 02:17
created

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