Passed
Branch master (bd1396)
by Viacheslav
07:53
created

Properties   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 56%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 78
c 0
b 0
f 0
ccs 14
cts 25
cp 0.56
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A setAdditionalProperties() 0 4 1
A setSchema() 0 4 1
A __set() 0 8 2
A getNestedProperties() 0 3 1
A getSchema() 0 3 1
A addNested() 0 10 3
1
<?php
2
3
namespace Swaggest\JsonSchema\Constraint;
4
5
use Swaggest\JsonSchema\Exception;
6
use Swaggest\JsonSchema\Schema;
7
use Swaggest\JsonSchema\Structure\Egg;
8
use Swaggest\JsonSchema\Structure\Nested;
9
use Swaggest\JsonSchema\Structure\ObjectItem;
10
11
/**
12
 * @method Schema __get($key)
13
 * @method Schema[] toArray()
14
 */
15
class Properties extends ObjectItem implements Constraint
16
{
17
    /** @var Schema[] */
18
    protected $__arrayOfData = array();
19
20
    /** @var Schema */
21
    protected $__schema;
22
23
    public function setSchema(Schema $schema)
24
    {
25
        $this->__schema = $schema;
26
        return $this;
27
    }
28
29
    public function getSchema()
30
    {
31
        return $this->__schema;
32
    }
33
34
    /**
35
     * @param string $name
36
     * @param mixed $column
37
     * @return $this|static
38
     * @throws Exception
39
     */
40 15
    public function __set($name, $column)
41
    {
42 15
        if ($column instanceof Nested) {
43 4
            $this->addNested($column->schema, $name);
44 4
            return $this;
45
        }
46 13
        parent::__set($name, $column);
47 13
        return $this;
48
    }
49
50 3
    public static function create()
51
    {
52 3
        return new static;
53
    }
54
55
    /** @var Schema|null */
56
    private $additionalProperties;
57
58
    /**
59
     * @param Schema $additionalProperties
60
     * @return Properties
61
     */
62
    public function setAdditionalProperties(Schema $additionalProperties = null)
63
    {
64
        $this->additionalProperties = $additionalProperties;
65
        return $this;
66
    }
67
68
69
    /** @var Egg[][] */
70
    public $nestedProperties = array();
71
72
    /** @var Schema[] */
73
    public $nestedPropertyNames = array();
74
75 4
    protected function addNested(Schema $nested, $name)
76
    {
77 4
        if (null === $nested->properties) {
0 ignored issues
show
introduced by
The condition null === $nested->properties can never be true.
Loading history...
78
            throw new Exception('Schema with properties required', Exception::PROPERTIES_REQUIRED);
79
        }
80 4
        $this->nestedPropertyNames[$name] = $name;
81 4
        foreach ($nested->properties->toArray() as $propertyName => $property) {
82 4
            $this->nestedProperties[$propertyName][] = new Egg($nested, $name, $property);
83
        }
84 4
        return $this;
85
    }
86
87
    /**
88
     * @return Egg[][]
89
     */
90
    public function getNestedProperties()
91
    {
92
        return $this->nestedProperties;
93
    }
94
}