Validation   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 1
dl 0
loc 116
rs 10
c 0
b 0
f 0
ccs 28
cts 28
cp 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidatorEnabled() 0 5 2
A enableValidator() 0 4 1
A disableValidator() 0 4 1
A describe() 0 4 1
A validate() 0 4 1
A validateArray() 0 8 2
A isValid() 0 22 5
1
<?php
2
3
namespace ORM\Entity;
4
5
use ORM\Dbal\Column;
6
use ORM\Dbal\Error;
7
use ORM\Dbal\Table;
8
use ORM\EntityManager as EM;
9
10
trait Validation
11
{
12
13
    /** The current data of a row.
14
     * @var mixed[] */
15
    protected $data = [];
16
17
    /** Whether or not the validator for this class is enabled.
18
     * @var bool */
19
    protected static $enableValidator = false;
20
21
    /** Whether or not the validator for a class got enabled during runtime.
22
     * @var bool[] */
23 39
    protected static $enabledValidators = [];
24
25 39
    /**
26 39
     * Check if the validator is enabled
27
     *
28
     * @return bool
29
     */
30
    public static function isValidatorEnabled()
31
    {
32
        return isset(self::$enabledValidators[static::class]) ?
33
            self::$enabledValidators[static::class] : static::$enableValidator;
34 8
    }
35
36 8
    /**
37 8
     * Enable validator
38
     *
39
     * @param bool $enable
40
     */
41
    public static function enableValidator($enable = true)
42
    {
43
        self::$enabledValidators[static::class] = $enable;
44 33
    }
45
46 33
    /**
47 33
     * Disable validator
48
     *
49
     * @param bool $disable
50
     */
51
    public static function disableValidator($disable = true)
52
    {
53
        self::$enabledValidators[static::class] = !$disable;
54
    }
55
56
    /**
57
     * Get a description for this table.
58
     *
59
     * @return Table|Column[]
60
     * @codeCoverageIgnore This is just a proxy
61
     */
62
    public static function describe()
63
    {
64
        return EM::getInstance(static::class)->describe(static::getTableName());
65
    }
66
67
    /**
68 10
     * Validate $value for $attribute
69
     *
70 10
     * @param string $attribute
71
     * @param mixed $value
72
     * @return bool|Error
73
     */
74
    public static function validate($attribute, $value)
75
    {
76
        return static::describe()->validate(static::getColumnName($attribute), $value);
77
    }
78
79
    /**
80
     * Validate $data
81 1
     *
82
     * $data has to be an array of $attribute => $value
83 1
     *
84 1
     * @param array $data
85 1
     * @return array
86
     */
87 1
    public static function validateArray(array $data)
88
    {
89
        $result = $data;
90
        foreach ($result as $attribute => &$value) {
91
            $value = static::validate($attribute, $value);
92
        }
93
        return $result;
94
    }
95
96
    /**
97 1
     * Check if the current data is valid
98
     *
99 1
     * Returns boolean true when valid otherwise an array of Errors.
100
     *
101 1
     * @return bool|Error[]
102 1
     */
103 1
    public function isValid()
104 1
    {
105
        $result = [];
106
107 1
        $presentColumns = [];
108 1
        foreach ($this->data as $column => $value) {
109 1
            $presentColumns[] = $column;
110
            $result[]         = static::validate($column, $value);
111
        }
112
113 1
        foreach (static::describe() as $column) {
114 1
            if (!in_array($column->name, $presentColumns)) {
115 1
                $result[] = static::validate($column->name, null);
116
            }
117
        }
118
119
        $result = array_values(array_filter($result, function ($error) {
120
            return $error instanceof Error;
121
        }));
122
123
        return count($result) === 0 ? true : $result;
124
    }
125
}
126