Completed
Push — master ( 1b3946...4711c5 )
by Viacheslav
12s queued 10s
created

Properties   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 80.77%

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 81
ccs 21
cts 26
cp 0.8077
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A lock() 0 4 1
A __set() 0 11 3
A getNestedProperties() 0 3 1
A addNested() 0 13 4
A isEmpty() 0 3 1
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 7
    public function lock()
27
    {
28 7
        $this->__isReadOnly = true;
29 7
        return $this;
30
    }
31
32
    /**
33
     * @param string $name
34
     * @param mixed $column
35
     * @return $this|static
36
     * @throws Exception
37
     */
38 15
    public function __set($name, $column)
39
    {
40 15
        if ($this->__isReadOnly) {
41
            throw new Exception('Trying to modify read-only Properties');
42
        }
43 15
        if ($column instanceof Nested) {
44 4
            $this->addNested($column->schema, $name);
45 4
            return $this;
46
        }
47 13
        parent::__set($name, $column);
48 13
        return $this;
49
    }
50
51 3
    public static function create()
52
    {
53 3
        return new static;
54
    }
55
56
    /** @var Schema|null */
57
    private $additionalProperties;
0 ignored issues
show
introduced by
The private property $additionalProperties is not used, and could be removed.
Loading history...
58
59
    /** @var Egg[][] */
60
    public $nestedProperties = array();
61
62
    /** @var string[] */
63
    public $nestedPropertyNames = array();
64
65
    /**
66
     * @param SchemaContract $nested
67
     * @param string $name
68
     * @return $this
69
     * @throws Exception
70
     */
71 4
    protected function addNested(SchemaContract $nested, $name)
72
    {
73 4
        if ($this->__isReadOnly) {
74
            throw new Exception('Trying to modify read-only Properties');
75
        }
76 4
        if (null === $nested->getProperties()) {
77
            throw new Exception('Schema with properties required', Exception::PROPERTIES_REQUIRED);
78
        }
79 4
        $this->nestedPropertyNames[$name] = $name;
80 4
        foreach ($nested->getProperties()->toArray() as $propertyName => $property) {
81 4
            $this->nestedProperties[$propertyName][] = new Egg($nested, $name, $property);
82
        }
83 4
        return $this;
84
    }
85
86
    /**
87
     * @return Egg[][]
88
     */
89
    public function getNestedProperties()
90
    {
91
        return $this->nestedProperties;
92
    }
93
94 7
    public function isEmpty()
95
    {
96 7
        return (count($this->__arrayOfData) + count($this->nestedProperties)) === 0;
97
    }
98
}