| 1 |
<?php |
| 2 |
|
| 3 |
namespace Rakit\Validation; |
| 4 |
|
| 5 |
class ErrorBag |
| 6 |
{ |
| 7 |
|
| 8 |
/** @var array */ |
| 9 |
protected $messages = []; |
| 10 |
|
| 11 |
/** |
| 12 |
* Constructor |
| 13 |
* |
| 14 |
* @param array $messages |
| 15 |
* @return void |
| 16 |
*/ |
| 17 |
public function __construct(array $messages = []) |
| 18 |
{ |
| 19 |
$this->messages = $messages; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Add message for given key and rule |
| 24 |
* |
| 25 |
* @param string $key |
| 26 |
* @param string $rule |
| 27 |
* @param string $message |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function add(string $key, string $rule, string $message) |
| 31 |
{ |
| 32 |
if (!isset($this->messages[$key])) { |
| 33 |
$this->messages[$key] = []; |
| 34 |
} |
| 35 |
|
| 36 |
$this->messages[$key][$rule] = $message; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get messages count |
| 41 |
* |
| 42 |
* @return int |
| 43 |
*/ |
| 44 |
public function count(): int |
| 45 |
{ |
| 46 |
return count($this->all()); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Check given key is existed |
| 51 |
* |
| 52 |
* @param string $key |
| 53 |
* @return bool |
| 54 |
*/ |
| 55 |
public function has(string $key): bool |
| 56 |
{ |
| 57 |
list($key, $ruleName) = $this->parsekey($key); |
| 58 |
if ($this->isWildcardKey($key)) { |
| 59 |
$messages = $this->filterMessagesForWildcardKey($key, $ruleName); |
| 60 |
return count(Helper::arrayDot($messages)) > 0; |
| 61 |
} else { |
| 62 |
$messages = isset($this->messages[$key])? $this->messages[$key] : null; |
| 63 |
|
| 64 |
if (!$ruleName) { |
| 65 |
return !empty($messages); |
| 66 |
} else { |
| 67 |
return !empty($messages) and isset($messages[$ruleName]); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get the first value of array |
| 74 |
* |
| 75 |
* @param string $key |
| 76 |
* @return mixed |
| 77 |
*/ |
| 78 |
public function first(string $key) |
| 79 |
{ |
| 80 |
list($key, $ruleName) = $this->parsekey($key); |
| 81 |
if ($this->isWildcardKey($key)) { |
| 82 |
$messages = $this->filterMessagesForWildcardKey($key, $ruleName); |
| 83 |
$flattenMessages = Helper::arrayDot($messages); |
| 84 |
return array_shift($flattenMessages); |
| 85 |
} else { |
| 86 |
$keyMessages = isset($this->messages[$key])? $this->messages[$key] : []; |
| 87 |
|
| 88 |
if (empty($keyMessages)) { |
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
if ($ruleName) { |
| 93 |
return isset($keyMessages[$ruleName])? $keyMessages[$ruleName] : null; |
| 94 |
} else { |
| 95 |
return array_shift($keyMessages); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get messages from given key, can be use custom format |
| 102 |
* |
| 103 |
* @param string $key |
| 104 |
* @param string $format |
| 105 |
* @return array |
| 106 |
*/ |
| 107 |
public function get(string $key, string $format = ':message'): array |
| 108 |
{ |
| 109 |
list($key, $ruleName) = $this->parsekey($key); |
| 110 |
$results = []; |
| 111 |
if ($this->isWildcardKey($key)) { |
| 112 |
$messages = $this->filterMessagesForWildcardKey($key, $ruleName); |
| 113 |
foreach ($messages as $explicitKey => $keyMessages) { |
| 114 |
foreach ($keyMessages as $rule => $message) { |
| 115 |
$results[$explicitKey][$rule] = $this->formatMessage($message, $format); |
| 116 |
} |
| 117 |
} |
| 118 |
} else { |
| 119 |
$keyMessages = isset($this->messages[$key])? $this->messages[$key] : []; |
| 120 |
foreach ($keyMessages as $rule => $message) { |
| 121 |
if ($ruleName and $ruleName != $rule) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
$results[$rule] = $this->formatMessage($message, $format); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $results; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get all messages |
| 133 |
* |
| 134 |
* @param string $format |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
public function all(string $format = ':message'): array |
| 138 |
{ |
| 139 |
$messages = $this->messages; |
| 140 |
$results = []; |
| 141 |
foreach ($messages as $key => $keyMessages) { |
| 142 |
foreach ($keyMessages as $message) { |
| 143 |
$results[] = $this->formatMessage($message, $format); |
| 144 |
} |
| 145 |
} |
| 146 |
return $results; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get the first message from existing keys |
| 151 |
* |
| 152 |
* @param string $format |
| 153 |
* @param boolean $dotNotation |
| 154 |
* @return array |
| 155 |
*/ |
| 156 |
public function firstOfAll(string $format = ':message', bool $dotNotation = false): array |
| 157 |
{ |
| 158 |
$messages = $this->messages; |
| 159 |
$results = []; |
| 160 |
foreach ($messages as $key => $keyMessages) { |
| 161 |
if ($dotNotation) { |
| 162 |
$results[$key] = $this->formatMessage(array_shift($messages[$key]), $format); |
| 163 |
} else { |
| 164 |
Helper::arraySet($results, $key, $this->formatMessage(array_shift($messages[$key]), $format)); |
| 165 |
} |
| 166 |
} |
| 167 |
return $results; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Get plain array messages |
| 172 |
* |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
public function toArray(): array |
| 176 |
{ |
| 177 |
return $this->messages; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Parse $key to get the array of $key and $ruleName |
| 182 |
* |
| 183 |
* @param string $key |
| 184 |
* @return array |
| 185 |
*/ |
| 186 |
protected function parseKey(string $key): array |
| 187 |
{ |
| 188 |
$expl = explode(':', $key, 2); |
| 189 |
$key = $expl[0]; |
| 190 |
$ruleName = isset($expl[1])? $expl[1] : null; |
| 191 |
return [$key, $ruleName]; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Check the $key is wildcard |
| 196 |
* |
| 197 |
* @param mixed $key |
| 198 |
* @return bool |
| 199 |
*/ |
| 200 |
protected function isWildcardKey(string $key): bool |
| 201 |
{ |
| 202 |
return false !== strpos($key, '*'); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Filter messages with wildcard key |
| 207 |
* |
| 208 |
* @param string $key |
| 209 |
* @param mixed $ruleName |
| 210 |
* @return array |
| 211 |
*/ |
| 212 |
protected function filterMessagesForWildcardKey(string $key, $ruleName = null): array |
| 213 |
{ |
| 214 |
$messages = $this->messages; |
| 215 |
$pattern = preg_quote($key, '#'); |
| 216 |
$pattern = str_replace('\*', '.*', $pattern); |
| 217 |
|
| 218 |
$filteredMessages = []; |
| 219 |
|
| 220 |
foreach ($messages as $k => $keyMessages) { |
| 221 |
if ((bool) preg_match('#^'.$pattern.'\z#u', $k) === false) { |
| 222 |
continue; |
| 223 |
} |
| 224 |
|
| 225 |
foreach ($keyMessages as $rule => $message) { |
| 226 |
if ($ruleName and $rule != $ruleName) { |
| 227 |
continue; |
| 228 |
} |
| 229 |
$filteredMessages[$k][$rule] = $message; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return $filteredMessages; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Get formatted message |
| 238 |
* |
| 239 |
* @param string $message |
| 240 |
* @param string $format |
| 241 |
* @return string |
| 242 |
*/ |
| 243 |
protected function formatMessage(string $message, string $format): string |
| 244 |
{ |
| 245 |
return str_replace(':message', $message, $format); |
| 246 |
} |
| 247 |
} |
| 248 |
|