Completed
Push — master ( b10a48...29cde4 )
by Ori
05:27
created

SchemaValidationError::getMessage()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 23

Duplication

Lines 11
Ratio 42.31 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 26
rs 8.439
cc 6
eloc 23
nc 6
nop 0
1
<?php
2
namespace frictionlessdata\tableschema;
3
4
class SchemaValidationError
5
{
6
    const LOAD_FAILED=1;
7
    const SCHEMA_VIOLATION=8;
8
    const FIELD_VALIDATION=21;
9
    const ROW_FIELD_VALIDATION=22;
10
    const ROW_VALIDATION=23;
11
12
    public $code;
13
    public $extraDetails;
14
15
    /**
16
     * @param integer $code
17
     * @param mixed $extraDetails
18
     */
19
    public function __construct($code, $extraDetails=null)
20
    {
21
        $this->code = $code;
22
        $this->extraDetails = $extraDetails;
23
    }
24
25
    /**
26
     * returns a string representation of the error
27
     * @return string
28
     * @throws \Exception
29
     */
30
    public function getMessage()
31
    {
32
        switch ($this->code) {
33
            case self::LOAD_FAILED:
34
                return $this->extraDetails;
35
            case self::SCHEMA_VIOLATION:
36
                return $this->extraDetails;
37 View Code Duplication
            case self::FIELD_VALIDATION:
0 ignored issues
show
Duplication introduced by
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...
38
                $field = $this->extraDetails["field"];
39
                $error = $this->extraDetails["error"];
40
                $value = $this->extraDetails["value"];
41
                return "{$field}: {$error} ({$value})";
42 View Code Duplication
            case self::ROW_FIELD_VALIDATION:
0 ignored issues
show
Duplication introduced by
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...
43
                $row = $this->extraDetails["row"];
44
                $field = $this->extraDetails["field"];
45
                $error = $this->extraDetails["error"];
46
                $value = $this->extraDetails["value"];
47
                return "row {$row} {$field}: {$error} ({$value})";
48
            case self::ROW_VALIDATION:
49
                $row = $this->extraDetails["row"];
50
                $error = $this->extraDetails["error"];
51
                return "row {$row}: {$error}";
52
            default:
53
                throw new \Exception("Invalid schema validation code: {$this->code}");
54
        }
55
    }
56
57
    /**
58
     * given a list of validation errors, returns a single combined string message
59
     * @param SchemaValidationError[] $validationErrors
60
     * @return string
61
     */
62
    public static function getErrorMessages($validationErrors)
63
    {
64
        return implode(", ", array_map(function($validationError){
65
            /** @var SchemaValidationError $validationError */
66
            return $validationError->getMessage();
67
        }, $validationErrors));
68
    }
69
}
70