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

Properties::addNested()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace Swaggest\JsonSchema\Constraint;
4
5
use Swaggest\JsonSchema\Exception;
6
use Swaggest\JsonSchema\MagicMap;
7
use Swaggest\JsonSchema\Schema;
8
use Swaggest\JsonSchema\Structure\Egg;
9
use Swaggest\JsonSchema\Structure\Nested;
10
11
/**
12
 * @method Schema __get($key)
13
 */
14
class Properties extends MagicMap implements Constraint
15
{
16
    /** @var Schema[] */
17
    protected $__arrayOfData = array();
18
19
    public function __set($name, $column)
20
    {
21
        if ($column instanceof Nested) {
22
            $this->addNested($column->schema, $name);
23
            return $this;
24
        }
25
        return parent::__set($name, $column);
26
    }
27
28
    public static function create()
29
    {
30
        return new static;
31
    }
32
33
    /** @var Schema */
34
    private $additionalProperties;
35
36
    /**
37
     * @param Schema $additionalProperties
38
     * @return Properties
39
     */
40
    public function setAdditionalProperties(Schema $additionalProperties = null)
41
    {
42
        $this->additionalProperties = $additionalProperties;
43
        return $this;
44
    }
45
46
47
    /** @var Egg[] */
48
    private $nestedProperties = array();
49
50
    /** @var Schema[] */
51
    public $nestedPropertyNames = array();
52
53
    protected function addNested(Schema $nested, $name)
54
    {
55
        if (null === $nested->properties) {
56
            throw new Exception('Schema with properties required', Exception::PROPERTIES_REQUIRED);
57
        }
58
        $this->nestedPropertyNames[$name] = $name;
59
        foreach ($nested->properties->toArray() as $propertyName => $property) {
60
            $this->nestedProperties[$propertyName] = new Egg($nested, $name, $property);
61
        }
62
        return $this;
63
    }
64
65
    /**
66
     * @return Egg[]
67
     */
68
    public function getNestedProperties()
69
    {
70
        return $this->nestedProperties;
71
    }
72
}