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

SchemaValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 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 17 3
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