Completed
Push — master ( 29cde4...77c4c6 )
by Ori
03:02
created

SchemaValidator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 75
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 5 1
A __construct() 0 5 1
A getValidationErrors() 0 6 1
A addError() 0 4 1
A validateSchema() 0 19 3
B applyForeignKeysResourceHack() 0 14 9
1
<?php
2
namespace frictionlessdata\tableschema;
3
4
/**
5
 * validates a table schema descriptor object
6
 * returns a list of validation errors
7
 */
8
class SchemaValidator
9
{
10
    /**
11
     * @param object $descriptor
12
     * @return SchemaValidationError[]
13
     */
14
    public static function validate($descriptor)
15
    {
16
        $validator = new self($descriptor);
17
        return $validator->getValidationErrors();
18
    }
19
20
    /**
21
     * @param object $descriptor
22
     */
23
    public function __construct($descriptor)
24
    {
25
        $this->descriptor = $descriptor;
0 ignored issues
show
Bug introduced by
The property descriptor does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
        $this->errors = [];
0 ignored issues
show
Bug introduced by
The property errors does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
    }
28
29
    /**
30
     * @return SchemaValidationError[]
31
     */
32
    public function getValidationErrors()
33
    {
34
        $this->errors = [];
35
        $this->validateSchema();
36
        return $this->errors;
37
    }
38
39
    /**
40
     * @param integer $code
41
     * @param mixed $extraDetails
42
     */
43
    protected function addError($code, $extraDetails=null)
44
    {
45
        $this->errors[] = new SchemaValidationError($code, $extraDetails);
46
    }
47
48
    protected function validateSchema()
49
    {
50
        // Validate
51
        $validator = new \JsonSchema\Validator();
52
        $descriptor = json_decode(json_encode($this->descriptor));
53
        $this->applyForeignKeysResourceHack($descriptor);
54
        $validator->validate(
55
            $descriptor,
56
            (object)['$ref' => 'file://' . realpath(dirname(__FILE__)).'/schemas/table-schema.json']
57
        );
58
        if (!$validator->isValid()) {
59
            foreach ($validator->getErrors() as $error) {
60
                $this->addError(
61
                    SchemaValidationError::SCHEMA_VIOLATION,
62
                    sprintf("[%s] %s", $error['property'], $error['message'])
63
                );
64
            }
65
        }
66
    }
67
68
    protected function applyForeignKeysResourceHack($descriptor)
69
    {
70
        if (isset($descriptor->foreignKeys) && is_array($descriptor->foreignKeys)) {
71
            foreach ($descriptor->foreignKeys as $foreignKey) {
72
                if (
73
                    is_object($foreignKey)
74
                    && isset($foreignKey->reference) && is_object($foreignKey->reference)
75
                    && isset($foreignKey->reference->resource) && empty($foreignKey->reference->resource)
76
                ) {
77
                    $foreignKey->reference->resource = "self";
78
                }
79
            }
80
        }
81
    }
82
}
83