Completed
Pull Request — master (#76)
by Simone
03:01
created

Schema::required()   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\Schema;
13
14
use RuntimeException;
15
16
final class Schema
17
{
18
    const PRIMITIVE_ARRAY   = 'array';
19
    const PRIMITIVE_BOOLEAN = 'boolean';
20
    const PRIMITIVE_DEFAULT = Schema::PRIMITIVE_NULL;
21
    const PRIMITIVE_INTEGER = 'integer';
22
    const PRIMITIVE_NULL    = 'null';
23
    const PRIMITIVE_NUMBER  = 'number';
24
    const PRIMITIVE_OBJECT  = 'object';
25
    const PRIMITIVE_STRING  = 'string';
26
27
    private $completeSchema;
28
29
    private $schemaNames = [];
30
31
    private $required = [];
32
33
    private $validTypes = [
34
        self::PRIMITIVE_ARRAY,
35
        self::PRIMITIVE_BOOLEAN,
36
        self::PRIMITIVE_DEFAULT,
37
        self::PRIMITIVE_INTEGER,
38
        self::PRIMITIVE_NULL,
39
        self::PRIMITIVE_NUMBER,
40
        self::PRIMITIVE_OBJECT,
41
        self::PRIMITIVE_STRING,
42
    ];
43
44
    private $subSchemaCounter = 0;
45
46
    private $properties = [];
47
48
    public function __construct($schema)
49
    {
50
        $this->completeSchema = $schema;
51
        $this->schemaNames[] = 'root';
52
        $this->parseSchema($this->completeSchema);
53
    }
54
55
    private function parseSchema($schema)
56
    {
57
        if (!isset($schema['type'])) {
58
            throw new Exceptions\NoTypeException();
59
        }
60
61
        if (!in_array($schema['type'], $this->validTypes)) {
62
            throw new Exceptions\InvalidTypeException();
63
        }
64
65
        if (!isset($schema['properties'])) {
66
            throw new Exceptions\NoPropertiesException();
67
        }
68
69
        foreach ($schema['properties'] as $name => $def) {
70
            $this->properties[$name] = $def;
71
            if (!isset($def['type'])) {
72
                throw new Exceptions\NoPropertyTypeException();
73
            }
74
        }
75
76
        if (isset($schema['required'])) {
77
            $this->required = array_merge(
78
                $this->required,
79
                $schema['required']
80
            );
81
        }
82
83
        foreach ($schema as $key => $value) {
84
            if (!in_array($key, ['required', 'properties', 'title', 'type'])) {
85
                $this->schemaNames[] = $key;
86
                $this->subSchemaCounter++;
87
                $this->parseSchema($value);
88
            }
89
        }
90
    }
91
92
    public function countSubSchemas()
93
    {
94
        return $this->subSchemaCounter;
95
    }
96
97
    public function completeSchema()
98
    {
99
        return $this->completeSchema;
100
    }
101
102
    public function schemaNames()
103
    {
104
        return $this->schemaNames;
105
    }
106
107
    public function getTitle()
108
    {
109
        return $this->completeSchema['title'];
110
    }
111
112
    /** @todo move this inside an applipcation service */
113
    public function validate($json) 
114
    {
115
        $jsonAsArray = json_decode($json, true);
116
117
        foreach ($jsonAsArray as $name => $property) {
118
            if (!isset($this->properties()[$name])) {
119
                throw new Exceptions\NotAllowedPropertyException();
120
            }
121
        }
122
123
        foreach ($this->required as $requiredProperty) {
124
            if (!isset($jsonAsArray[$requiredProperty])) {
125
                throw new Exceptions\MissingPropertyException();
126
            }
127
        }
128
129
        foreach ($jsonAsArray as $name => $property) {
130
            $prop = $this->properties()[$name];
131
            if (gettype($jsonAsArray[$name]) != $prop['type']) {
132
                if ($prop['type'] == Schema::PRIMITIVE_INTEGER && is_numeric($jsonAsArray[$name])) {
133
                    continue;
134
                }
135
136
                if ($prop['type'] == Schema::PRIMITIVE_ARRAY) {
137
                    if (!isset($prop['items'])) {
138
                        throw new Exceptions\UndefinedArrayTypeException();
139
                    }
140
                }
141
142
                throw new Exceptions\NotAllowedValueException();
143
            }
144
        }
145
146
        return json_decode($json);
147
    }
148
149
    public function properties()
150
    {
151
        return $this->properties;
152
    }
153
154
    public function required()
155
    {
156
        return $this->required;
157
    }
158
}
159