Completed
Push — master ( 4711c5...aa438e )
by Viacheslav
11s
created

Properties::jsonSerialize()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.1426

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 10
cts 14
cp 0.7143
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 14
nc 8
nop 0
crap 8.1426
1
<?php
2
3
namespace Swaggest\JsonSchema\Constraint;
4
5
use Swaggest\JsonSchema\Exception;
6
use Swaggest\JsonSchema\Schema;
7
use Swaggest\JsonSchema\SchemaContract;
8
use Swaggest\JsonSchema\Structure\Egg;
9
use Swaggest\JsonSchema\Structure\Nested;
10
use Swaggest\JsonSchema\Structure\ObjectItem;
11
12
/**
13
 * @method SchemaContract __get($key)
14
 * @method Schema[] toArray()
15
 */
16
class Properties extends ObjectItem implements Constraint
17
{
18
    private $__isReadOnly = false;
19
20
    /** @var Schema[] */
21
    protected $__arrayOfData = array();
22
23
    /** @var Schema */
24
    protected $__schema;
25
26
    /**
27
     * Property to data mapping, example ["ref" => "$ref"]
28
     * @var array
29
     */
30
    public $__defaultMapping = array();
31
32 8
    public function lock()
33
    {
34 8
        $this->__isReadOnly = true;
35 8
        return $this;
36
    }
37
38
    /**
39
     * @param string $name
40
     * @param mixed $column
41
     * @return $this|static
42
     * @throws Exception
43
     */
44 16
    public function __set($name, $column)
45
    {
46 16
        if ($this->__isReadOnly) {
47
            throw new Exception('Trying to modify read-only Properties');
48
        }
49 16
        if ($column instanceof Nested) {
50 4
            $this->addNested($column->schema, $name);
51 4
            return $this;
52
        }
53 14
        parent::__set($name, $column);
54 14
        return $this;
55
    }
56
57 3
    public static function create()
58
    {
59 3
        return new static;
60
    }
61
62
    /** @var Egg[][] */
63
    public $nestedProperties = array();
64
65
    /** @var string[] */
66
    public $nestedPropertyNames = array();
67
68
    /**
69
     * @param SchemaContract $nested
70
     * @param string $name
71
     * @return $this
72
     * @throws Exception
73
     */
74 4
    protected function addNested(SchemaContract $nested, $name)
75
    {
76 4
        if ($this->__isReadOnly) {
77
            throw new Exception('Trying to modify read-only Properties');
78
        }
79 4
        if (null === $nested->getProperties()) {
80
            throw new Exception('Schema with properties required', Exception::PROPERTIES_REQUIRED);
81
        }
82 4
        $this->nestedPropertyNames[$name] = $name;
83 4
        foreach ($nested->getProperties()->toArray() as $propertyName => $property) {
84 4
            $this->nestedProperties[$propertyName][] = new Egg($nested, $name, $property);
85
        }
86 4
        return $this;
87
    }
88
89
    /**
90
     * @return Egg[][]
91
     */
92
    public function getNestedProperties()
93
    {
94
        return $this->nestedProperties;
95
    }
96
97 8
    public function isEmpty()
98
    {
99 8
        return (count($this->__arrayOfData) + count($this->nestedProperties)) === 0;
100
    }
101
102 3
    public function jsonSerialize()
103
    {
104 3
        $result = $this->__arrayOfData;
105 3
        if ($this->__nestedObjects) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->__nestedObjects of type Swaggest\JsonSchema\Structure\ObjectItem[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
106
            foreach ($this->__nestedObjects as $object) {
107
                foreach ($object->toArray() as $key => $value) {
108
                    $result[$key] = $value;
109
                }
110
            }
111
        }
112
113 3
        if (isset($this->__defaultMapping)) {
114 3
            $mappedResult = new \stdClass();
115 3
            foreach ($result as $key => $value) {
116 3
                if (isset($this->__defaultMapping[$key])) {
117 1
                    $mappedResult->{$this->__defaultMapping[$key]} = $value;
118
                } else {
119 3
                    $mappedResult->$key = $value;
120
                }
121
            }
122 3
            return $mappedResult;
123
        }
124
125
        return (object)$result;
126
    }
127
128
}