Completed
Push — master ( fff5f3...fc4fc8 )
by Thomas
02:20
created

Validation::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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
use ORM\Exception;
10
11
trait Validation
12
{
13
    /**
14
     * Check if the validator is enabled
15
     *
16
     * @return bool
17
     */
18 39
    public static function isValidatorEnabled()
19
    {
20 39
        return isset(self::$enabledValidators[static::class]) ?
21 39
            self::$enabledValidators[static::class] : static::$enableValidator;
22
    }
23
24
    /**
25
     * Enable validator
26
     *
27
     * @param bool $enable
28
     */
29 8
    public static function enableValidator($enable = true)
30
    {
31 8
        self::$enabledValidators[static::class] = $enable;
32 8
    }
33
34
    /**
35
     * Disable validator
36
     *
37
     * @param bool $disable
38
     */
39 33
    public static function disableValidator($disable = true)
40
    {
41 33
        self::$enabledValidators[static::class] = !$disable;
42 33
    }
43
44
    /**
45
     * Get a description for this table.
46
     *
47
     * @return Table|Column[]
48
     * @codeCoverageIgnore This is just a proxy
49
     */
50
    public static function describe()
51
    {
52
        return EM::getInstance(static::class)->describe(static::getTableName());
53
    }
54
55
    /**
56
     * Validate $value for $attribute
57
     *
58
     * @param string $attribute
59
     * @param mixed  $value
60
     * @return bool|Error
61
     * @throws Exception
62
     */
63 10
    public static function validate($attribute, $value)
64
    {
65 10
        return static::describe()->validate(static::getColumnName($attribute), $value);
66
    }
67
68
    /**
69
     * Validate $data
70
     *
71
     * $data has to be an array of $attribute => $value
72
     *
73
     * @param array $data
74
     * @return array
75
     */
76 1
    public static function validateArray(array $data)
77
    {
78 1
        $result = $data;
79 1
        foreach ($result as $attribute => &$value) {
80 1
            $value = static::validate($attribute, $value);
81
        }
82 1
        return $result;
83
    }
84
85
    /**
86
     * Check if the current data is valid
87
     *
88
     * Returns boolean true when valid otherwise an array of Errors.
89
     *
90
     * @return bool|Error[]
91
     */
92 1
    public function isValid()
93
    {
94 1
        $result = [];
95
96 1
        $presentColumns = [];
97 1
        foreach ($this->data as $column => $value) {
0 ignored issues
show
Bug introduced by
The property data 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...
98 1
            $presentColumns[] = $column;
99 1
            $result[]         = static::validate($column, $value);
100
        }
101
102 1
        foreach (static::describe() as $column) {
103 1
            if (!in_array($column->name, $presentColumns)) {
104 1
                $result[] = static::validate($column->name, null);
105
            }
106
        }
107
108 1
        $result = array_values(array_filter($result, function ($error) {
109 1
            return $error instanceof Error;
110 1
        }));
111
112
        return count($result) === 0 ? true : $result;
113
    }
114
}
115