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

Properties   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 59
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __set() 0 8 2
A create() 0 4 1
A setAdditionalProperties() 0 5 1
A addNested() 0 11 3
A getNestedProperties() 0 4 1
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
}