SchemaValidationError   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getErrorMessages() 0 7 1
B getMessage() 0 29 6
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
            case self::FIELD_VALIDATION:
41
                $field = $this->extraDetails['field'];
42
                $error = $this->extraDetails['error'];
43
                $value = json_encode($this->extraDetails['value']);
44
45
                return "{$field}: {$error} ({$value})";
46
            case self::ROW_FIELD_VALIDATION:
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