Completed
Push — master ( fbe049...cd378b )
by Ori
07:11
created

src/SchemaValidationError.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace frictionlessdata\tableschema;
4
5
class SchemaValidationError
6
{
7
    const LOAD_FAILED = 1;
8
    const SCHEMA_VIOLATION = 8;
9
    const FIELD_VALIDATION = 21;
10
    const ROW_FIELD_VALIDATION = 22;
11
    const ROW_VALIDATION = 23;
12
13
    public $code;
14
    public $extraDetails;
15
16
    /**
17
     * @param int   $code
18
     * @param mixed $extraDetails
19
     */
20
    public function __construct($code, $extraDetails = null)
21
    {
22
        $this->code = $code;
23
        $this->extraDetails = $extraDetails;
24
    }
25
26
    /**
27
     * returns a string representation of the error.
28
     *
29
     * @return string
30
     *
31
     * @throws \Exception
32
     */
33
    public function getMessage()
34
    {
35
        switch ($this->code) {
36
            case self::LOAD_FAILED:
37
                return $this->extraDetails;
38
            case self::SCHEMA_VIOLATION:
39
                return $this->extraDetails;
40 View Code Duplication
            case self::FIELD_VALIDATION:
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
                $field = $this->extraDetails['field'];
42
                $error = $this->extraDetails['error'];
43
                $value = $this->extraDetails['value'];
44
45
                return "{$field}: {$error} ({$value})";
46 View Code Duplication
            case self::ROW_FIELD_VALIDATION:
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
                $row = $this->extraDetails['row'];
48
                $field = $this->extraDetails['field'];
49
                $error = $this->extraDetails['error'];
50
                $value = $this->extraDetails['value'];
51
52
                return "row {$row} {$field}: {$error} ({$value})";
53
            case self::ROW_VALIDATION:
54
                $row = $this->extraDetails['row'];
55
                $error = $this->extraDetails['error'];
56
57
                return "row {$row}: {$error}";
58
            default:
59
                throw new \Exception("Invalid schema validation code: {$this->code}");
60
        }
61
    }
62
63
    /**
64
     * given a list of validation errors, returns a single combined string message.
65
     *
66
     * @param SchemaValidationError[] $validationErrors
67
     *
68
     * @return string
69
     */
70
    public static function getErrorMessages($validationErrors)
71
    {
72
        return implode(', ', array_map(function ($validationError) {
73
            /* @var SchemaValidationError $validationError */
74
            return $validationError->getMessage();
75
        }, $validationErrors));
76
    }
77
}
78