Completed
Branch master (7d7b3f)
by Ori
01:38
created

DatapackageValidator::getDescriptorForValidation()   C

Complexity

Conditions 9
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 11
nc 2
nop 0
dl 0
loc 22
rs 6.412
c 0
b 0
f 0
1
<?php
2
3
namespace frictionlessdata\datapackage\Validators;
4
5
use frictionlessdata\datapackage\Registry;
6
7
/**
8
 * validate a datapackage descriptor
9
 * checks the profile attribute to determine which schema to validate with.
10
 */
11
class DatapackageValidator extends BaseValidator
12
{
13
    protected function getSchemaValidationErrorClass()
14
    {
15
        return 'frictionlessdata\\datapackage\\Validators\\DatapackageValidationError';
16
    }
17
18
    protected function getValidationProfile()
19
    {
20
        return Registry::getDatapackageValidationProfile($this->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...
21
    }
22
23
    protected function getDescriptorForValidation()
24
    {
25
        // add base path to uri fields - it runs before validations, so need to validate the attributes we need
26
        // TODO: find a more elegant way to do it with support for registring custom url fields
27
        $descriptor = clone $this->descriptor;
28
        if (isset($descriptor->resources) && is_array($descriptor->resources)) {
29
            foreach ($descriptor->resources as &$resource) {
30
                if (is_object($resource)) {
31
                    $resource = clone $resource;
32
                    if (isset($resource->path) && is_array($resource->path)) {
33
                        foreach ($resource->path as &$url) {
34
                            if (is_string($url)) {
35
                                $url = 'file://'.$url;
36
                            }
37
                        }
38
                    }
39
                }
40
            }
41
        }
42
43
        return $descriptor;
44
    }
45
46
    protected function validateSchema()
47
    {
48
        parent::validateSchema();
49
        if ($this->getValidationProfile() != 'data-package') {
50
            // all schemas must be an extension of datapackage spec
51
            $this->validateSchemaUrl(
52
                $this->convertValidationSchemaFilenameToUrl(
53
                    $this->getJsonSchemaFileFromRegistry('data-package')
54
                )
55
            );
56
        }
57
    }
58
59
    protected function validateKeys()
60
    {
61
        foreach ($this->descriptor->resources as $resourceDescriptor) {
62
            foreach ($this->resourceValidate($resourceDescriptor) as $error) {
63
                $this->errors[] = $error;
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...
64
            }
65
        }
66
    }
67
68
    protected function resourceValidate($resourceDescriptor)
69
    {
70
        return ResourceValidator::validate($resourceDescriptor, $this->basePath);
71
    }
72
73
    protected function getJsonSchemaFileFromRegistry($profile)
74
    {
75
        if ($filename = Registry::getJsonSchemaFile($profile)) {
76
            return $filename;
77
        } else {
78
            return parent::getJsonSchemaFileFromRegistry($profile);
79
        }
80
    }
81
}
82