Completed
Pull Request — master (#76)
by Simone
05:05 queued 02:35
created

Schema::countSubSchemas()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sensorario\Resources;
13
14
use RuntimeException;
15
use Sensorario\Resources\Exceptions\InvalidTypeException;
16
use Sensorario\Resources\Exceptions\NoPropertiesException;
17
use Sensorario\Resources\Exceptions\NoPropertyTypeException;
18
use Sensorario\Resources\Exceptions\NoTypeException;
19
20
final class Schema
21
{
22
    const PRIMITIVE_ARRAY   = 'array';
23
    const PRIMITIVE_BOOLEAN = 'boolean';
24
    const PRIMITIVE_DEFAULT = Schema::PRIMITIVE_NULL;
25
    const PRIMITIVE_INTEGER = 'integer';
26
    const PRIMITIVE_NULL    = 'null';
27
    const PRIMITIVE_NUMBER  = 'number';
28
    const PRIMITIVE_OBJECT  = 'object';
29
    const PRIMITIVE_STRING  = 'string';
30
31
    private $completeSchema;
32
33
    private $schemaNames = [];
34
35
    private $validTypes = [
36
        self::PRIMITIVE_ARRAY,
37
        self::PRIMITIVE_BOOLEAN,
38
        self::PRIMITIVE_DEFAULT,
39
        self::PRIMITIVE_INTEGER,
40
        self::PRIMITIVE_NULL,
41
        self::PRIMITIVE_NUMBER,
42
        self::PRIMITIVE_OBJECT,
43
        self::PRIMITIVE_STRING,
44
    ];
45
46
    private $subSchemaCounter = 0;
47
48
    public function __construct($schema)
49
    {
50
        $this->completeSchema = $schema;
51
52
        $this->schemaNames[] = 'root';
53
54
        $this->parseSchema($this->completeSchema);
55
    }
56
57
    private function parseSchema($schema)
58
    {
59
        if (!isset($schema['type'])) {
60
            throw new NoTypeException('Schema type is not defined');
61
        }
62
63
        if (!in_array($schema['type'], $this->validTypes)) {
64
            throw new InvalidTypeException();
65
        }
66
67
        if (!isset($schema['properties'])) {
68
            throw new NoPropertiesException();
69
        }
70
71
        foreach ($schema['properties'] as $name => $def) {
72
            if (!isset($def['type'])) {
73
                throw new NoPropertyTypeException(
74
                    'Property `' . $name . '`'
75
                    . ' MUST HAVE a type'
76
                );
77
            }
78
        }
79
80
        foreach ($schema as $key => $value) {
81
            if (!in_array($key, ['properties', 'title', 'type'])) {
82
                $this->schemaNames[] = $key;
83
                $this->subSchemaCounter++;
84
                $this->parseSchema($value);
85
            }
86
        }
87
    }
88
89
    public function countSubSchemas()
90
    {
91
        return $this->subSchemaCounter;
92
    }
93
94
    public function completeSchema()
95
    {
96
        return $this->completeSchema;
97
    }
98
99
    public function schemaNames()
100
    {
101
        return $this->schemaNames;
102
    }
103
104
    public function getTitle()
105
    {
106
        return $this->completeSchema['title'];
107
    }
108
}
109