Completed
Pull Request — master (#76)
by Simone
06:07 queued 03:13
created

Schema::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
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
17
final class Schema
18
{
19
    const PRIMITIVE_ARRAY   = 'array';
20
    const PRIMITIVE_BOOLEAN = 'boolean';
21
    const PRIMITIVE_INTEGER = 'integer';
22
    const PRIMITIVE_NUMBER  = 'number';
23
    const PRIMITIVE_NULL    = 'null';
24
    const PRIMITIVE_OBJECT  = 'object';
25
    const PRIMITIVE_STRING  = 'string';
26
27
    private $completeSchema;
28
29
    private $schemaNames = [];
30
31
    public function __construct($schema)
32
    {
33
        if (!isset($schema['type'])) {
34
            throw new InvalidTypeException();
35
        }
36
37
        $this->completeSchema = $schema;
38
39
        $this->schemaNames[] = 'root';
40
41
        $this->parseSchema($this->completeSchema);
42
    }
43
44
    private function parseSchema($itemSchema)
45
    {
46
        foreach ($itemSchema as $key => $value) {
47
            if ($key != 'title' && $key != 'type') {
48
                $this->schemaNames[] = $key;
49
                $this->parseSchema($value);
50
            }
51
        }
52
    }
53
54
    public function countSubSchemas()
55
    {
56
        return 0;
57
    }
58
59
    public function completeSchema()
60
    {
61
        return $this->completeSchema;
62
    }
63
64
    public function schemaNames()
65
    {
66
        return $this->schemaNames;
67
    }
68
69
    public function getTitle()
70
    {
71
        return $this->completeSchema['title'];
72
    }
73
}
74