Completed
Pull Request — master (#76)
by Simone
04:54 queued 01:42
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;
13
14
use RuntimeException;
15
use Sensorario\Resources\Exceptions\InvalidTypeException;
16
use Sensorario\Resources\Exceptions\NoPropertiesException;
17
18
final class Schema
19
{
20
    const PRIMITIVE_ARRAY   = 'array';
21
    const PRIMITIVE_BOOLEAN = 'boolean';
22
    const PRIMITIVE_INTEGER = 'integer';
23
    const PRIMITIVE_NUMBER  = 'number';
24
    const PRIMITIVE_NULL    = 'null';
25
    const PRIMITIVE_OBJECT  = 'object';
26
    const PRIMITIVE_STRING  = 'string';
27
28
    private $completeSchema;
29
30
    private $schemaNames = [];
31
32
    public function __construct($schema)
33
    {
34
        if (!isset($schema['type'])) {
35
            throw new InvalidTypeException();
36
        }
37
38
        if (!isset($schema['properties'])) {
39
            throw new NoPropertiesException();
40
        }
41
42
        $this->completeSchema = $schema;
43
44
        $this->schemaNames[] = 'root';
45
46
        $this->parseSchema($this->completeSchema);
47
    }
48
49
    private function parseSchema($itemSchema)
50
    {
51
        foreach ($itemSchema as $key => $value) {
52
            if (!in_array($key, ['properties', 'title', 'type'])) {
53
                $this->schemaNames[] = $key;
54
                $this->parseSchema($value);
55
            }
56
        }
57
    }
58
59
    public function countSubSchemas()
60
    {
61
        return 0;
62
    }
63
64
    public function completeSchema()
65
    {
66
        return $this->completeSchema;
67
    }
68
69
    public function schemaNames()
70
    {
71
        return $this->schemaNames;
72
    }
73
74
    public function getTitle()
75
    {
76
        return $this->completeSchema['title'];
77
    }
78
}
79