Completed
Pull Request — master (#19)
by Viacheslav
08:06
created

Properties::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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) {
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
}