Completed
Pull Request — master (#76)
by Simone
03:01
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\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
        if (isset($schema['required'])) {
62
            $this->required = $schema['required'];
63
        }
64
    }
65
66
    private function parseSchema($schema)
67
    {
68
        if (!isset($schema['type'])) {
69
            throw new NoTypeException();
70
        }
71
72
        if (!in_array($schema['type'], $this->validTypes)) {
73
            throw new InvalidTypeException();
74
        }
75
76
        if (!isset($schema['properties'])) {
77
            throw new NoPropertiesException();
78
        }
79
80
        foreach ($schema['properties'] as $name => $def) {
81
            $this->properties[$name] = $def;
82
            if (!isset($def['type'])) {
83
                throw new NoPropertyTypeException();
84
            }
85
        }
86
87
        foreach ($schema as $key => $value) {
88
            if (!in_array($key, ['required', 'properties', 'title', 'type'])) {
89
                $this->schemaNames[] = $key;
90
                $this->subSchemaCounter++;
91
                $this->parseSchema($value);
92
            }
93
        }
94
    }
95
96
    public function countSubSchemas()
97
    {
98
        return $this->subSchemaCounter;
99
    }
100
101
    public function completeSchema()
102
    {
103
        return $this->completeSchema;
104
    }
105
106
    public function schemaNames()
107
    {
108
        return $this->schemaNames;
109
    }
110
111
    public function getTitle()
112
    {
113
        return $this->completeSchema['title'];
114
    }
115
116
    /** @todo move this inside an applipcation service */
117
    public function validate($json) 
118
    {
119
        $jsonAsArray = json_decode($json, true);
120
121
        foreach ($jsonAsArray as $name => $property) {
122
            if (!isset($this->properties()[$name])) {
123
                throw new NotAllowedPropertyException();
124
            }
125
        }
126
127
        foreach ($this->required as $requiredProperty) {
128
            if (!isset($jsonAsArray[$requiredProperty])) {
129
                throw new MissingPropertyException();
130
            }
131
        }
132
133
        foreach ($jsonAsArray as $name => $property) {
134
            $prop = $this->properties()[$name];
135
            if (gettype($jsonAsArray[$name]) != $prop['type']) {
136
                if ($prop['type'] == Schema::PRIMITIVE_INTEGER && is_numeric($jsonAsArray[$name])) {
137
                    continue;
138
                }
139
140
                throw new NotAllowedValueException();
141
            }
142
        }
143
144
        return json_decode($json);
145
    }
146
147
    public function properties()
148
    {
149
        return $this->properties;
150
    }
151
}
152