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

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