diff --git a/src/MessageErrorBuilder.php b/src/MessageErrorBuilder.php new file mode 100644 index 0000000..2ea36c7 --- /dev/null +++ b/src/MessageErrorBuilder.php @@ -0,0 +1,73 @@ +items = []; + } + + /** + * @param string $value + * @return $this + */ + public function type(string $value): MessageErrorBuilder + { + $this->items['type'] = $value; + return $this; + } + + /** + * @param string $value + * @return $this + */ + public function message(string $value): MessageErrorBuilder + { + $this->items['message'] = $value; + return $this; + } + + /** + * @param string $value + * @return $this + */ + public function label(string $value): MessageErrorBuilder + { + $this->items['label'] = $value; + return $this; + } + + /** + * @param string $value + * @return $this + */ + public function limit(string $value): MessageErrorBuilder + { + $this->items['limit'] = $value; + return $this; + } + + /** + * @return array + */ + public function generate(){ + return $this->items; + } +} \ No newline at end of file diff --git a/src/Providers/ValidatorProvider.php b/src/Providers/ValidatorProvider.php index d05fa9b..4b22a31 100644 --- a/src/Providers/ValidatorProvider.php +++ b/src/Providers/ValidatorProvider.php @@ -6,6 +6,7 @@ namespace Wepesi\App\Providers; +use Wepesi\App\MessageErrorBuilder; use Wepesi\App\Providers\Contracts\Contracts; /** @@ -29,13 +30,18 @@ abstract class ValidatorProvider implements Contracts * @var */ protected $field_value; - + /** + * @var MessageErrorBuilder + */ + protected MessageErrorBuilder $messageItem; /** * */ + function __construct() { $this->errors = []; + $this->messageItem = new MessageErrorBuilder(); } /** @@ -70,22 +76,20 @@ public function required() { if (is_array($this->field_value)) { if (count($this->field_value) == 0) { - $message = [ - 'type' => $this->getClassProvider() . ' required', - 'message' => "'$this->field_name' is required", - 'label' => $this->field_name, - ]; - $this->addError($message); + $this->messageItem + ->type($this->getClassProvider() . ' required') + ->label($this->field_name) + ->message("'$this->field_name' is required"); + $this->addError($this->messageItem); } } else { $required_value = trim($this->field_value); if (strlen($required_value) == 0) { - $message = [ - 'type' => $this->classProvider() . ' required', - 'message' => "'$this->field_name' is required", - 'label' => $this->field_name, - ]; - $this->addError($message); + $this->messageItem + ->type($this->classProvider() . ' required') + ->message("'$this->field_name' is required") + ->label($this->field_name); + $this->addError($this->messageItem); } } } @@ -95,9 +99,9 @@ public function required() * @param array $value * @return void */ - public function addError(array $value): void + public function addError(MessageErrorBuilder $item): void { - $this->errors[] = $value; + $this->errors[] = $item->generate(); } /** @@ -118,12 +122,11 @@ protected function positiveParamMethod(int $rule, bool $max = false): bool $status = true; if ($rule < 1) { $method = $max ? "max" : "min"; - $message = [ - 'type' => $this->getClassProvider() . ' method ' . $method, - 'message' => "'$this->field_name' $method param should be a positive number", - 'label' => $this->field_name, - ]; - $this->addError($message); + $this->messageItem + ->type($this->getClassProvider() . ' method ' . $method) + ->message("'$this->field_name' $method param should be a positive number") + ->label($this->field_name); + $this->addError($this->messageItem); $status = false; } return $status; diff --git a/src/Validate.php b/src/Validate.php index 8f6f825..075108e 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -21,7 +21,10 @@ class Validate * @var bool */ private bool $passed; - + /** + * @var MessageErrorBuilder + */ + private MessageErrorBuilder $message; /** * */ @@ -29,6 +32,7 @@ function __construct() { $this->errors = []; $this->passed = false; + $this->message = new MessageErrorBuilder(); } /** @@ -53,11 +57,10 @@ function check(array $resource, array $schema) $exceptions = isset($options['exception']) || isset($options['InvalidArgumentException']) ?? false; if ($exceptions) { - $message = [ - 'type' => 'object.unknown', - 'message' => $options['exception'] ?? $options['InvalidArgumentException'], - 'label' => "exception", - ]; + $this->message + ->type('object.unknown') + ->message($options['exception'] ?? $options['InvalidArgumentException']) + ->label("exception"); $this->addError($message); } else { foreach ($schema as $item => $rules) { @@ -97,9 +100,9 @@ function check(array $resource, array $schema) * @param array $message * @return void */ - private function addError(array $message) + private function addError(MessageErrorBuilder $item) { - $this->errors[] = $message; + $this->errors[] = $item->generate(); } /** diff --git a/src/Validator/ArrayValidator.php b/src/Validator/ArrayValidator.php index c28a1c5..532d236 100644 --- a/src/Validator/ArrayValidator.php +++ b/src/Validator/ArrayValidator.php @@ -37,13 +37,12 @@ public function min(int $rule): void // TODO: Implement min() method. if ($this->positiveParamMethod($rule)) return; if (count($this->field_value) < $rule) { - $message = [ - 'type' => 'array.min', - 'message' => "`$this->field_name` should have a minimum of `$rule` elements", - 'label' => $this->field_name, - 'limit' => $rule - ]; - $this->addError($message); + $this->messageItem + ->type('array.min') + ->message("`$this->field_name` should have a minimum of `$rule` elements") + ->label($this->field_name) + ->limit($rule); + $this->addError($this->messageItem); } } @@ -56,13 +55,12 @@ public function max(int $rule): void // TODO: Implement max() method. if ($this->positiveParamMethod($rule, true)) return; if (count($this->field_value) > $rule) { - $message = [ - 'type' => 'array.max', - 'message' => "`$this->field_name` should have a maximum of `$rule` elements", - 'label' => $this->field_name, - 'limit' => $rule - ]; - $this->addError($message); + $this->messageItem + ->type('array.max') + ->message("`$this->field_name` should have a maximum of `$rule` elements") + ->label($this->field_name) + ->limit($rule); + $this->addError($this->messageItem); } } @@ -96,13 +94,12 @@ public function string(): void $keys = array_keys($filter); for ($i = 0; $i < count($keys); $i++) { $position = $keys[$i]; - $message = [ - 'type' => 'array.string', - 'message' => "`$this->field_name[$position]` should be a string", - 'label' => $this->field_name, - 'limit' => 1 - ]; - $this->addError($message); + $this->messageItem + ->type('array.string') + ->message("`$this->field_name[$position]` should be a string") + ->label($this->field_name) + ->limit(1); + $this->addError($this->messageItem); } } } @@ -123,13 +120,12 @@ public function number(): void $keys = array_keys($filter); for ($i = 0; $i < count($keys); $i++) { $position = $keys[$i]; - $message = [ - 'type' => 'array.number', - 'message' => "`$this->field_name[$position]` should be a number", - 'label' => $this->field_name, - 'limit' => count($keys) - ]; - $this->addError($message); + $this->messageItem + ->type('array.number') + ->message("`$this->field_name[$position]` should be a number") + ->label($this->field_name) + ->limit(count($keys)); + $this->addError($this->messageItem); } } } diff --git a/src/Validator/BooleanValidator.php b/src/Validator/BooleanValidator.php index 773b7f5..875c441 100644 --- a/src/Validator/BooleanValidator.php +++ b/src/Validator/BooleanValidator.php @@ -66,12 +66,11 @@ private function isBoolean(string $itemKey = null): bool $regex = "/^(true|false)$/"; if (!preg_match($regex, is_bool($val) ? ($val ? 'true' : 'false') : $val)) { - $message = [ - 'type' => 'boolean.unknown', - 'message' => "`$item_to_check` should be a boolean", - 'label' => $item_to_check, - ]; - $this->addError($message); + $this->messageItem + ->type('boolean.unknown') + ->message("`$item_to_check` should be a boolean") + ->label($item_to_check); + $this->addError($this->messageItem); return false; } return true; @@ -87,23 +86,21 @@ public function isValid(string $value) $required_value = strtolower($passed_value); $check = $required_value == 'true' || $required_value == 'false'; if (!($check)) { - $message = [ - 'type' => 'boolean.required', - 'message' => "isValid param must be boolean but you put `$required_value`", - 'label' => $this->field_name, - ]; - $this->addError($message); + $this->messageItem + ->type('boolean.required') + ->message("isValid param must be boolean but you put `$required_value`") + ->label($this->field_name); + $this->addError($this->messageItem); } else { // $sting_value= is_bool($this->string_value); $incoming_value = $this->field_value ? 'true' : 'false'; if ($incoming_value != $required_value) { - $message = [ - 'type' => 'boolean.valid', - 'message' => "`$incoming_value` is not validValue required. You must put `$required_value`", - 'label' => $this->field_name, - ]; - $this->addError($message); + $this->messageItem + ->type('boolean.valid') + ->message("`$incoming_value` is not validValue required. You must put `$required_value`") + ->label($this->field_name); + $this->addError($this->messageItem); } } } diff --git a/src/Validator/DateValidator.php b/src/Validator/DateValidator.php index fe9d767..61e2531 100644 --- a/src/Validator/DateValidator.php +++ b/src/Validator/DateValidator.php @@ -39,13 +39,12 @@ public function now() $min_date = date('d/F/Y', $min_date_time); $date_value_time = strtotime($this->field_value); if ($date_value_time < $min_date_time) { - $message = [ - 'type' => 'date.now', - 'message' => "`$this->field_name` should be greater than now", - 'label' => $this->field_name, - 'limit' => $min_date - ]; - $this->addError($message); + $this->messageItem + ->type('date.now') + ->message("`$this->field_name` should be greater than now") + ->label($this->field_name) + ->limit($min_date); + $this->addError($this->messageItem); } } @@ -60,13 +59,12 @@ public function today(string $times = null) $min_date = date('d/F/Y', $min_date_time); $date_value_time = strtotime($this->field_value); if ($date_value_time > $min_date_time) { - $message = [ - 'type' => 'date.now', - 'message' => "`$this->field_name` should be greater than today ", - 'label' => $this->field_name, - 'limit' => $min_date - ]; - $this->addError($message); + $this->messageItem + ->type('date.now') + ->message("`$this->field_name` should be greater than today ") + ->label($this->field_name) + ->limit($min_date); + $this->addError($this->messageItem); } } @@ -88,13 +86,12 @@ public function min($rule) $min_date = date('d/F/Y', $min_date_time); $date_value_time = strtotime($this->field_value); if ($date_value_time > $min_date_time) { - $message = [ - 'type' => 'date.min', - 'message' => "`$this->field_name` should be greater than `$min_date`", - 'label' => $this->field_name, - 'limit' => $min_date - ]; - $this->addError($message); + $this->messageItem + ->type('date.min') + ->message("`$this->field_name` should be greater than `$min_date`") + ->label($this->field_name) + ->limit($min_date); + $this->addError($this->messageItem); } } @@ -110,13 +107,12 @@ public function max($rule) $max_date = date('d/F/Y', $max_date_time); $date_value_time = strtotime($this->field_value); if ($max_date_time < $date_value_time) { - $message = [ - 'type' => 'date.max', - 'message' => "`$this->field_name` should be less than `$max_date`", - 'label' => $this->field_name, - 'limit' => $max_date - ]; - $this->addError($message); + $this->messageItem + ->type('date.max') + ->message("`$this->field_name` should be less than `$max_date`") + ->label($this->field_name) + ->limit($max_date); + $this->addError($this->messageItem); } } @@ -130,19 +126,17 @@ protected function checkExist(string $itemKey = null): void $regex = '#[a-zA-Z0-9]#'; $this->_errors = []; if (!isset($this->data_source[$item_to_check])) { - $message = [ - 'type' => 'any.unknown', - 'message' => "`$item_to_check` is unknown", - 'label' => $item_to_check, - ]; - $this->addError($message); + $this->messageItem + ->type('any.unknown') + ->message("`$item_to_check` is unknown") + ->label($item_to_check); + $this->addError($this->messageItem); } else if (!preg_match($regex, $this->data_source[$item_to_check]) || strlen(trim($this->data_source[$item_to_check])) == 0) { - $message = [ - 'type' => 'date.unknown', - 'message' => "`$item_to_check` should be a date.", - 'label' => $item_to_check, - ]; - $this->addError($message); + $this->messageItem + ->type('date.unknown') + ->message("`$item_to_check` should be a date.") + ->label($item_to_check); + $this->addError($this->messageItem); } } diff --git a/src/Validator/NumberValidator.php b/src/Validator/NumberValidator.php index 9332bef..95f67f3 100644 --- a/src/Validator/NumberValidator.php +++ b/src/Validator/NumberValidator.php @@ -26,7 +26,7 @@ final class NumberValidator extends ValidatorProvider * @param string $item * @param array $data_source */ - function __construct(string $item, array $data_source) + public function __construct(string $item, array $data_source) { $this->data_source = $data_source; $this->field_name = $item; @@ -41,17 +41,16 @@ function __construct(string $item, array $data_source) * @param int $rule * @return void */ - function min(int $rule) + public function min(int $rule) { if ($this->positiveParamMethod($rule)) return; if ((int)$this->field_value < $rule) { - $message = [ - 'type' => 'number.min', - 'message' => "`$this->field_name` should be greater than `$rule`", - 'label' => $this->field_name, - 'limit' => $rule - ]; - $this->addError($message); + $this->messageItem + ->type('number.min') + ->message("`$this->field_name` should be greater than `$rule`") + ->label($this->field_name) + ->limit($rule); + $this->addError($this->messageItem); } } @@ -59,33 +58,31 @@ function min(int $rule) * @param int $rule * @return void */ - function max(int $rule) + public function max(int $rule) { if ($this->positiveParamMethod($rule, true)) return; if ((int)$this->field_value > $rule) { - $message = [ - 'type' => 'number.max', - 'message' => "`$this->field_name` should be less than `$rule`", - 'label' => $this->field_name, - 'limit' => $rule - ]; - $this->addError($message); + $this->messageItem + ->type('number.max') + ->message("`$this->field_name` should be less than `$rule`") + ->label($this->field_name) + ->limit($rule); + $this->addError($this->messageItem); } } /** * */ - function positive() + public function positive() { if ((int)$this->field_value < 0) { - $message = [ - 'type' => 'number.positive', - 'message' => "`$this->field_name` should be a positive number", - 'label' => $this->field_name, - 'limit' => 1 - ]; - $this->addError($message); + $this->messageItem + ->type('number.positive') + ->message("`$this->field_name` should be a positive number") + ->label($this->field_name) + ->limit(1); + $this->addError($this->messageItem); } } @@ -96,12 +93,11 @@ protected function isNumber(): bool { $regex_string = '#[a-zA-Z]#'; if (preg_match($regex_string, trim($this->data_source[$this->field_name])) || !is_integer($this->data_source[$this->field_name])) { - $message = [ - 'type' => 'number.unknown', - 'message' => "`$this->field_name` should be a number", - 'label' => $this->field_name, - ]; - $this->addError($message); + $this->messageItem + ->type('number.unknown') + ->message("`$this->field_name` should be a number") + ->label($this->field_name); + $this->addError($this->messageItem); return false; } return true; diff --git a/src/Validator/StringValidator.php b/src/Validator/StringValidator.php index 75c2fe8..71941e2 100644 --- a/src/Validator/StringValidator.php +++ b/src/Validator/StringValidator.php @@ -38,13 +38,12 @@ public function min(int $rule): void { if ($this->positiveParamMethod($rule)) return; if (strlen($this->field_value) < $rule) { - $message = [ - 'type' => 'string.min', - 'message' => "`$this->field_name` should have minimum of `$rule` characters", - 'label' => $this->field_name, - 'limit' => $rule - ]; - $this->addError($message); + $this->messageItem + ->type('string.min') + ->message("`$this->field_name` should have minimum of `$rule` characters") + ->label($this->field_name) + ->limit($rule); + $this->addError($this->messageItem); } } @@ -57,13 +56,12 @@ public function max(int $rule): void { if ($this->positiveParamMethod($rule, true)) return; if (strlen($this->field_value) > $rule) { - $message = [ - 'type' => 'string.max', - 'message' => "`$this->field_name` should have maximum of `$rule` characters", - 'label' => $this->field_name, - 'limit' => $rule - ]; - $this->addError($message); + $this->messageItem + ->type('string.max') + ->message("`$this->field_name` should have maximum of `$rule` characters") + ->label($this->field_name) + ->limit($rule); + $this->addError($this->messageItem); } } @@ -73,12 +71,11 @@ public function max(int $rule): void public function email() { if (!filter_var($this->field_value, FILTER_VALIDATE_EMAIL)) { - $message = [ - 'type' => 'string.email', - 'message' => ("`$this->field_name` should be an email."), - 'label' => $this->field_name, - ]; - $this->addError($message); + $this->messageItem + ->type('string.email') + ->message("`$this->field_name` should be an email.") + ->label($this->field_name); + $this->addError($this->messageItem); } } @@ -92,12 +89,11 @@ public function email() public function url() { if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->field_value)) { - $message = [ - 'type' => 'string.url', - 'message' => ("'$this->field_name' this should be a link(url)"), - 'label' => $this->field_name, - ]; - $this->addError($message); + $this->messageItem + ->type('string.url') + ->message("'$this->field_name' this should be a link(url)") + ->label($this->field_name); + $this->addError($this->messageItem); } } @@ -110,12 +106,11 @@ public function match(string $key_to_match) { $this->isStringAndValid($key_to_match); if (isset($this->data_source[$key_to_match]) && (strlen($this->field_value) != strlen($this->data_source[$key_to_match])) && ($this->field_value != $this->data_source[$key_to_match])) { - $message = [ - 'type' => 'string.match', - 'message' => "`$this->field_name` should match `$key_to_match`", - 'label' => $this->field_name, - ]; - $this->addError($message); + $this->messageItem + ->type('string.match') + ->message("`$this->field_name` should match `$key_to_match`") + ->label($this->field_name); + $this->addError($this->messageItem); } } @@ -126,12 +121,11 @@ public function match(string $key_to_match) public function addressIp() { if (!filter_var($this->field_value, FILTER_VALIDATE_IP)) { - $message = [ - 'type' => 'string.ip_address', - 'message' => "`$this->field_name` is not a valid Ip address", - 'label' => $this->field_name, - ]; - $this->addError($message); + $this->messageItem + ->type('string.ip_address') + ->message("`$this->field_name` is not a valid Ip address") + ->label($this->field_name); + $this->addError($this->messageItem); } } @@ -142,12 +136,11 @@ public function addressIp() public function addressIpv6(string $ip_address) { if (!filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - $message = [ - 'type' => 'string.ip_address_v6', - 'message' => "`$this->field_name` is not a valid ip address (ipv6)", - 'label' => $this->field_name, - ]; - $this->addError($message); + $this->messageItem + ->type('string.ip_address_v6') + ->message("`$this->field_name` is not a valid ip address (ipv6)") + ->label($this->field_name); + $this->addError($this->messageItem); } } @@ -161,19 +154,17 @@ protected function isStringAndValid(string $item_key): void $field_to_check = !$item_key ? $this->field_name : $item_key; $regex = '#[a-zA-Z0-9]#'; if (!isset($this->data_source[$field_to_check])) { - $message = [ - 'type' => 'string.unknown', - 'message' => ("`$field_to_check` is not valid"), - 'label' => $field_to_check, - ]; - $this->addError($message); + $this->messageItem + ->type('string.unknown') + ->message("`$field_to_check` is not valid") + ->label($field_to_check); + $this->addError($this->messageItem); } else if (!preg_match($regex, $this->data_source[$field_to_check]) || strlen(trim($this->field_value)) == 0) { - $message = [ - 'type' => 'string.unknown', - 'message' => ("`$field_to_check` should be a string"), - 'label' => $field_to_check, - ]; - $this->addError($message); + $this->messageItem + ->type('string.unknown') + ->message("`$field_to_check` should be a string") + ->label($field_to_check); + $this->addError($this->messageItem); } }