From 0947d8ddf100b3139486e3c1e5cc5b134b58c137 Mon Sep 17 00:00:00 2001 From: bim-g Date: Mon, 24 Jul 2023 23:16:23 +0200 Subject: [PATCH] [FIX] Refactoring application test. --- .phpunit.result.cache | 2 +- index.php | 14 +- .../Contracts/ValidatorContracts.php | 2 +- src/Providers/SChemaProvider.php | 2 +- src/Providers/ValidatorProvider.php | 45 ++- src/Resolver/Option.php | 12 +- src/Resolver/OptionsResolver.php | 31 +- src/Schema.php | 5 +- src/Schema/ArraySchema.php | 17 +- src/Schema/DateSchema.php | 8 +- src/Schema/NumberSchema.php | 6 +- src/Schema/StringSchema.php | 20 +- src/Traits/InitTrait.php | 13 +- src/Validate.php | 32 +- src/Validator/ArrayValidator.php | 47 ++- src/Validator/BooleanValidator.php | 43 ++- src/Validator/DateValidator.php | 92 ++--- src/Validator/NumberValidator.php | 61 +-- src/Validator/StringValidator.php | 87 +++-- test/Schema/BooleanSchemaTest.php | 23 +- test/Schema/DateSchemaTest.php | 23 +- test/Schema/NumberSchemaTest.php | 10 +- test/Schema/StringSchemaTest.php | 34 +- test/SchemaTest.php | 143 +++++--- test/Script/StringValidationTest.php | 346 ------------------ 25 files changed, 474 insertions(+), 644 deletions(-) delete mode 100644 test/Script/StringValidationTest.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache index 32ef5a9..3ba092a 100644 --- a/.phpunit.result.cache +++ b/.phpunit.result.cache @@ -1 +1 @@ -{"version":1,"defects":{"Test\\Schema\\SCHStringTest::testStringObject":4,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":3,"Test\\Schema\\SCHStringTest::testStringEmailKey":3,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":4,"Test\\Schema\\StringSchemaTest::testStringURLKey":3,"Test\\Schema\\StringSchemaTest::testStringMatchKey":3,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":3,"Test\\Schema\\NumberSchemaTest::testStringIsObject":4,"Test\\Schema\\NumberSchemaTest::testRequiredKey":3,"Test\\Schema\\DateSchemaTest::testDateTodayKey":3,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":3,"Test\\Script\\StringValidationTest::testStringIsObject":4,"Test\\Script\\StringValidationTest::testStringObjectException":4,"Test\\Script\\StringValidationTest::testException":3,"Test\\Script\\StringValidationTest::testStringMin":3,"Test\\Script\\StringValidationTest::testStringMaxError":4,"Test\\Script\\StringValidationTest::testStringMaxSuccess":4,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":4,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":4,"Test\\Script\\StringValidationTest::testStringMinError":4,"Test\\Script\\StringValidationTest::testStringMinSuccess":4,"Test\\Script\\StringValidationTest::testStringEmailError":4,"Test\\Script\\StringValidationTest::testStringURLSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredErrors":4,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":4,"Test\\Script\\StringValidationTest::testStringMatchSuccess":4,"Test\\Script\\StringValidationTest::testStringMatchErrors":4,"Test\\Schema\\StringSchemaTest::testStringIsObject":4,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\StringSchemaTest::testStringEmailKey":3,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":3,"Test\\Schema\\DateSchemaTest::testStringIsObject":4,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\DateSchemaTest::testDateNowKey":3,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":3,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":4,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":3,"Test\\ValidationTest::testValidationStringObject":3,"Test\\ValidationTest::testValidationStringErrorObject":3,"Test\\Script\\StringValidationTest::testStringValidationFieldNotDefinedException":4,"Test\\Script\\StringValidationTest::testStringEmailSuccess":4,"Test\\Script\\StringValidationTest::testStringURLError":4,"Test\\SchemaTest::testSchemaStringObject":3,"Test\\SchemaTest::testSchemaNumberObject":3,"Test\\SchemaTest::testSchemaDateObject":3},"times":{"Test\\Schema\\SCHStringTest::testStringObject":0.021,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":0,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":0.01,"Test\\Schema\\SCHStringTest::testStringEmailKey":0.001,"Test\\Schema\\SCHStringTest::testStringIsObject":0.008,"Test\\Schema\\StringSchemaTest::testStringIsObject":0.007,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":0.001,"Test\\Schema\\StringSchemaTest::testStringEmailKey":0.001,"Test\\Schema\\StringSchemaTest::testStringURLKey":0,"Test\\Schema\\StringSchemaTest::testStringMatchKey":0,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":0,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":0,"Test\\Schema\\NumberSchemaTest::testStringIsObject":0.007,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\NumberSchemaTest::testRequiredKey":0.001,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":0,"Test\\Schema\\DateSchemaTest::testStringIsObject":0.009,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":0.008,"Test\\Schema\\DateSchemaTest::testDateNowKey":0.001,"Test\\Schema\\DateSchemaTest::testDateTodayKey":0,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":0.007,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":0.001,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":0.001,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":0,"Test\\Script\\StringValidationTest::testStringIsObject":0.001,"Test\\Script\\StringValidationTest::testStringObjectException":0.006,"Test\\Script\\StringValidationTest::testException":0.007,"Test\\Script\\StringValidationTest::testStringMin":0.001,"Test\\Script\\StringValidationTest::testStringValidationException":0.006,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":0.001,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":0.076,"Test\\Script\\StringValidationTest::testStringMinError":0.001,"Test\\Script\\StringValidationTest::testStringMinSuccess":0.001,"Test\\Script\\StringValidationTest::testStringMaxError":0.001,"Test\\Script\\StringValidationTest::testStringMaxSuccess":0.001,"Test\\Script\\StringValidationTest::testStringEmailError":0.001,"Test\\Script\\StringValidationTest::testStringEmailSuccess":0.001,"Test\\Script\\StringValidationTest::testStringUrlError":0.008,"Test\\Script\\StringValidationTest::testStringURLError":0.001,"Test\\Script\\StringValidationTest::testStringURLSuccess":0.001,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":0.001,"Test\\Script\\StringValidationTest::testStringRequiredErrors":0.001,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknow":0,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":0.001,"Test\\Script\\StringValidationTest::testStringMatchErrors":0.001,"Test\\Script\\StringValidationTest::testStringMatchSuccess":0.001,"Test\\Script\\StringValidationTest::testStringValidationFieldNotDefinedException":0.001,"Test\\ValidationTest::testValidationObject":0.016,"Test\\ValidationTest::testValidationStringObject":0.005,"Test\\ValidationTest::testValidationStringErrorObject":0.262,"Test\\SchemaTest::testSchemaObject":0.008,"Test\\SchemaTest::testSchemaStringObject":0.002,"Test\\SchemaTest::testSchemaStringErrorObject":0.003,"Test\\SchemaTest::testSchemaNumberObject":0.007,"Test\\SchemaTest::testSchemaNumberErrorObject":0.011,"Test\\SchemaTest::testSchemaDateErrorObject":0.012,"Test\\SchemaTest::testSchemaDateObject":0.007}} \ No newline at end of file +{"version":1,"defects":{"Test\\Schema\\SCHStringTest::testStringObject":4,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":3,"Test\\Schema\\SCHStringTest::testStringEmailKey":3,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":4,"Test\\Schema\\StringSchemaTest::testStringURLKey":3,"Test\\Schema\\StringSchemaTest::testStringMatchKey":3,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":3,"Test\\Schema\\NumberSchemaTest::testStringIsObject":4,"Test\\Schema\\NumberSchemaTest::testRequiredKey":3,"Test\\Schema\\DateSchemaTest::testDateTodayKey":3,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":3,"Test\\Script\\StringValidationTest::testStringIsObject":4,"Test\\Script\\StringValidationTest::testStringObjectException":4,"Test\\Script\\StringValidationTest::testException":3,"Test\\Script\\StringValidationTest::testStringMin":3,"Test\\Script\\StringValidationTest::testStringMaxError":4,"Test\\Script\\StringValidationTest::testStringMaxSuccess":4,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":3,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":4,"Test\\Script\\StringValidationTest::testStringMinError":4,"Test\\Script\\StringValidationTest::testStringMinSuccess":4,"Test\\Script\\StringValidationTest::testStringEmailError":4,"Test\\Script\\StringValidationTest::testStringURLSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredErrors":4,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":4,"Test\\Script\\StringValidationTest::testStringMatchSuccess":4,"Test\\Script\\StringValidationTest::testStringMatchErrors":4,"Test\\Schema\\StringSchemaTest::testStringIsObject":4,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\StringSchemaTest::testStringEmailKey":3,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":3,"Test\\Schema\\DateSchemaTest::testStringIsObject":4,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\DateSchemaTest::testDateNowKey":3,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":3,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":4,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":3,"Test\\ValidationTest::testValidationStringObject":3,"Test\\ValidationTest::testValidationStringErrorObject":3,"Test\\Script\\StringValidationTest::testStringValidationFieldNotDefinedException":3,"Test\\Script\\StringValidationTest::testStringEmailSuccess":4,"Test\\Script\\StringValidationTest::testStringURLError":4,"Test\\SchemaTest::testSchemaStringObject":3,"Test\\SchemaTest::testSchemaNumberObject":3,"Test\\SchemaTest::testSchemaDateObject":3},"times":{"Test\\Schema\\SCHStringTest::testStringObject":0.021,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":0,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":0.01,"Test\\Schema\\SCHStringTest::testStringEmailKey":0.001,"Test\\Schema\\SCHStringTest::testStringIsObject":0.008,"Test\\Schema\\StringSchemaTest::testStringIsObject":0.017,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":0.001,"Test\\Schema\\StringSchemaTest::testStringEmailKey":0.002,"Test\\Schema\\StringSchemaTest::testStringURLKey":0,"Test\\Schema\\StringSchemaTest::testStringMatchKey":0,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":0,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":0,"Test\\Schema\\NumberSchemaTest::testStringIsObject":0.02,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":0.001,"Test\\Schema\\NumberSchemaTest::testRequiredKey":0.001,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":0,"Test\\Schema\\DateSchemaTest::testStringIsObject":0.015,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":0.001,"Test\\Schema\\DateSchemaTest::testDateNowKey":0.001,"Test\\Schema\\DateSchemaTest::testDateTodayKey":0.001,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":0.012,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":0.001,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":0.002,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":0,"Test\\Script\\StringValidationTest::testStringIsObject":0.002,"Test\\Script\\StringValidationTest::testStringObjectException":0.006,"Test\\Script\\StringValidationTest::testException":0.007,"Test\\Script\\StringValidationTest::testStringMin":0.001,"Test\\Script\\StringValidationTest::testStringValidationException":0.006,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":0.001,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":0.013,"Test\\Script\\StringValidationTest::testStringMinError":0.001,"Test\\Script\\StringValidationTest::testStringMinSuccess":0.003,"Test\\Script\\StringValidationTest::testStringMaxError":0.002,"Test\\Script\\StringValidationTest::testStringMaxSuccess":0.002,"Test\\Script\\StringValidationTest::testStringEmailError":0.001,"Test\\Script\\StringValidationTest::testStringEmailSuccess":0.002,"Test\\Script\\StringValidationTest::testStringUrlError":0.008,"Test\\Script\\StringValidationTest::testStringURLError":0.002,"Test\\Script\\StringValidationTest::testStringURLSuccess":0.002,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":0.002,"Test\\Script\\StringValidationTest::testStringRequiredErrors":0.002,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknow":0,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":0.002,"Test\\Script\\StringValidationTest::testStringMatchErrors":0.002,"Test\\Script\\StringValidationTest::testStringMatchSuccess":0.003,"Test\\Script\\StringValidationTest::testStringValidationFieldNotDefinedException":0.01,"Test\\ValidationTest::testValidationObject":0.016,"Test\\ValidationTest::testValidationStringObject":0.005,"Test\\ValidationTest::testValidationStringErrorObject":0.262,"Test\\SchemaTest::testSchemaObject":0.012,"Test\\SchemaTest::testSchemaStringObject":0.003,"Test\\SchemaTest::testSchemaStringErrorObject":0.004,"Test\\SchemaTest::testSchemaNumberObject":0.001,"Test\\SchemaTest::testSchemaNumberErrorObject":0.002,"Test\\SchemaTest::testSchemaDateErrorObject":0.001,"Test\\SchemaTest::testSchemaDateObject":0.002}} \ No newline at end of file diff --git a/index.php b/index.php index 35196b1..6f28ca5 100644 --- a/index.php +++ b/index.php @@ -4,5 +4,17 @@ * @author Boss Ibrahim Mussa */ +use Wepesi\App\Validator\StringValidator; + require_once __DIR__."/vendor/autoload.php"; -include __DIR__.'/example/index.php'; \ No newline at end of file +//include __DIR__.'/example/index.php'; +try { + $stringValidationSourceDataException = new StringValidator(''); +} catch (Exception $ex) { + $className = get_class($ex); + $msg = $ex->getMessage(); + $code = $ex->getCode(); + var_dump($className, + $msg, + $code); +} diff --git a/src/Providers/Contracts/ValidatorContracts.php b/src/Providers/Contracts/ValidatorContracts.php index 0d6edc1..3c891f7 100644 --- a/src/Providers/Contracts/ValidatorContracts.php +++ b/src/Providers/Contracts/ValidatorContracts.php @@ -21,5 +21,5 @@ public function addError(array $value); /** * @return array */ - public function result():array; + public function result(): array; } \ No newline at end of file diff --git a/src/Providers/SChemaProvider.php b/src/Providers/SChemaProvider.php index 3253941..dc986d6 100644 --- a/src/Providers/SChemaProvider.php +++ b/src/Providers/SChemaProvider.php @@ -65,6 +65,6 @@ public function required(): SChemaProvider */ public function generate(): array { - return $this->schema; + return $this->schema; } } \ No newline at end of file diff --git a/src/Providers/ValidatorProvider.php b/src/Providers/ValidatorProvider.php index 5429a1a..d05fa9b 100644 --- a/src/Providers/ValidatorProvider.php +++ b/src/Providers/ValidatorProvider.php @@ -9,7 +9,7 @@ use Wepesi\App\Providers\Contracts\Contracts; /** - * + * Validator provider model */ abstract class ValidatorProvider implements Contracts { @@ -29,15 +29,12 @@ abstract class ValidatorProvider implements Contracts * @var */ protected $field_value; - /** - * @var string - */ - protected string $class_provider = 'unknown'; /** * */ - function __construct(){ + function __construct() + { $this->errors = []; } @@ -53,24 +50,38 @@ abstract public function min(int $rule); */ abstract public function max(int $rule); + /** + * Provide validation module name + * @return string + */ + abstract protected function classProvider(): string ; + + /** + * @return string + */ + private function getClassProvider(): string + { + return $this->classProvider && strlen($this->classProvider) > 0 ? $this->classProvider : 'unknown'; + } /** * @return void */ - public function required(){ - if(is_array($this->field_value)){ + public function required() + { + if (is_array($this->field_value)) { if (count($this->field_value) == 0) { $message = [ - 'type' => $this->class_provider . ' required', + 'type' => $this->getClassProvider() . ' required', 'message' => "'$this->field_name' is required", 'label' => $this->field_name, ]; $this->addError($message); } - }else{ + } else { $required_value = trim($this->field_value); if (strlen($required_value) == 0) { $message = [ - 'type' => $this->class_provider . ' required', + 'type' => $this->classProvider() . ' required', 'message' => "'$this->field_name' is required", 'label' => $this->field_name, ]; @@ -78,6 +89,7 @@ public function required(){ } } } + /** * * @param array $value @@ -93,7 +105,7 @@ public function addError(array $value): void */ public function result(): array { - return $this->errors; + return $this->errors; } /** @@ -101,12 +113,13 @@ public function result(): array * @param bool $max * @return bool */ - protected function positiveParamMethod(int $rule,bool $max = false):bool{ + protected function positiveParamMethod(int $rule, bool $max = false): bool + { $status = true; - if($rule<1){ - $method = $max?"max":"min"; + if ($rule < 1) { + $method = $max ? "max" : "min"; $message = [ - 'type' => $this->class_provider . ' method '. $method, + 'type' => $this->getClassProvider() . ' method ' . $method, 'message' => "'$this->field_name' $method param should be a positive number", 'label' => $this->field_name, ]; diff --git a/src/Resolver/Option.php b/src/Resolver/Option.php index e94f567..56b2b87 100644 --- a/src/Resolver/Option.php +++ b/src/Resolver/Option.php @@ -6,6 +6,8 @@ namespace Wepesi\App\Resolver; +use Closure; + /** * Class Option * @package Wepesi\App\Resolver @@ -28,9 +30,9 @@ final class Option private bool $hasDefaultValue; /** - * @var \Closure|null + * @var Closure|null */ - private ?\Closure $validator = null; + private ?Closure $validator = null; /** * Option constructor. @@ -78,10 +80,10 @@ public function hasDefaultValue(): bool } /** - * @param \Closure $closure + * @param Closure $closure * @return $this */ - public function validator(\Closure $closure): self + public function validator(Closure $closure): self { $this->validator = $closure; return $this; @@ -93,7 +95,7 @@ public function validator(\Closure $closure): self */ public function isValid($value): bool { - if ($this->validator instanceof \Closure) { + if ($this->validator instanceof Closure) { $validator = $this->validator; return $validator($value); } diff --git a/src/Resolver/OptionsResolver.php b/src/Resolver/OptionsResolver.php index d71659c..f20c005 100644 --- a/src/Resolver/OptionsResolver.php +++ b/src/Resolver/OptionsResolver.php @@ -6,7 +6,14 @@ namespace Wepesi\App\Resolver; +use ArrayObject; +use Exception; +use InvalidArgumentException; use Wepesi\App\Traits\ExceptionTraits; +use function array_key_exists; +use function get_class; +use function gettype; +use function sprintf; /** * Class OptionsResolver @@ -15,9 +22,9 @@ final class OptionsResolver { /** - * @var \ArrayObject + * @var ArrayObject */ - private \ArrayObject $options; + private ArrayObject $options; use ExceptionTraits; /** @@ -25,7 +32,7 @@ final class OptionsResolver */ public function __construct(array $options) { - $this->options = new \ArrayObject(); + $this->options = new ArrayObject(); foreach ($options as $option) { $this->add($option); } @@ -39,7 +46,7 @@ public function resolve(array $options): array { try { $checkDiff = $this->checkDiff($options); - if(isset($checkDiff['exception'])){ + if (isset($checkDiff['exception'])) { return $checkDiff; } /** @@ -48,10 +55,10 @@ public function resolve(array $options): array $optionsResolved = []; foreach ($this->options as $option) { $optionName = $option->getName(); - if (\array_key_exists($optionName, $options)) { + if (array_key_exists($optionName, $options)) { $value = $options[$optionName]; if ($option->isValid($value) === false) { - throw new \InvalidArgumentException(sprintf('The option "%s" with value %s is invalid.', $optionName, self::formatValue($value))); + throw new InvalidArgumentException(sprintf('The option "%s" with value %s is invalid.', $optionName, self::formatValue($value))); } $optionsResolved[$optionName] = $value; continue; @@ -62,10 +69,10 @@ public function resolve(array $options): array continue; } - throw new \InvalidArgumentException(sprintf('The required option "%s" is missing.', $optionName)); + throw new InvalidArgumentException(sprintf('The required option "%s" is missing.', $optionName)); } return $optionsResolved; - } catch (\Exception $ex) { + } catch (Exception $ex) { return $this->exception($ex); } } @@ -89,14 +96,14 @@ private function checkDiff(array $options): array $defined = $this->options->getArrayCopy(); $diff = array_diff_key($options, $defined); if (count($diff) > 0) { - throw new \InvalidArgumentException(\sprintf( + throw new InvalidArgumentException(sprintf( 'The option(s) "%s" do(es) not exist. Defined options are: "%s".', implode(', ', array_keys($diff)), implode('", "', array_keys($defined))) ); } return []; - } catch (\Exception $ex) { + } catch (Exception $ex) { return $this->exception($ex); } } @@ -108,7 +115,7 @@ private function checkDiff(array $options): array private static function formatValue($value): string { if (is_object($value)) { - return \get_class($value); + return get_class($value); } if (is_string($value)) { @@ -123,6 +130,6 @@ private static function formatValue($value): string return 'true'; } - return \gettype($value); + return gettype($value); } } \ No newline at end of file diff --git a/src/Schema.php b/src/Schema.php index 87584eb..17b6f6d 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -61,8 +61,9 @@ public function boolean(): BooleanSchema /** * @return ArraySchema */ - public function array(): ArraySchema{ - return new ArraySchema(); + public function array(): ArraySchema + { + return new ArraySchema(); } // TODO add support for file validation } \ No newline at end of file diff --git a/src/Schema/ArraySchema.php b/src/Schema/ArraySchema.php index aceff28..1fbf1c6 100644 --- a/src/Schema/ArraySchema.php +++ b/src/Schema/ArraySchema.php @@ -7,8 +7,6 @@ namespace Wepesi\App\Schema; use Wepesi\App\Providers\SChemaProvider; -use Wepesi\App\Resolver\Option; -use Wepesi\App\Resolver\OptionsResolver; /** * Arraye schema validation @@ -27,8 +25,9 @@ public function __construct() * @param array $elements data array to be validated * @return $this */ - public function structure(array $elements):?ArraySchema{ - if(isset($this->schema[$this->class_name]['string']) || isset($this->schema[$this->class_name]['number'])) { + public function structure(array $elements): ?ArraySchema + { + if (isset($this->schema[$this->class_name]['string']) || isset($this->schema[$this->class_name]['number'])) { return false; } $this->schema[$this->class_name]['structure'] = $elements; @@ -39,8 +38,9 @@ public function structure(array $elements):?ArraySchema{ * check if array content are(is) string * @return $this|null */ - public function string():?ArraySchema{ - if(isset($this->schema[$this->class_name]['number'])) { + public function string(): ?ArraySchema + { + if (isset($this->schema[$this->class_name]['number'])) { return false; } $this->schema[$this->class_name]['string'] = true; @@ -50,8 +50,9 @@ public function string():?ArraySchema{ /** * @return $this|null */ - public function number():?ArraySchema{ - if(isset($this->schema[$this->class_name]['string'])) { + public function number(): ?ArraySchema + { + if (isset($this->schema[$this->class_name]['string'])) { return false; } $this->schema[$this->class_name]['number'] = true; diff --git a/src/Schema/DateSchema.php b/src/Schema/DateSchema.php index 487b577..f9fd059 100644 --- a/src/Schema/DateSchema.php +++ b/src/Schema/DateSchema.php @@ -5,6 +5,7 @@ */ namespace Wepesi\App\Schema; + use Wepesi\App\Providers\SChemaProvider; /** @@ -16,7 +17,8 @@ final class DateSchema extends SChemaProvider /** * */ - function __construct() { + function __construct() + { parent::__construct(__CLASS__); } @@ -39,6 +41,7 @@ public function max($rule): DateSchema $this->schema[$this->class_name]['max'] = $rule; return $this; } + /** * @return $this */ @@ -47,12 +50,13 @@ function now(): DateSchema $this->schema[$this->class_name]["now"] = true; return $this; } + /** * @return $this */ function today(): DateSchema { - $this->schema[$this->class_name]["today"]=true; + $this->schema[$this->class_name]["today"] = true; return $this; } } \ No newline at end of file diff --git a/src/Schema/NumberSchema.php b/src/Schema/NumberSchema.php index b1025e4..9e7d17c 100644 --- a/src/Schema/NumberSchema.php +++ b/src/Schema/NumberSchema.php @@ -12,12 +12,14 @@ * Schema number validation * validate any format number */ -final class NumberSchema extends SChemaProvider { +final class NumberSchema extends SChemaProvider +{ /** * */ - function __construct() { + function __construct() + { parent::__construct(__CLASS__); } diff --git a/src/Schema/StringSchema.php b/src/Schema/StringSchema.php index 2842ebc..08e2eee 100644 --- a/src/Schema/StringSchema.php +++ b/src/Schema/StringSchema.php @@ -18,12 +18,14 @@ * String valiation schema * validate string value */ -final class StringSchema extends SChemaProvider { +final class StringSchema extends SChemaProvider +{ /** * */ - public function __construct() { + public function __construct() + { parent::__construct(__CLASS__); } @@ -32,16 +34,17 @@ public function __construct() { */ public function email(): StringSchema { - $this->schema[$this->class_name]["email"]=true; + $this->schema[$this->class_name]["email"] = true; return $this; } + /** - * + * * @return $this */ public function url(): StringSchema { - $this->schema[$this->class_name]["url"]=true; + $this->schema[$this->class_name]["url"] = true; return $this; } @@ -60,10 +63,11 @@ public function match(string $key_to_match): StringSchema * @param string $ip_address * @return $this */ - public function addressIp(bool $ipv6 = false): StringSchema{ - if($ipv6) { + public function addressIp(bool $ipv6 = false): StringSchema + { + if ($ipv6) { $this->schema[$this->class_name]['addressIpv6'] = true; - }else{ + } else { $this->schema[$this->class_name]['addressIp'] = true; } return $this; diff --git a/src/Traits/InitTrait.php b/src/Traits/InitTrait.php index 5c95ca2..0458fa9 100644 --- a/src/Traits/InitTrait.php +++ b/src/Traits/InitTrait.php @@ -7,25 +7,27 @@ namespace Wepesi\App\Traits; +use Exception; + trait InitTrait { /** * @param $source * @param $schema - * @throws \Exception + * @throws Exception */ private function initInstance($source, $schema) { if (!is_array($source) || count($source) == 0) { - throw new \Exception('Your Source Data should not be en empty array'); + throw new Exception('Your Source Data should not be en empty array'); } if (!is_array($schema) || count($schema) == 0) { - throw new \Exception('Your Schema should not be en empty array'); + throw new Exception('Your Schema should not be en empty array'); } $fields = array_keys($schema); if (!isset($source[$fields[0]])) { - throw new \Exception('field not defined'); + throw new Exception('field not defined'); } $this->extract_data($schema); @@ -34,7 +36,8 @@ private function initInstance($source, $schema) /** * @param array $schema */ - protected function extract_data(array $schema ):void{ + protected function extract_data(array $schema): void + { // TODO implement extract data } } \ No newline at end of file diff --git a/src/Validate.php b/src/Validate.php index db65a9d..b265305 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -6,6 +6,7 @@ namespace Wepesi\App; +use Exception; use ReflectionClass; use Wepesi\App\Resolver\Option; use Wepesi\App\Resolver\OptionsResolver; @@ -38,7 +39,8 @@ function __construct() * @param array $schema data schema * @return void */ - function check(array $resource, array $schema){ + function check(array $resource, array $schema) + { try { $this->errors = []; $option_resolver = []; @@ -58,34 +60,34 @@ function check(array $resource, array $schema){ 'label' => "exception", ]; $this->addError($message); - }else{ + } else { foreach ($schema as $item => $rules) { - if(!is_array($rules)){ - throw new \Exception("Trying to access array offset on value of type null! method generate not called"); + if (!is_array($rules)) { + throw new Exception("Trying to access array offset on value of type null! method generate not called"); } $class_namespace = array_keys($rules)[0]; if ($class_namespace == "any") continue; - $validator_class = str_replace("Schema","Validator",$class_namespace); + $validator_class = str_replace("Schema", "Validator", $class_namespace); $reflexion = new ReflectionClass($validator_class); - $instance = $reflexion->newInstance($item,$resource); + $instance = $reflexion->newInstance($item, $resource); - foreach ($rules[$class_namespace] as $method => $params){ - if(method_exists($instance,$method)){ - call_user_func_array([$instance,$method],[$params]); + foreach ($rules[$class_namespace] as $method => $params) { + if (method_exists($instance, $method)) { + call_user_func_array([$instance, $method], [$params]); } } $result = $instance->result(); - if( count($result)>0 ){ - $this->errors = array_merge($this->errors,$result); + if (count($result) > 0) { + $this->errors = array_merge($this->errors, $result); } } - if ( count($this->errors) == 0) { + if (count($this->errors) == 0) { $this->passed = true; } } - }catch (\Exception $ex){ + } catch (Exception $ex) { die($ex); } } @@ -94,7 +96,8 @@ function check(array $resource, array $schema){ * @param array $message * @return void */ - private function addError(array $message){ + private function addError(array $message) + { $this->errors[] = $message; } @@ -105,6 +108,7 @@ public function errors(): array { return $this->errors; } + /** * @return bool */ diff --git a/src/Validator/ArrayValidator.php b/src/Validator/ArrayValidator.php index 63c6e0e..c28a1c5 100644 --- a/src/Validator/ArrayValidator.php +++ b/src/Validator/ArrayValidator.php @@ -10,7 +10,7 @@ use Wepesi\App\Validate; /** - * + * Validate array schema */ final class ArrayValidator extends ValidatorProvider { @@ -18,13 +18,12 @@ final class ArrayValidator extends ValidatorProvider * @param string $item field name * @param array $data_source resource where data will come from */ - public function __construct(string $item, array $data_source=[]) + public function __construct(string $item, array $data_source = []) { $this->errors = []; $this->data_source = $data_source; $this->field_name = $item; $this->field_value = $data_source[$item]; - $this->class_provider = "array"; parent::__construct(); } @@ -33,10 +32,10 @@ public function __construct(string $item, array $data_source=[]) * @param int $rule * @return void */ - public function min(int $rule):void + public function min(int $rule): void { // TODO: Implement min() method. - if($this->positiveParamMethod($rule)) return; + if ($this->positiveParamMethod($rule)) return; if (count($this->field_value) < $rule) { $message = [ 'type' => 'array.min', @@ -52,10 +51,10 @@ public function min(int $rule):void * @param int $rule * @return void */ - public function max(int $rule):void + public function max(int $rule): void { // TODO: Implement max() method. - if($this->positiveParamMethod($rule,true)) return; + if ($this->positiveParamMethod($rule, true)) return; if (count($this->field_value) > $rule) { $message = [ 'type' => 'array.max', @@ -71,12 +70,12 @@ public function max(int $rule):void * @param array $elements validate an array if elements, it should be and array with key value to be well set * @return void */ - public function structure(array $elements):void + public function structure(array $elements): void { $validate = new Validate(); $element_source = $this->data_source[$this->field_name]; - $validate->check($element_source,$elements); - if(!$validate->passed()){ + $validate->check($element_source, $elements); + if (!$validate->passed()) { foreach ($validate->errors() as $error) $this->addError($error); } } @@ -85,17 +84,17 @@ public function structure(array $elements):void * check content are string, in other case handler an error * @return void */ - public function string():void + public function string(): void { $len = count($this->field_value); if ($len < 1) { $this->min(1); - }else{ - $filter = array_filter($this->field_value,function($element){ - if(!is_string($element)) return $element; + } else { + $filter = array_filter($this->field_value, function ($element) { + if (!is_string($element)) return $element; }); $keys = array_keys($filter); - for($i=0; $i 'array.string', @@ -111,18 +110,18 @@ public function string():void /** * @return void */ - public function number():void + public function number(): void { $len = count($this->field_value); if ($len < 1) { $this->min(1); - }else{ - $filter = array_filter($this->field_value,function ($element){ - if(!is_numeric($element)) return $element; + } else { + $filter = array_filter($this->field_value, function ($element) { + if (!is_numeric($element)) return $element; }); $keys = array_keys($filter); - for($i=0; $i 'array.number', @@ -134,4 +133,12 @@ public function number():void } } } + + /** + * @return string + */ + protected function classProvider(): string + { + return 'array'; + } } \ No newline at end of file diff --git a/src/Validator/BooleanValidator.php b/src/Validator/BooleanValidator.php index d9a008b..773b7f5 100644 --- a/src/Validator/BooleanValidator.php +++ b/src/Validator/BooleanValidator.php @@ -15,7 +15,7 @@ use Wepesi\App\Providers\ValidatorProvider; /** - * Description of String + * Validate boolean schema * * @author Domeshow */ @@ -31,11 +31,10 @@ function __construct(string $item, array $source) $this->field_name = $item; $this->data_source = $source; $this->field_value = $source[$this->field_name]; - $this->class_provider = "boolean"; if ($this->isBoolean()) { $this->field_value = $source[$item]; - }; + } parent::__construct(); } @@ -43,13 +42,17 @@ function __construct(string $item, array $source) * @param $rule * @return void */ - public function min($rule){} + public function min($rule) + { + } /** * @param $rule * @return void */ - public function max($rule){} + public function max($rule) + { + } /** * @@ -58,15 +61,15 @@ public function max($rule){} */ private function isBoolean(string $itemKey = null): bool { - $item_to_check = !$itemKey ? $this->field_name:$itemKey ; + $item_to_check = !$itemKey ? $this->field_name : $itemKey; $val = $this->data_source[$item_to_check]; $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, + 'type' => 'boolean.unknown', + 'message' => "`$item_to_check` should be a boolean", + 'label' => $item_to_check, ]; $this->addError($message); return false; @@ -85,23 +88,31 @@ public function isValid(string $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, + 'type' => 'boolean.required', + 'message' => "isValid param must be boolean but you put `$required_value`", + 'label' => $this->field_name, ]; $this->addError($message); } else { // $sting_value= is_bool($this->string_value); - $incoming_value = $this->field_value ? 'true' : 'false'; + $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, + 'type' => 'boolean.valid', + 'message' => "`$incoming_value` is not validValue required. You must put `$required_value`", + 'label' => $this->field_name, ]; $this->addError($message); } } } + + /** + * @return string + */ + protected function classProvider(): string + { + return 'boolean'; + } } diff --git a/src/Validator/DateValidator.php b/src/Validator/DateValidator.php index 4df8911..fe9d767 100644 --- a/src/Validator/DateValidator.php +++ b/src/Validator/DateValidator.php @@ -5,8 +5,8 @@ */ - namespace Wepesi\App\Validator; + use Wepesi\App\Providers\ValidatorProvider; /** @@ -21,11 +21,11 @@ final class DateValidator extends ValidatorProvider * @param string $item * @param array $data_source */ - public function __construct(string $item, array $data_source ) { + public function __construct(string $item, array $data_source) + { $this->field_name = $item; $this->field_value = $data_source[$item]; $this->data_source = $data_source; - $this->class_provider = "date"; $this->checkExist(); parent::__construct(); } @@ -35,15 +35,15 @@ public function __construct(string $item, array $data_source ) { */ public function now() { - $min_date_time = strtotime("now"); - $min_date = date("d/F/Y",$min_date_time); + $min_date_time = strtotime('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 + $message = [ + 'type' => 'date.now', + 'message' => "`$this->field_name` should be greater than now", + 'label' => $this->field_name, + 'limit' => $min_date ]; $this->addError($message); } @@ -54,17 +54,17 @@ public function now() * @return void * while trying to get day validation use this module */ - public function today(string $times=null) + public function today(string $times = null) { $min_date_time = strtotime("now {$times}"); - $min_date = date("d/F/Y",$min_date_time); + $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 + $message = [ + 'type' => 'date.now', + 'message' => "`$this->field_name` should be greater than today ", + 'label' => $this->field_name, + 'limit' => $min_date ]; $this->addError($message); } @@ -83,16 +83,16 @@ public function min($rule) * $con=!$time?$time:(int)$time; * in case the parameters are integers */ - $rule = $rule ?? "now"; + $rule = $rule ?? 'now'; $min_date_time = strtotime($rule); - $min_date = date("d/F/Y",$min_date_time); + $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 + $message = [ + 'type' => 'date.min', + 'message' => "`$this->field_name` should be greater than `$min_date`", + 'label' => $this->field_name, + 'limit' => $min_date ]; $this->addError($message); } @@ -105,16 +105,16 @@ public function min($rule) */ public function max($rule) { - $rule = $rule ?? "now"; + $rule = $rule ?? 'now'; $max_date_time = strtotime($rule); - $max_date = date("d/F/Y",$max_date_time); + $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 + 'type' => 'date.max', + 'message' => "`$this->field_name` should be less than `$max_date`", + 'label' => $this->field_name, + 'limit' => $max_date ]; $this->addError($message); } @@ -124,25 +124,33 @@ public function max($rule) * @param string|null $itemKey * @return void */ - protected function checkExist(string $itemKey=null): void + protected function checkExist(string $itemKey = null): void { - $item_to_check = $itemKey?$itemKey:$this->field_name; - $regex = "#[a-zA-Z0-9]#"; - $this->_errors=[]; + $item_to_check = $itemKey ? $itemKey : $this->field_name; + $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, + 'type' => 'any.unknown', + 'message' => "`$item_to_check` is unknown", + 'label' => $item_to_check, ]; $this->addError($message); - }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, + } 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); } } + + /** + * @return string + */ + protected function classProvider(): string + { + return 'date'; + } } \ No newline at end of file diff --git a/src/Validator/NumberValidator.php b/src/Validator/NumberValidator.php index 009fbc2..9332bef 100644 --- a/src/Validator/NumberValidator.php +++ b/src/Validator/NumberValidator.php @@ -19,18 +19,19 @@ * * @author Boss Ibrahim Mussa */ -final class NumberValidator extends ValidatorProvider { +final class NumberValidator extends ValidatorProvider +{ /** * @param string $item * @param array $data_source */ - function __construct(string $item, array $data_source) { + function __construct(string $item, array $data_source) + { $this->data_source = $data_source; $this->field_name = $item; $this->field_value = $data_source[$item]; - $this->class_provider = "number"; - if($this->isNumber()){ + if ($this->isNumber()) { $this->field_value = $data_source[$item]; } parent::__construct(); @@ -42,13 +43,13 @@ function __construct(string $item, array $data_source) { */ function min(int $rule) { - if($this->positiveParamMethod($rule)) return; - if ((int) $this->field_value < $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 + 'type' => 'number.min', + 'message' => "`$this->field_name` should be greater than `$rule`", + 'label' => $this->field_name, + 'limit' => $rule ]; $this->addError($message); } @@ -60,13 +61,13 @@ function min(int $rule) */ function max(int $rule) { - if($this->positiveParamMethod($rule,true)) return; - if ((int) $this->field_value > $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 + 'type' => 'number.max', + 'message' => "`$this->field_name` should be less than `$rule`", + 'label' => $this->field_name, + 'limit' => $rule ]; $this->addError($message); } @@ -77,12 +78,12 @@ function max(int $rule) */ function positive() { - if ((int) $this->field_value < 0) { + 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 + 'type' => 'number.positive', + 'message' => "`$this->field_name` should be a positive number", + 'label' => $this->field_name, + 'limit' => 1 ]; $this->addError($message); } @@ -93,16 +94,24 @@ function positive() */ 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])) { + $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, + 'type' => 'number.unknown', + 'message' => "`$this->field_name` should be a number", + 'label' => $this->field_name, ]; $this->addError($message); return false; } return true; } + + /** + * @return string + */ + protected function classProvider(): string + { + return 'number'; + } } diff --git a/src/Validator/StringValidator.php b/src/Validator/StringValidator.php index 2ef5182..8f22215 100644 --- a/src/Validator/StringValidator.php +++ b/src/Validator/StringValidator.php @@ -13,18 +13,19 @@ * * @author Boss Ibrahim Mussa */ -final class StringValidator extends ValidatorProvider { +final class StringValidator extends ValidatorProvider +{ /** * * @param string $item the item to be validated. * @param array $data_source the source data from where is going to check it the match key exist and have value. */ - public function __construct(string $item, array $data_source=[]) { + public function __construct(string $item, array $data_source = []) + { $this->errors = []; $this->data_source = $data_source; $this->field_name = $item; $this->field_value = $data_source[$item]; - $this->class_provider = "string"; parent::__construct(); } @@ -33,15 +34,15 @@ public function __construct(string $item, array $data_source=[]) { * @param int $rule * */ - public function min(int $rule):void + public function min(int $rule): void { - if($this->positiveParamMethod($rule)) return; + 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 + $message = [ + 'type' => 'string.min', + 'message' => "`$this->field_name` should have minimum of `$rule` characters", + 'label' => $this->field_name, + 'limit' => $rule ]; $this->addError($message); } @@ -52,15 +53,15 @@ public function min(int $rule):void * @param int $rule * */ - public function max(int $rule):void + public function max(int $rule): void { - if($this->positiveParamMethod($rule,true)) return; + 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 + 'type' => 'string.max', + 'message' => "`$this->field_name` should have maximum of `$rule` characters", + 'label' => $this->field_name, + 'limit' => $rule ]; $this->addError($message); } @@ -73,9 +74,9 @@ 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, + 'type' => 'string.email', + 'message' => ("`$this->field_name` should be an email."), + 'label' => $this->field_name, ]; $this->addError($message); } @@ -92,9 +93,9 @@ 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, + 'type' => 'string.url', + 'message' => ("'$this->field_name' this should be a link(url)"), + 'label' => $this->field_name, ]; $this->addError($message); } @@ -108,11 +109,11 @@ public function url() 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])) { + 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, + 'type' => 'string.match', + 'message' => "`$this->field_name` should match `$key_to_match`", + 'label' => $this->field_name, ]; $this->addError($message); } @@ -122,8 +123,9 @@ public function match(string $key_to_match) * @param string $ip_address * @return void */ - public function addressIp(string $ip_address){ - if (!filter_var($this->data_source[$ip_address], FILTER_VALIDATE_IP) ) { + public function addressIp(string $ip_address) + { + if (!filter_var($this->data_source[$ip_address], FILTER_VALIDATE_IP)) { $message = [ 'type' => 'string.ip_address', 'message' => "`$this->field_name` is not a valid Ip address", @@ -137,8 +139,9 @@ public function addressIp(string $ip_address){ * @param string $ip_address * @return void */ - public function addressIpv6(string $ip_address){ - if (!filter_var($this->data_source[$ip_address], FILTER_VALIDATE_IP,FILTER_FLAG_IPV6) ) { + public function addressIpv6(string $ip_address) + { + if (!filter_var($this->data_source[$ip_address], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { $message = [ 'type' => 'string.ip_address_v6', 'message' => "`$this->field_name` should be an ip address (ipv6)", @@ -147,6 +150,7 @@ public function addressIpv6(string $ip_address){ $this->addError($message); } } + /** * * @param string $item_key @@ -154,22 +158,27 @@ public function addressIpv6(string $ip_address){ */ 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])){ + $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); - }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, - ]; + } 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); } } + + protected function classProvider(): string + { + return 'string'; + } } diff --git a/test/Schema/BooleanSchemaTest.php b/test/Schema/BooleanSchemaTest.php index ae89e54..29d54f8 100644 --- a/test/Schema/BooleanSchemaTest.php +++ b/test/Schema/BooleanSchemaTest.php @@ -10,30 +10,47 @@ use PHPUnit\Framework\TestCase; use Wepesi\App\Schema\BooleanSchema; +/** + * + */ class BooleanSchemaTest extends TestCase { + private string $booleanSchema = 'Wepesi\\App\\Schema\\BooleanSchema'; + /** + * @return void + */ public function testBooleanIsObject() { $booleanSchema = new BooleanSchema(); $this->assertIsObject($booleanSchema); } + /** + * @return void + */ public function testStringObjectIsKey() { $booleanSchema = new BooleanSchema(); - $this->assertArrayHasKey('BooleanValidator', $booleanSchema->generate()); + $this->assertArrayHasKey($this->booleanSchema, $booleanSchema->generate()); } + /** + * @return void + */ public function testBooleanIsRequireddKey() { $booleanSchema = new BooleanSchema(); - $subset_array = ['BooleanValidator' => ['required' => true]]; + $subset_array = [$this->booleanSchema => ['required' => true]]; $this->assertEquals($subset_array, $booleanSchema->required()->generate()); } + + /** + * @return void + */ public function testBooleanIsValidKey() { $booleanSchema = new BooleanSchema(); - $subset_array = ['BooleanValidator' => ['isValid' => true]]; + $subset_array = [$this->booleanSchema => ['isValid' => true]]; $this->assertEquals($subset_array, $booleanSchema->isValid()->generate()); } } \ No newline at end of file diff --git a/test/Schema/DateSchemaTest.php b/test/Schema/DateSchemaTest.php index 58c719d..45a955b 100644 --- a/test/Schema/DateSchemaTest.php +++ b/test/Schema/DateSchemaTest.php @@ -10,30 +10,47 @@ use PHPUnit\Framework\TestCase; use Wepesi\App\Schema\DateSchema; +/** + * + */ class DateSchemaTest extends TestCase { + private string $dateSchema = 'Wepesi\\App\\Schema\\DateSchema'; + /** + * @return void + */ public function testStringIsObject() { $dateSchema = new DateSchema(); $this->assertIsObject($dateSchema); } + /** + * @return void + */ public function testStringObjectIsKey() { $dateSchema = new DateSchema(); - $this->assertArrayHasKey('DateValidator', $dateSchema->generate()); + $this->assertArrayHasKey($this->dateSchema, $dateSchema->generate()); } + /** + * @return void + */ public function testDateNowKey() { $dateSchema = new DateSchema(); - $subset_array = ['DateValidator' => ['now' => true]]; + $subset_array = [$this->dateSchema => ['now' => true]]; $this->assertEquals($subset_array, $dateSchema->now()->generate()); } + + /** + * @return void + */ public function testDateTodayKey() { $dateSchema = new DateSchema(); - $subset_array = ['DateValidator' => ['today' => true]]; + $subset_array = [$this->dateSchema => ['today' => true]]; $this->assertEquals($subset_array, $dateSchema->today()->generate()); } } \ No newline at end of file diff --git a/test/Schema/NumberSchemaTest.php b/test/Schema/NumberSchemaTest.php index b63d88d..e752cb8 100644 --- a/test/Schema/NumberSchemaTest.php +++ b/test/Schema/NumberSchemaTest.php @@ -10,8 +10,12 @@ use PHPUnit\Framework\TestCase; use Wepesi\App\Schema\NumberSchema; +/** + * + */ class NumberSchemaTest extends TestCase { + private string $numberSchema = 'Wepesi\\App\\Schema\\NumberSchema'; /** * @return void */ @@ -27,7 +31,7 @@ public function testStringIsObject() public function testStringObjectIsKey() { $numberSchema = new NumberSchema(); - $this->assertArrayHasKey("NumberValidator", $numberSchema->generate()); + $this->assertArrayHasKey($this->numberSchema, $numberSchema->generate()); } /** @@ -36,7 +40,7 @@ public function testStringObjectIsKey() public function testRequiredKey() { $numberSchema = new NumberSchema(); - $subset_array = ["NumberValidator" => ['required' => true]]; + $subset_array = [$this->numberSchema => ['required' => true]]; $this->assertEquals($subset_array, $numberSchema->required()->generate()); } @@ -46,7 +50,7 @@ public function testRequiredKey() public function testNumberPositiveKey() { $numberSchema = new NumberSchema(); - $subset_array = ["NumberValidator" => ['positive' => true]]; + $subset_array = [$this->numberSchema => ['positive' => true]]; $this->assertEquals($subset_array, $numberSchema->positive()->generate()); } } \ No newline at end of file diff --git a/test/Schema/StringSchemaTest.php b/test/Schema/StringSchemaTest.php index a8953a7..070bcda 100644 --- a/test/Schema/StringSchemaTest.php +++ b/test/Schema/StringSchemaTest.php @@ -12,10 +12,12 @@ class StringSchemaTest extends TestCase { + private string $stringSchema = 'Wepesi\\App\\Schema\\StringSchema'; /** * @return void */ - public function testStringIsObject(){ + public function testStringIsObject() + { $stringSchema = new StringSchema(); $this->assertIsObject($stringSchema); } @@ -23,53 +25,59 @@ public function testStringIsObject(){ /** * @return void */ - public function testStringObjectIsKey(){ + public function testStringObjectIsKey() + { $stringSchema = new StringSchema(); - $this->assertArrayHasKey("StringValidator",$stringSchema->generate()); + $this->assertArrayHasKey($this->stringSchema, $stringSchema->generate()); } /** * @return void */ - public function testStringEmailKey(){ + public function testStringEmailKey() + { $stringSchema = new StringSchema(); - $subset_array = ["StringValidator"=>["email"=>true]]; + $subset_array = [$this->stringSchema => ["email" => true]]; $this->assertEquals($subset_array, $stringSchema->email()->generate()); } /** * @return void */ - public function testStringURLKey(){ + public function testStringURLKey() + { $stringSchema = new StringSchema(); - $subset_array = ["StringValidator"=>["url"=>true]]; + $subset_array = [$this->stringSchema => ["url" => true]]; $this->assertEquals($subset_array, $stringSchema->url()->generate()); } /** * @return void */ - public function testStringMatchKey(){ + public function testStringMatchKey() + { $stringSchema = new StringSchema(); - $subset_array = ["StringValidator" => ["match"=>'email']]; + $subset_array = [$this->stringSchema => ["match" => 'email']]; $this->assertEquals($subset_array, $stringSchema->match('email')->generate()); } /** * @return void */ - public function testStringMinimumKey(){ + public function testStringMinimumKey() + { $stringSchema = new StringSchema(); - $subset_array = ["StringValidator" => ["min" => 1]]; + $subset_array = [$this->stringSchema => ["min" => 1]]; $this->assertEquals($subset_array, $stringSchema->min(1)->generate()); } /** * @return void */ - public function testStringMaximumKey(){ + public function testStringMaximumKey() + { $stringSchema = new StringSchema(); - $subset_array = ["StringValidator" => ["max" => 10]]; + $subset_array = [$this->stringSchema => ["max" => 10]]; $this->assertEquals($subset_array, $stringSchema->max(10)->generate()); } } \ No newline at end of file diff --git a/test/SchemaTest.php b/test/SchemaTest.php index 9ff0d1c..05a8300 100644 --- a/test/SchemaTest.php +++ b/test/SchemaTest.php @@ -10,9 +10,16 @@ use PHPUnit\Framework\TestCase; use Wepesi\App\Schema; +/** + * + */ class SchemaTest extends TestCase { - public function testSchemaObject(){ + /** + * @return void + */ + public function testSchemaObject() + { $schema = new Schema(); $this->assertIsObject($schema); } @@ -20,102 +27,128 @@ public function testSchemaObject(){ /** * @return void */ - public function testSchemaStringObject(){ + public function testSchemaStringObject() + { $schema = new Schema(); - $rules=["name"=>$schema->string()->min(3)->max(10)->required()->generate()]; - $expected=[ - "name"=>[ - "StringValidator"=>[ - "min"=>3, - "max"=>10, - "required"=>true + $rules = ["name" => $schema->string()->min(3)->max(10)->required()->generate()]; + $expected = [ + "name" => [ + "Wepesi\\App\\Schema\\StringSchema" => [ + "min" => 3, + "max" => 10, + "required" => true ] ] ]; - $this->assertEquals($rules,$expected); + $this->assertEquals($rules, $expected); } - public function testSchemaStringErrorObject(){ + + /** + * @return void + */ + public function testSchemaStringErrorObject() + { $schema = new Schema(); - $rules=["name" => $schema->string()->min(3)->max(10)->required()]; - $expected=[ - "name"=>[ - "StringValidator"=>[ - "min"=>3, - "max"=>10, - "required"=>true + $rules = ["name" => $schema->string()->min(3)->max(10)->required()]; + $expected = [ + "name" => [ + "Wepesi\\App\\Schema\\StringSchema" => [ + "min" => 3, + "max" => 10, + "required" => true ] ] ]; $this->assertIsNotArray($rules["name"]); - $this->assertNotEquals($expected,$rules); + $this->assertNotEquals($expected, $rules); } + /* * Number Schema Test */ - public function testSchemaNumberObject(){ + /** + * @return void + */ + public function testSchemaNumberObject() + { $schema = new Schema(); - $rules=["age"=>$schema->number()->min(3)->max(10)->required()->positive()->generate()]; - $expected=[ - "age"=>[ - "NumberValidator"=>[ - "min"=>3, - "max"=>10, - "required"=>true, + $rules = ["age" => $schema->number()->min(3)->max(10)->required()->positive()->generate()]; + $expected = [ + "age" => [ + "Wepesi\\App\\Schema\\NumberSchema" => [ + "min" => 3, + "max" => 10, + "required" => true, "positive" => true, ] ] ]; - $this->assertEquals($rules,$expected); + $this->assertEquals($rules, $expected); } - public function testSchemaNumberErrorObject(){ + + /** + * @return void + */ + public function testSchemaNumberErrorObject() + { $schema = new Schema(); $rules = ["age" => $schema->number()->min(5)->max(10)->positive()->required()]; - $expected=[ - "age"=>[ - "NumberValidator"=>[ - "min"=>3, - "max"=>10, - "required"=>true, - "positive"=>false, + $expected = [ + "age" => [ + "Wepesi\\App\\Schema\\NumberSchema" => [ + "min" => 3, + "max" => 10, + "required" => true, + "positive" => false, ] ] ]; $this->assertIsNotArray($rules["age"]); - $this->assertNotEquals($expected,$rules); + $this->assertNotEquals($expected, $rules); } + /* * Number Schema Test */ - public function testSchemaDateObject(){ + /** + * @return void + */ + public function testSchemaDateObject() + { $schema = new Schema(); $rules = ["date_creat" => $schema->date()->min("now")->max("2022-12-25")->now()->required()->generate()]; $expected = [ - "date_creat"=>[ - "DateValidator" => + "date_creat" => [ + "Wepesi\\App\\Schema\\DateSchema" => [ - "min" => "now", - "max" => "2022-12-25", - "required"=> true, - "now" => true, - ] + "min" => "now", + "max" => "2022-12-25", + "required" => true, + "now" => true, + ] ] ]; - $this->assertEquals($rules,$expected); + $this->assertEquals($rules, $expected); } - public function testSchemaDateErrorObject(){ + + /** + * @return void + */ + public function testSchemaDateErrorObject() + { $schema = new Schema(); $rules = ["date_creat" => $schema->date()->min("now")->max("2022-12-25")->required()]; - $expected=[ - "date_creat"=>[ - "DateValidator"=>[ - "min"=>"now", - "max"=>"2022-12-25", - "required"=>true, - "positive"=>false, + $expected = [ + "date_creat" => [ + "Wepesi\\App\\Schema\\DateSchema" => [ + "min" => "now", + "max" => "2022-12-25", + "required" => true, + "positive" => false, ] ] ]; $this->assertIsNotArray($rules["date_creat"]); - $this->assertNotEquals($expected,$rules); + $this->assertNotEquals($expected, $rules); } } \ No newline at end of file diff --git a/test/Script/StringValidationTest.php b/test/Script/StringValidationTest.php deleted file mode 100644 index 55ba72a..0000000 --- a/test/Script/StringValidationTest.php +++ /dev/null @@ -1,346 +0,0 @@ -getMessage(); - $code = $ex->getCode(); - } - - $expectedMessage = 'Your Source Data should not be en empty array'; - $expectedCode = 0; - $this->assertSame($stringValidationSourceDataException,$className); - $this->assertSame($expectedMessage,$msg); - $this->assertSame($expectedCode,$code); - } - public function testStringValidationSchemaException(){ - $stringValidationSchemaException= Exception::class; - try { - $source=["name"=>"john Doe"]; - $stringValidationSchemaException = new StringValidator($source, []); - }catch (Exception $ex){ - $className = get_class($ex); - $msg = $ex->getMessage(); - $code = $ex->getCode(); - } - - $expectedMessage = 'Your Schema should not be en empty array'; - $expectedCode = 0; - $this->assertSame($stringValidationSchemaException,$className); - $this->assertSame($expectedMessage,$msg); - $this->assertSame($expectedCode,$code); - } - public function testStringValidationFieldNotDefinedException(){ - $stringValidationSchemaException= Exception::class; - try { - $source=["name"=>"john Doe"]; - $schema=["names"=>[ - "string"=>[ - "required"=>false - ] - ]]; - $stringValidationSchemaException = new StringValidator($source, $schema); - }catch (Exception $ex){ - $className = get_class($ex); - $msg = $ex->getMessage(); - $code = $ex->getCode(); - } - - $expectedMessage = 'field not defined'; - $expectedCode = 0; - $this->assertSame($stringValidationSchemaException,$className); - $this->assertSame($expectedMessage,$msg); - $this->assertSame($expectedCode,$code); - } - /** - * @throws Exception - */ - public function testStringIsObject(){ - $source = ['name' => 'John Doe']; - $schema = [ - 'name' => [ - 'String' => [ - 'min' => 150 - ] - ] - ]; - $stringValidation= new StringValidator($source,$schema); - $this->assertIsObject($stringValidation); - } - /** - * @throws Exception - */ - public function testStringMinError(){ - $source=["name"=>"John Doe"]; - $schema=[ - 'name'=>[ - 'String'=>[ - 'min'=>150 - ] - ] - ]; - $stringValidation= new StringValidator($source,$schema); - $error[]=[ - 'type'=>'string.min', - 'message'=>'`name` should have minimum of `150` characters', - 'label'=>'name', - 'limit'=>150 - ]; - $this->assertEquals($stringValidation->result(150),$error); - } - - /** - * @throws Exception - */ - public function testStringMinSuccess(){ - $source=["name"=>"John Doe"]; - $schema=[ - 'name'=>[ - 'String'=>[ - 'min'=>5 - ] - ] - ]; - $stringValidation= new StringValidator($source,$schema); - $this->assertEmpty($stringValidation->result()); - }/** - * @throws Exception - */ - public function testStringMaxError(){ - $source=["description"=>"Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores assumenda at, dolorem dolores error harum laudantium magnam qui quisquam sapiente sequi unde, voluptatum. At illo nesciunt obcaecati, odit omnis repellendus! -"]; - $schema=[ - 'description'=>[ - 'String'=>[ - 'max'=>150 - ] - ] - ]; - $stringValidation= new StringValidator($source,$schema); - $error[]=[ - 'type'=>'string.max', - 'message'=>'`description` should have maximum of `150` characters', - 'label'=>'description', - 'limit'=>150 - ]; - $this->assertEquals($stringValidation->result(150),$error); - } - - /** - * @throws Exception - */ - public function testStringMaxSuccess(){ - $source=["description"=>"Lorem ipsum dolor sit amet."]; - $schema=[ - 'description'=>[ - 'String'=>[ - 'max'=>50 - ] - ] - ]; - $stringValidation= new StringValidator($source,$schema); - $this->assertEmpty($stringValidation->result()); - }/** - * @throws Exception - */ - public function testStringEmailError(){ - $source=["email"=>"John@Doe"]; - $schema=[ - 'email'=>[ - 'String'=>[ - 'email'=>true - ] - ] - ]; - $stringValidation= new StringValidator($source,$schema); - $error[]=[ - 'type'=>'string.email', - 'message'=>'`email` should be an email.', - 'label'=>'email' - ]; - $this->assertEquals($stringValidation->result(),$error); - } - - /** - * @throws Exception - */ - public function testStringEmailSuccess(){ - $source=["email"=>"John@doe.com"]; - $schema=[ - 'email'=>[ - 'String'=>[ - 'email'=>true - ] - ] - ]; - $stringValidation= new StringValidator($source,$schema); - $this->assertEmpty($stringValidation->result()); - } - /** - * @throws Exception - */ - public function testStringURLError(){ - $source=["link"=>"hello.com"]; - $schema=[ - 'link'=>[ - 'String'=>[ - 'url'=>150 - ] - ] - ]; - $stringValidation= new StringValidator($source,$schema); - $error[]=[ - 'type'=>'string.url', - 'message'=>'`link` this should be a link(url)', - 'label'=>'link' - ]; - $this->assertEquals($stringValidation->result(150),$error); - } - - /** - * @throws Exception - */ - public function testStringURLSuccess(){ - /** - * supported link : http(s)://[domain].[extension] , - * http(s)://www.[domain].[extension], - * www.[domain].[extension] - */ - $source=["link"=> "http://wepesi.com"]; - $schema=[ - 'link'=>[ - 'String'=>[ - 'url'=>true - ] - ] - ]; - $stringValidation= new StringValidator($source,$schema); - $this->assertEmpty($stringValidation->result()); - } - - /** - * @throws Exception - */ - public function testStringRequiredErrorsUnknown() - { - $source = ['username' => ' ']; - $schema = [ - 'username' => [ - 'String' => [ - 'required' => true - ] - ] - ]; - $stringValidation = new StringValidator($source, $schema); - $error=[ - 'type' => 'string.unknown', - 'message' => "`username` should be a string", - 'label' => "username" - ]; - $this->assertEquals($stringValidation->result()[0],$error); - } - - /** - * @throws Exception - */ - public function testStringRequiredErrors() - { - $source = ['username' => ' ']; - $schema = [ - 'username' => [ - 'String' => [ - 'required' => true - ] - ] - ]; - $stringValidation = new StringValidator($source, $schema); - $error=[ - 'type' => 'string.required', - 'message' => "`username` is required", - 'label' => "username" - ]; - $this->assertEquals($stringValidation->result()[1],$error); - } - - /** - * @throws Exception - */ - public function testStringRequiredSuccess() - { - $source = ['username' => 'JonDoe']; - $schema = [ - 'username' => [ - 'String' => [ - 'required' => true - ] - ] - ]; - $stringValidation = new StringValidator($source, $schema); - $this->assertEmpty($stringValidation->result()); - } - - /** - * @throws Exception - */ - public function testStringMatchErrors() - { - $source = [ - 'password' => 'passwrd', - 'retape_password' => 'password', - ]; - $schema = [ - 'password' => [ - 'String' => [ - 'match' => 'retape_password' - ] - ] - ]; - $stringValidation = new StringValidator($source, $schema); - $error[]=[ - 'type' => 'string.match', - 'message' => "`password` should match `retape_password`", - 'label' => "password" - ]; - $this->assertEquals($stringValidation->result(),$error); - } - - /** - * @throws Exception - */ - public function testStringMatchSuccess() - { - $source = [ - 'password' => 'password', - 'retape_password' => 'password', - ]; - $schema = [ - 'password' => [ - 'String' => [ - 'match' => 'retape_password' - ] - ] - ]; - $stringValidation = new StringValidator($source, $schema); - $this->assertEmpty($stringValidation->result()); - } -} \ No newline at end of file