|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipbox/skeleton/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipbox/skeleton |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Flipbox\Skeleton\Error; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Flipbox Factory <[email protected]> |
|
13
|
|
|
* @since 2.0.0 |
|
14
|
|
|
*/ |
|
15
|
|
|
trait ErrorTrait |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array The errors (key => value[]) |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $errors; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Identify whether there are any errors |
|
24
|
|
|
* |
|
25
|
|
|
* @param null $attribute |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
|
|
public function hasErrors($attribute = null): bool |
|
29
|
|
|
{ |
|
30
|
|
|
$errors = $this->getErrors(); |
|
31
|
|
|
return $attribute === null ? !empty($errors) : isset($errors[$attribute]); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Returns the errors for all attribute or a single attribute. |
|
36
|
|
|
* |
|
37
|
|
|
* @param null $attribute |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getErrors($attribute = null): array |
|
41
|
|
|
{ |
|
42
|
|
|
if ($attribute === null) { |
|
43
|
|
|
return $this->errors === null ? [] : $this->errors; |
|
44
|
|
|
} else { |
|
45
|
|
|
return isset($this->errors[$attribute]) ? $this->errors[$attribute] : []; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the first error of every attribute in the model. |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getFirstErrors(): array |
|
55
|
|
|
{ |
|
56
|
|
|
$errors = $this->getErrors(); |
|
57
|
|
|
if (empty($errors)) { |
|
58
|
|
|
return []; |
|
59
|
|
|
} else { |
|
60
|
|
|
$errors = []; |
|
61
|
|
|
foreach ($errors as $name => $es) { |
|
62
|
|
|
if (!empty($es)) { |
|
63
|
|
|
$errors[$name] = reset($es); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $errors; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns the first error of the specified attribute. |
|
73
|
|
|
* |
|
74
|
|
|
* @param $attribute |
|
75
|
|
|
* @return string|null |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getFirstError($attribute) |
|
78
|
|
|
{ |
|
79
|
|
|
$errors = $this->getErrors(); |
|
80
|
|
|
return isset($errors[$attribute]) ? reset($errors[$attribute]) : null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Adds a new error to the specified attribute. |
|
85
|
|
|
* |
|
86
|
|
|
* @param $attribute |
|
87
|
|
|
* @param string $error |
|
88
|
|
|
*/ |
|
89
|
|
|
public function addError($attribute, $error = '') |
|
90
|
|
|
{ |
|
91
|
|
|
$this->errors[$attribute][] = $error; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Adds a list of errors. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $items |
|
98
|
|
|
*/ |
|
99
|
|
|
public function addErrors(array $items) |
|
100
|
|
|
{ |
|
101
|
|
|
foreach ($items as $attribute => $errors) { |
|
102
|
|
|
if (is_array($errors)) { |
|
103
|
|
|
foreach ($errors as $error) { |
|
104
|
|
|
$this->addError($attribute, $error); |
|
105
|
|
|
} |
|
106
|
|
|
} else { |
|
107
|
|
|
$this->addError($attribute, $errors); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Removes errors for all attributes or a single attribute. |
|
114
|
|
|
* |
|
115
|
|
|
* @param null $attribute |
|
116
|
|
|
*/ |
|
117
|
|
|
public function clearErrors($attribute = null) |
|
118
|
|
|
{ |
|
119
|
|
|
if ($attribute === null) { |
|
120
|
|
|
$this->errors = []; |
|
121
|
|
|
} else { |
|
122
|
|
|
unset($this->errors[$attribute]); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|