Completed
Push — master ( fba866...b10a48 )
by Ori
03:03
created

SchemaValidator::validateSchema()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 0
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
        $validator->validate(
53
            $this->descriptor,
54
                (object)['$ref' => 'file://' . realpath(dirname(__FILE__)).'/schemas/table-schema.json']
55
        );
56
        if (!$validator->isValid()) {
57
            foreach ($validator->getErrors() as $error) {
58
                $this->addError(
59
                    SchemaValidationError::SCHEMA_VIOLATION,
60
                    sprintf("[%s] %s", $error['property'], $error['message'])
61
                );
62
            }
63
        }
64
    }
65
}
66