diff --git a/README.md b/README.md index b253257..f9c0fd9 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,11 @@ use this method your string value, it has built in method for different scenario - `matches` : this is used tho check if two key has the same value, you should specify the second field to check. - `generate` : this methode will generate the array structure that will be used to validate the value, and should be called and the end of each element. In case it's not called there will be an error. +### supported `url` : +* `http(s)://[domain].[extension]` , +* `http(s)://www.[domain].[extension]`, +* `www.[domain].[extension]` + In the example bellow, you can see a complete procured on how to validate data-source ```php diff --git a/src/Providers/ValidatorProvider.php b/src/Providers/ValidatorProvider.php index d5c683a..2457213 100644 --- a/src/Providers/ValidatorProvider.php +++ b/src/Providers/ValidatorProvider.php @@ -60,4 +60,19 @@ public function result(): array { return $this->errors; } + + protected function positiveParamMethod(int $rule,bool $max = false):bool{ + $status = true; + if($rule<1){ + $method = $max?"max":"min"; + $message = [ + 'type' => $this->class_provider . ' method '. $method, + 'message' => "'$this->field_name' $method param should be a positive number", + 'label' => $this->field_name, + ]; + $this->addError($message); + $status = false; + } + return $status; + } } \ No newline at end of file diff --git a/src/Traits/InitTrait.php b/src/Traits/InitTrait.php index 3a212e7..5c95ca2 100644 --- a/src/Traits/InitTrait.php +++ b/src/Traits/InitTrait.php @@ -17,10 +17,10 @@ trait InitTrait 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); @@ -34,11 +34,7 @@ private function initInstance($source, $schema) /** * @param array $schema */ - protected function extract_data(array $schema ){ - $conditions = $schema[$this->field_item]['String']; - foreach ($conditions as $key=>$value){ - call_user_func([$this,$key],$value); - return; - } + 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 a773935..b8979cc 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -6,13 +6,9 @@ namespace Wepesi\App; -use Exception; -use phpDocumentor\Reflection\Types\Null_; use ReflectionClass; use Wepesi\App\Resolver\Option; use Wepesi\App\Resolver\OptionsResolver; -use function PHPUnit\Framework\throwException; - class Validate { diff --git a/src/Validator/ArrayValidator.php b/src/Validator/ArrayValidator.php index 1270c0d..8832423 100644 --- a/src/Validator/ArrayValidator.php +++ b/src/Validator/ArrayValidator.php @@ -30,9 +30,10 @@ public function __construct(string $item, array $data_source=[]) * @param int $rule * @return void */ - public function min(int $rule) + 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', @@ -44,9 +45,10 @@ public function min(int $rule) } } - public function max(int $rule) + 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', @@ -62,7 +64,7 @@ public function max(int $rule) * @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) + public function structure(array $elements):void { $validate = new Validate(); $element_source = $this->data_source[$this->field_name]; @@ -76,7 +78,7 @@ public function structure(array $elements) * check content are string, in other case handler an error * @return void */ - public function string() + public function string():void { $len = count($this->field_value); if ($len < 1) { diff --git a/src/Validator/NumberValidator.php b/src/Validator/NumberValidator.php index f7552a8..eb2ac84 100644 --- a/src/Validator/NumberValidator.php +++ b/src/Validator/NumberValidator.php @@ -38,6 +38,7 @@ function __construct(string $item, array $data_source) { */ function min(int $rule) { + if($this->positiveParamMethod($rule)) return; if ((int) $this->field_value < $rule) { $message = [ "type" => "number.min", @@ -55,6 +56,7 @@ function min(int $rule) */ function max(int $rule) { + if($this->positiveParamMethod($rule,true)) return; if ((int) $this->field_value > $rule) { $message = [ "type" => "number.max", diff --git a/src/Validator/StringValidator.php b/src/Validator/StringValidator.php index b1a2349..6087d17 100644 --- a/src/Validator/StringValidator.php +++ b/src/Validator/StringValidator.php @@ -35,6 +35,7 @@ public function __construct(string $item, array $data_source=[]) { */ public function min(int $rule):void { + if($this->positiveParamMethod($rule)) return; if (strlen($this->field_value) < $rule) { $message=[ "type"=>"string.min", @@ -51,8 +52,9 @@ public function min(int $rule):void * @param int $rule * */ - public function max(int $rule) + public function max(int $rule):void { + if($this->positiveParamMethod($rule,true)) return; if (strlen($this->field_value) > $rule) { $message = [ "type" => "string.max",