From 27cb48e333c9a7bf44abaccb54f92adb353447cb Mon Sep 17 00:00:00 2001 From: bim-g Date: Tue, 25 Jul 2023 13:17:15 +0200 Subject: [PATCH] [UPD] replace builin option resolver to external option resolver --- composer.json | 3 +- composer.lock | 49 ++++++++++- example/string.php | 3 +- example/vardump.php | 6 +- index.php | 12 +-- src/Resolver/Option.php | 104 ----------------------- src/Resolver/OptionsResolver.php | 135 ------------------------------ src/Validate.php | 25 +++--- src/Validator/StringValidator.php | 10 +-- 9 files changed, 73 insertions(+), 274 deletions(-) delete mode 100644 src/Resolver/Option.php delete mode 100644 src/Resolver/OptionsResolver.php diff --git a/composer.json b/composer.json index a487304..553c3f7 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ } }, "require": { - "php": ">=7.4" + "php": ">=7.4", + "wepesi/optionsresolver": "^0.2.0" }, "require-dev": { "phpunit/phpunit": "^9.5", diff --git a/composer.lock b/composer.lock index 2dba39a..d0353fc 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,53 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "264ace750bc4365dbdd196650e2483b7", - "packages": [], + "content-hash": "f0a42787a9848338fdc72987d55cdb9e", + "packages": [ + { + "name": "wepesi/optionsresolver", + "version": "v0.2.0", + "source": { + "type": "git", + "url": "https://github.com/bim-g/OptionsResolver.git", + "reference": "6b60f6e8a86b0335d0f0c6dedb217684ddd22e82" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/bim-g/OptionsResolver/zipball/6b60f6e8a86b0335d0f0c6dedb217684ddd22e82", + "reference": "6b60f6e8a86b0335d0f0c6dedb217684ddd22e82", + "shasum": "" + }, + "require": { + "php": ">=7.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "Wepesi\\Resolver\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "bim-g", + "email": "ibmussafb@gmail.com" + } + ], + "description": "The OptionsResolver component helps you configure objects with option arrays. It supports default values, option constraints and lazy options.", + "support": { + "docs": "https://github.com/bim-g/OptionsResolver", + "email": "ibmussfb@gmail.com", + "forum": "https://github.com/bim-g/OptionsResolver", + "issues": "https://github.com/bim-g/OptionsResolver/issues", + "source": "https://github.com/bim-g/OptionsResolver", + "wiki": "https://github.com/bim-g/OptionsResolver" + }, + "time": "2023-04-05T12:09:04+00:00" + } + ], "packages-dev": [ { "name": "clue/stdio-react", diff --git a/example/string.php b/example/string.php index f450ec1..985a097 100644 --- a/example/string.php +++ b/example/string.php @@ -28,8 +28,7 @@ "new_password" => $schema->string()->min(3)->max(40)->match("password")->generate(), "email" => $schema->string()->min(3)->max(40)->email()->generate(), "link" => $schema->string()->min(3)->max(40)->url()->generate(), - "ip" => $schema->string()->addressIp()->max(40)->url()->generate(), - + "ip" => $schema->string()->addressIp()->generate() ]; $validate->check($source, $rules); //// check if the validation passed or not diff --git a/example/vardump.php b/example/vardump.php index 7c8080d..ed869e7 100644 --- a/example/vardump.php +++ b/example/vardump.php @@ -3,5 +3,7 @@ * Copyright (c) 2023. Wepesi validation. * @author Boss Ibrahim Mussa */ -var_dump(["passed"=>$validate->passed()]); -var_dump(["errors"=>$validate->errors()]); \ No newline at end of file +var_dump([ + 'passed' => $validate->passed(), + 'errors' => $validate->errors() +]); \ No newline at end of file diff --git a/index.php b/index.php index 6f28ca5..35c23fc 100644 --- a/index.php +++ b/index.php @@ -7,14 +7,4 @@ use Wepesi\App\Validator\StringValidator; require_once __DIR__."/vendor/autoload.php"; -//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); -} +include __DIR__.'/example/index.php'; diff --git a/src/Resolver/Option.php b/src/Resolver/Option.php deleted file mode 100644 index 56b2b87..0000000 --- a/src/Resolver/Option.php +++ /dev/null @@ -1,104 +0,0 @@ -hasDefaultValue = false; - $this->name = $name; - } - - /** - * @return string - */ - public function getName(): string - { - return $this->name; - } - - /** - * @return mixed - */ - public function getDefaultValue() - { - return $this->defaultValue; - } - - /** - * @param mixed $defaultValue - * @return Option - */ - public function setDefaultValue($defaultValue): self - { - $this->hasDefaultValue = true; - $this->defaultValue = $defaultValue; - return $this; - } - - /** - * @return bool - */ - public function hasDefaultValue(): bool - { - return $this->hasDefaultValue; - } - - /** - * @param Closure $closure - * @return $this - */ - public function validator(Closure $closure): self - { - $this->validator = $closure; - return $this; - } - - /** - * @param $value - * @return bool - */ - public function isValid($value): bool - { - if ($this->validator instanceof Closure) { - $validator = $this->validator; - return $validator($value); - } - return true; - } -} \ No newline at end of file diff --git a/src/Resolver/OptionsResolver.php b/src/Resolver/OptionsResolver.php deleted file mode 100644 index f20c005..0000000 --- a/src/Resolver/OptionsResolver.php +++ /dev/null @@ -1,135 +0,0 @@ -options = new ArrayObject(); - foreach ($options as $option) { - $this->add($option); - } - } - - /** - * @param array $options - * @return array - */ - public function resolve(array $options): array - { - try { - $checkDiff = $this->checkDiff($options); - if (isset($checkDiff['exception'])) { - return $checkDiff; - } - /** - * @var Option $option - */ - $optionsResolved = []; - foreach ($this->options as $option) { - $optionName = $option->getName(); - 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))); - } - $optionsResolved[$optionName] = $value; - continue; - } - - if ($option->hasDefaultValue()) { - $optionsResolved[$optionName] = $option->getDefaultValue(); - continue; - } - - throw new InvalidArgumentException(sprintf('The required option "%s" is missing.', $optionName)); - } - return $optionsResolved; - } catch (Exception $ex) { - return $this->exception($ex); - } - } - - /** - * @param Option $option - * @return void - */ - private function add(Option $option): void - { - $this->options->offsetSet($option->getName(), $option); - } - - /** - * @param array $options - * @return array - */ - private function checkDiff(array $options): array - { - try { - $defined = $this->options->getArrayCopy(); - $diff = array_diff_key($options, $defined); - if (count($diff) > 0) { - 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) { - return $this->exception($ex); - } - } - - /** - * @param $value - * @return string - */ - private static function formatValue($value): string - { - if (is_object($value)) { - return get_class($value); - } - - if (is_string($value)) { - return '"' . $value . '"'; - } - - if (false === $value) { - return 'false'; - } - - if (true === $value) { - return 'true'; - } - - return gettype($value); - } -} \ No newline at end of file diff --git a/src/Validate.php b/src/Validate.php index b265305..8f6f825 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -6,11 +6,8 @@ namespace Wepesi\App; -use Exception; -use ReflectionClass; -use Wepesi\App\Resolver\Option; -use Wepesi\App\Resolver\OptionsResolver; - +use Wepesi\Resolver\OptionsResolver; +use Wepesi\Resolver\Option; /** * */ @@ -53,23 +50,27 @@ function check(array $resource, array $schema) } $resolver = new OptionsResolver($option_resolver); $options = $resolver->resolve($schema); - if (isset($options["exception"])) { + + $exceptions = isset($options['exception']) || isset($options['InvalidArgumentException']) ?? false; + if ($exceptions) { $message = [ 'type' => 'object.unknown', - 'message' => $options["exception"], + 'message' => $options['exception'] ?? $options['InvalidArgumentException'], 'label' => "exception", ]; $this->addError($message); } 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) && is_object($rules)) { + if(!$rules->generate()){ + throw new \Exception("Schema rule is not a valid schema! method generate does not exist"); + } + $rules = $rules->generate(); } $class_namespace = array_keys($rules)[0]; if ($class_namespace == "any") continue; - $validator_class = str_replace("Schema", "Validator", $class_namespace); - $reflexion = new ReflectionClass($validator_class); + $reflexion = new \ReflectionClass($validator_class); $instance = $reflexion->newInstance($item, $resource); @@ -87,7 +88,7 @@ function check(array $resource, array $schema) $this->passed = true; } } - } catch (Exception $ex) { + } catch (\Exception $ex) { die($ex); } } diff --git a/src/Validator/StringValidator.php b/src/Validator/StringValidator.php index 8f22215..75c2fe8 100644 --- a/src/Validator/StringValidator.php +++ b/src/Validator/StringValidator.php @@ -20,7 +20,7 @@ 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; @@ -123,9 +123,9 @@ public function match(string $key_to_match) * @param string $ip_address * @return void */ - public function addressIp(string $ip_address) + public function addressIp() { - if (!filter_var($this->data_source[$ip_address], FILTER_VALIDATE_IP)) { + if (!filter_var($this->field_value, FILTER_VALIDATE_IP)) { $message = [ 'type' => 'string.ip_address', 'message' => "`$this->field_name` is not a valid Ip address", @@ -141,10 +141,10 @@ public function addressIp(string $ip_address) */ public function addressIpv6(string $ip_address) { - if (!filter_var($this->data_source[$ip_address], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { + if (!filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { $message = [ 'type' => 'string.ip_address_v6', - 'message' => "`$this->field_name` should be an ip address (ipv6)", + 'message' => "`$this->field_name` is not a valid ip address (ipv6)", 'label' => $this->field_name, ]; $this->addError($message);