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

Schema::validate()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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