default value

main
Robert 2 months ago
parent 75a77d7a09
commit 4a70f8b183
  1. 14
      src/classes/attributes/validators/my_validator.php
  2. 32
      src/classes/traits/form_validator.php
  3. 28
      src/classes/validator.php

@ -12,6 +12,11 @@ namespace CodeHydrater\attributes\validators;
use \Attribute;
enum DEFAULT_VALUE {
case do_error_on_null;
case do_not_error_on_null;
}
#[Attribute(Attribute::TARGET_PROPERTY)]
class Positive {}
@ -116,7 +121,7 @@ class my_validator {
* @return void
*/
public function validate(object $object, array $messages = [], $default = null): void {
public function validate(object $object, array $messages = [], DEFAULT_VALUE $default_value = DEFAULT_VALUE::do_not_error_on_null): void {
$handlers = [
Positive::class => function (string $name, $value, $attr, array $messages) {
@ -188,6 +193,13 @@ $handlers = [
];
$default = match($default_value) {
DEFAULT_VALUE::do_not_error_on_null => null,
// Fail validation as user requested false on default
DEFAULT_VALUE::do_error_on_null => false,
default => null, // Pass validation as null or value unset
};
$ref = new \ReflectionObject($object);
foreach ($ref->getProperties() as $property) {

@ -35,9 +35,9 @@ trait form_validator {
*
* @param array $data
* @param string $field
* @param mixed return: null avoids an error on no-entry; false gives errors on no-entry; default is the value
* @param mixed return: null avoids an error on no-entry; false gives errors on no-entry; default is the value. DO NOT MESS with this...
*/
private static function check_if_empty(array $data, string $field) {
private static function check_if_null_or_false(array $data, string $field) {
$d = $data[$field] ?? null;
return match($d) {
null => true, // Pass validation as null or value unset
@ -47,13 +47,13 @@ trait form_validator {
}
private static function is_positive(array $data, string $field): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
return (intval($r) >= 0) ? true : false;
}
private static function is_required(array $data, string $field): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
if (common::get_count($r)) {
@ -68,51 +68,51 @@ trait form_validator {
}
private static function is_email(array $data, string $field): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
return (filter_var($r, FILTER_VALIDATE_EMAIL) === false) ? false : true;
}
private static function is_min(array $data, string $field, string $min): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
return mb_strlen($r) >= intval($min);
}
private static function is_max(array $data, string $field, string $max): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
return mb_strlen($r) <= intval($max);
}
private static function is_greater_than(array $data, string $field, string $min): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
return intval($r) > intval($min);
}
private static function is_less_than(array $data, string $field, string $max): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
return intval($r) < intval($max);
}
private static function is_number_range(array $data, string $field, string $min, string $max): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
$no = intval($r);
return $no >= intval($min) && $no <= intval($max);
}
private static function is_between(array $data, string $field, string $min, string $max): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
$len = mb_strlen($r);
return $len >= intval($min) && $len <= intval($max);
}
private static function is_same(array $data, string $field, string $other): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
if (isset($data[$other])) {
@ -123,13 +123,13 @@ trait form_validator {
}
private static function is_alphanumeric(array $data, string $field): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
return ctype_alnum($r);
}
private static function is_secure(array $data, string $field): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
// Is 8 to 64 CHRs
$pattern = "#.*^(?=.{8,64})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\W).*$#";
@ -137,14 +137,14 @@ trait form_validator {
}
private static function is_valid_email_domain(array $data, string $field): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
$domain = ltrim(stristr($r, '@'), '@') . '.';
return checkdnsrr($domain, 'MX');
}
private static function is_valid_domain(array $data, string $field): bool {
$r = self::check_if_empty($data, $field);
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
return checkdnsrr($r, 'A')
|| checkdnsrr($r, 'AAAA')

@ -9,11 +9,20 @@ declare(strict_types=1);
namespace CodeHydrater;
enum DEFAULT_VALUE {
case do_error_on_null;
case do_not_error_on_null;
}
class validator {
use \CodeHydrater\traits\form_validator;
private static function make_arrays(array $data, $field): array {
private static function make_arrays(
array $data,
$field,
DEFAULT_VALUE $default_value = DEFAULT_VALUE::do_not_error_on_null
): array {
$dataset = [];
if (isset($data[$field])) {
if (common::get_count($data[$field])) {
@ -24,7 +33,14 @@ class validator {
$dataset[] = $data[$field];
}
} else {
$dataset[] = null; // If field is null, force null set
$default = match($default_value) {
DEFAULT_VALUE::do_not_error_on_null => null,
// Fail validation as user requested false on default
DEFAULT_VALUE::do_error_on_null => false,
default => null, // Pass validation as null or value unset
};
$dataset[] = $default; // How to handle nulls
}
return $dataset;
}
@ -32,7 +48,8 @@ class validator {
public static function validate(
array $data,
array $fields,
array $messages = []
array $messages = [],
DEFAULT_VALUE $default_value = DEFAULT_VALUE::do_not_error_on_null
): array {
// Split the array by a separator, trim each element
// and return the array
@ -46,7 +63,7 @@ class validator {
$errors = [];
foreach ($fields as $field => $option) {
foreach(self::make_arrays($data, $field) as $index=>$v) {
foreach(self::make_arrays($data, $field, $default_value) as $index=>$v) {
$data[$field] = $v; // Force update on arrays
$rules = $split($option, '|');
@ -90,7 +107,8 @@ class validator {
$data = ['email'=>'jim@aol.com'];
$fields = ['email' => 'required | email'];
$errors = \CodeHydrater\validator::validate($data, $fields);
$errors = \CodeHydrater\validator::validate($data, $fields, default_value: \CodeHydrater\DEFAULT_VALUE::do_error_on_null);
print_r($errors);
*/

Loading…
Cancel
Save