Completed
Pull Request — master (#76)
by Simone
02:27
created

Schema::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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\NoTypeException;
18
19
final class Schema
20
{
21
    const PRIMITIVE_ARRAY   = 'array';
22
    const PRIMITIVE_BOOLEAN = 'boolean';
23
    const PRIMITIVE_DEFAULT = Schema::PRIMITIVE_NULL;
24
    const PRIMITIVE_INTEGER = 'integer';
25
    const PRIMITIVE_NULL    = 'null';
26
    const PRIMITIVE_NUMBER  = 'number';
27
    const PRIMITIVE_OBJECT  = 'object';
28
    const PRIMITIVE_STRING  = 'string';
29
30
    private $completeSchema;
31
32
    private $schemaNames = [];
33
34
    private $validTypes = [
35
        self::PRIMITIVE_ARRAY,
36
        self::PRIMITIVE_BOOLEAN,
37
        self::PRIMITIVE_DEFAULT,
38
        self::PRIMITIVE_INTEGER,
39
        self::PRIMITIVE_NULL,
40
        self::PRIMITIVE_NUMBER,
41
        self::PRIMITIVE_OBJECT,
42
        self::PRIMITIVE_STRING,
43
    ];
44
45
    public function __construct($schema)
46
    {
47
        $this->completeSchema = $schema;
48
49
        $this->schemaNames[] = 'root';
50
51
        $this->parseSchema($this->completeSchema);
52
    }
53
54
    private function parseSchema($schema)
55
    {
56
        if (!isset($schema['type'])) {
57
            throw new NoTypeException('Schema type is not defined');
58
        }
59
60
        if (!in_array($schema['type'], $this->validTypes)) {
61
            throw new InvalidTypeException();
62
        }
63
64
        if (!isset($schema['properties'])) {
65
            throw new NoPropertiesException();
66
        }
67
68
        foreach ($schema['properties'] as $name => $def) {
69
            if (!isset($def['type'])) {
70
                throw new NoTypeException(
71
                    'Property `' . $name . '`'
72
                    . ' MUST HAVE a type'
73
                );
74
            }
75
        }
76
77
        foreach ($schema as $key => $value) {
78
            if (!in_array($key, ['properties', 'title', 'type'])) {
79
                $this->schemaNames[] = $key;
80
                $this->parseSchema($value);
81
            }
82
        }
83
    }
84
85
    public function countSubSchemas()
86
    {
87
        return 0;
88
    }
89
90
    public function completeSchema()
91
    {
92
        return $this->completeSchema;
93
    }
94
95
    public function schemaNames()
96
    {
97
        return $this->schemaNames;
98
    }
99
100
    public function getTitle()
101
    {
102
        return $this->completeSchema['title'];
103
    }
104
}
105