diff --git a/middleware/Validation/exampleValidation.php b/middleware/Validation/exampleValidation.php index c5c4ad3..2e99b6d 100644 --- a/middleware/Validation/exampleValidation.php +++ b/middleware/Validation/exampleValidation.php @@ -9,20 +9,20 @@ class exampleValidation extends MiddleWare { function changeLang() { - $rules = [ - "token" => $this->schema->string("token") + $schema = [ + "token" => $this->rule->string("token") ->min(5) ->max(30) ->required(), - "lang" => $this->schema->string("lang") + "lang" => $this->rule->string("lang") ->min(1) ->max(2) ->required() ]; - $this->validate->check($_POST, $rules); + $this->validate->check($_POST, $schema); if (!$this->validate->passed()) { - Application::dumper($this->validate->errors()); + print_r($this->validate->errors()); } } } \ No newline at end of file diff --git a/src/Core/Event/EventEmitter.php b/src/Core/Event/EventEmitter.php index 3af09bb..f492423 100644 --- a/src/Core/Event/EventEmitter.php +++ b/src/Core/Event/EventEmitter.php @@ -2,21 +2,36 @@ namespace Wepesi\Core\Event; -use Wepesi\Core\{listener}; - +/** + * + */ class EventEmitter { - private static $_instance; - private $listeners = []; + /** + * @var EventEmitter + */ + private static EventEmitter $instance; + /** + * @var array + */ + private array $listeners = []; + /** + * @return EventEmitter + */ static function getInstance(): EventEmitter { - if (!self::$_instance) { - self::$_instance = new self(); + if (!self::$instance) { + self::$instance = new self(); } - return self::$_instance; + return self::$instance; } + /** + * @param string $event + * @param ...$args + * @return void + */ function emit(string $event, ...$args) { if ($this->hasListeners($event)) { @@ -29,17 +44,33 @@ function emit(string $event, ...$args) } } + /** + * @param string $event + * @return bool + */ private function hasListeners(string $event): bool { return array_key_exists($event, $this->listeners); } + /** + * @param string $event + * @param callable $callback + * @param int $priority + * @return Listener + */ function once(string $event, callable $callback, int $priority = 0): Listener { return $this->on($event, $callback, $priority)->once(); } + /** + * @param string $event + * @param callable $callback + * @param int $priority + * @return Listener + */ function on(string $event, callable $callback, int $priority = 0): Listener { if (!$this->hasListeners($event)) { @@ -51,6 +82,10 @@ function on(string $event, callable $callback, int $priority = 0): Listener return $listener; } + /** + * @param $event + * @return void + */ private function sortListener($event) { uasort($this->listeners[$event], function ($a, $b) { diff --git a/src/Core/Event/Listener.php b/src/Core/Event/Listener.php index 7eef1b9..66ef63d 100644 --- a/src/Core/Event/Listener.php +++ b/src/Core/Event/Listener.php @@ -2,20 +2,46 @@ namespace Wepesi\Core\Event; +/** + * + */ class Listener { - public $stopPropagation = false; - private $callback, - $priority, - $once, - $calls = 0; + /** + * @var bool + */ + public bool $stopPropagation = false; + /** + * @var callable + */ + private $callback; + /** + * @var int + */ + private int $priority; + /** + * @var int + */ + private int $once; + /** + * @var int + */ + private int $calls = 0; + /** + * @param callable $callback + * @param int $priority + */ function __construct(callable $callback, int $priority) { $this->callback = $callback; $this->priority = $priority; } + /** + * @param array $args + * @return mixed|null + */ function handle(array $args) { if ($this->once && $this->calls > 0) { @@ -25,17 +51,26 @@ function handle(array $args) return call_user_func_array($this->callback, $args); } + /** + * @return $this + */ function once(): Listener { $this->once = true; return $this; } - function getPriority() + /** + * @return int + */ + function getPriority(): int { return $this->priority; } + /** + * @return $this + */ function stopPropagation(): Listener { $this->stopPropagation = true; diff --git a/src/Core/Exceptions/ValidationException.php b/src/Core/Exceptions/ValidationException.php new file mode 100644 index 0000000..c40ab5b --- /dev/null +++ b/src/Core/Exceptions/ValidationException.php @@ -0,0 +1,13 @@ + $schema->string()->url()]; + $schema = ['link' => $rule->string()->url()]; $source = ['link' => $location]; if ($location) { // check if the location is an url - $validate->check($source, $rules); + $validate->check($source, $schema); if ($validate->passed()) { // Redirect a url header('Location:' . $location, true, 301); diff --git a/src/Core/MiddleWare.php b/src/Core/MiddleWare.php index 5e7e3d7..8d9f67c 100644 --- a/src/Core/MiddleWare.php +++ b/src/Core/MiddleWare.php @@ -5,17 +5,17 @@ namespace Wepesi\Core; -use Wepesi\Core\Validation\Schema; +use Wepesi\Core\Validation\Rules; use Wepesi\Core\Validation\Validate; abstract class MiddleWare { protected Validate $validate; - protected Schema $schema; + protected Rules $rule; public function __construct() { - $this->schema = new Schema(); + $this->rule = new Rules(); $this->validate = new Validate(); } } \ No newline at end of file diff --git a/src/Core/Validation/MessageErrorBuilder.php b/src/Core/Validation/MessageErrorBuilder.php index d6645a7..c1d4834 100644 --- a/src/Core/Validation/MessageErrorBuilder.php +++ b/src/Core/Validation/MessageErrorBuilder.php @@ -30,7 +30,7 @@ public function __construct() * @param string $value * @return $this */ - public function type(string $value): MessageErrorBuilder + public function type(string $value): MessageBuilderContracts { $this->items['type'] = $value; return $this; @@ -40,7 +40,7 @@ public function type(string $value): MessageErrorBuilder * @param string $value * @return $this */ - public function message(string $value): MessageErrorBuilder + public function message(string $value): MessageBuilderContracts { $this->items['message'] = $value; return $this; @@ -50,7 +50,7 @@ public function message(string $value): MessageErrorBuilder * @param string $value * @return $this */ - public function label(string $value): MessageErrorBuilder + public function label(string $value): MessageBuilderContracts { $this->items['label'] = $value; return $this; @@ -60,7 +60,7 @@ public function label(string $value): MessageErrorBuilder * @param string $value * @return $this */ - public function limit(string $value): MessageErrorBuilder + public function limit(string $value): MessageBuilderContracts { $this->items['limit'] = $value; return $this; diff --git a/src/Core/Validation/Providers/Contracts/MessageBuilderContracts.php b/src/Core/Validation/Providers/Contracts/MessageBuilderContracts.php index 9eae2ec..b96e137 100644 --- a/src/Core/Validation/Providers/Contracts/MessageBuilderContracts.php +++ b/src/Core/Validation/Providers/Contracts/MessageBuilderContracts.php @@ -2,7 +2,32 @@ namespace Wepesi\Core\Validation\Providers\Contracts; +/** + * + */ interface MessageBuilderContracts { + /** + * @param string $value + * @return MessageBuilderContracts + */ + public function message(string $value): MessageBuilderContracts; + /** + * @param string $value + * @return MessageBuilderContracts + */ + public function type(string $value): MessageBuilderContracts; + + /** + * @param string $value + * @return MessageBuilderContracts + */ + public function label(string $value): MessageBuilderContracts; + + /** + * @param string $value + * @return MessageBuilderContracts + */ + public function limit(string $value): MessageBuilderContracts; } \ No newline at end of file diff --git a/src/Core/Validation/Providers/Contracts/SchemaContracts.php b/src/Core/Validation/Providers/Contracts/RulesValidationContracts.php similarity index 78% rename from src/Core/Validation/Providers/Contracts/SchemaContracts.php rename to src/Core/Validation/Providers/Contracts/RulesValidationContracts.php index bd45676..7c843b7 100644 --- a/src/Core/Validation/Providers/Contracts/SchemaContracts.php +++ b/src/Core/Validation/Providers/Contracts/RulesValidationContracts.php @@ -10,7 +10,7 @@ /** * */ -interface SchemaContracts extends Contracts +interface RulesValidationContracts extends ValidationContracts { /** diff --git a/src/Core/Validation/Providers/Contracts/ValidatorContracts.php b/src/Core/Validation/Providers/Contracts/ValidateRulesContracts.php similarity index 70% rename from src/Core/Validation/Providers/Contracts/ValidatorContracts.php rename to src/Core/Validation/Providers/Contracts/ValidateRulesContracts.php index 571bae4..a9e6d96 100644 --- a/src/Core/Validation/Providers/Contracts/ValidatorContracts.php +++ b/src/Core/Validation/Providers/Contracts/ValidateRulesContracts.php @@ -10,13 +10,13 @@ /** * */ -interface ValidatorContracts extends Contracts +interface ValidateRulesContracts extends ValidationContracts { /** * @param array $value * @return mixed */ - public function addError(array $value); + public function addError(MessageBuilderContracts $value); /** * @return array diff --git a/src/Core/Validation/Providers/Contracts/Contracts.php b/src/Core/Validation/Providers/Contracts/ValidationContracts.php similarity index 93% rename from src/Core/Validation/Providers/Contracts/Contracts.php rename to src/Core/Validation/Providers/Contracts/ValidationContracts.php index 8f7a7e5..d07317f 100644 --- a/src/Core/Validation/Providers/Contracts/Contracts.php +++ b/src/Core/Validation/Providers/Contracts/ValidationContracts.php @@ -10,7 +10,7 @@ /** * */ -interface Contracts +interface ValidationContracts { /** * @param int $rule diff --git a/src/Core/Validation/Providers/SChemaProvider.php b/src/Core/Validation/Providers/RulesProvider.php similarity index 76% rename from src/Core/Validation/Providers/SChemaProvider.php rename to src/Core/Validation/Providers/RulesProvider.php index 3864308..29d4543 100644 --- a/src/Core/Validation/Providers/SChemaProvider.php +++ b/src/Core/Validation/Providers/RulesProvider.php @@ -6,12 +6,12 @@ namespace Wepesi\Core\Validation\Providers; -use Wepesi\Core\Validation\Providers\Contracts\SchemaContracts; +use Wepesi\Core\Validation\Providers\Contracts\RulesValidationContracts; /** * */ -abstract class SChemaProvider implements SchemaContracts +abstract class RulesProvider implements RulesValidationContracts { /** * @var array @@ -33,9 +33,9 @@ public function __construct(string $class_name) /** * @param int $rule - * @return SChemaProvider + * @return RulesProvider */ - public function min(int $rule): SChemaProvider + public function min(int $rule): RulesProvider { $this->schema[$this->class_name]["min"] = $rule; return $this; @@ -45,7 +45,7 @@ public function min(int $rule): SChemaProvider * @param $rule * @return $this */ - public function max($rule): SChemaProvider + public function max($rule): RulesProvider { $this->schema[$this->class_name]["max"] = $rule; return $this; @@ -54,7 +54,7 @@ public function max($rule): SChemaProvider /** * @return $this */ - public function required(): SChemaProvider + public function required(): RulesProvider { $this->schema[$this->class_name]["required"] = true; return $this; diff --git a/src/Core/Validation/Providers/ValidatorProvider.php b/src/Core/Validation/Providers/ValidatorProvider.php index f5aa877..dbe7b22 100644 --- a/src/Core/Validation/Providers/ValidatorProvider.php +++ b/src/Core/Validation/Providers/ValidatorProvider.php @@ -7,12 +7,13 @@ namespace Wepesi\Core\Validation\Providers; use Wepesi\Core\Validation\MessageErrorBuilder; -use Wepesi\Core\Validation\Providers\Contracts\Contracts; +use Wepesi\Core\Validation\Providers\Contracts\MessageBuilderContracts; +use Wepesi\Core\Validation\Providers\Contracts\ValidateRulesContracts; /** * Validator provider model */ -abstract class ValidatorProvider implements Contracts +abstract class ValidatorProvider implements ValidateRulesContracts { /** * @var array @@ -31,9 +32,9 @@ abstract class ValidatorProvider implements Contracts */ protected $field_value; /** - * @var MessageErrorBuilder + * @var MessageBuilderContracts */ - protected MessageErrorBuilder $messageItem; + protected MessageBuilderContracts $messageItem; /** * @@ -98,10 +99,10 @@ abstract protected function classProvider(): string; /** * - * @param array $value + * @param MessageBuilderContracts $item * @return void */ - public function addError(MessageErrorBuilder $item): void + public function addError(MessageBuilderContracts $item): void { $this->errors[] = $item->generate(); } diff --git a/src/Core/Validation/Rules.php b/src/Core/Validation/Rules.php new file mode 100644 index 0000000..3928f3e --- /dev/null +++ b/src/Core/Validation/Rules.php @@ -0,0 +1,69 @@ + true]; + } + + /** + * @return StringRules + */ + public function string(): StringRules + { + return new StringRules(); + } + + /** + * @return NumberRules + */ + public function number(): NumberRules + { + return new NumberRules(); + } + + /** + * @return DateRules + */ + public function date(): DateRules + { + return new DateRules(); + } + + /** + * @return BooleanRules + */ + public function boolean(): BooleanRules + { + return new BooleanRules(); + } + + /** + * @return ArrayRules + */ + public function array(): ArrayRules + { + return new ArrayRules(); + } + // TODO add support for file validation +} \ No newline at end of file diff --git a/src/Core/Validation/Schema/ArraySchema.php b/src/Core/Validation/Rules/ArrayRules.php similarity index 72% rename from src/Core/Validation/Schema/ArraySchema.php rename to src/Core/Validation/Rules/ArrayRules.php index fe42841..563f2df 100644 --- a/src/Core/Validation/Schema/ArraySchema.php +++ b/src/Core/Validation/Rules/ArrayRules.php @@ -4,14 +4,14 @@ * @author Boss Ibrahim Mussa */ -namespace Wepesi\Core\Validation\Schema; +namespace Wepesi\Core\Validation\Rules; -use Wepesi\Core\Validation\Providers\SChemaProvider; +use Wepesi\Core\Validation\Providers\RulesProvider; /** - * Arraye schema validation + * Array schema validation */ -final class ArraySchema extends SChemaProvider +final class ArrayRules extends RulesProvider { /** * @@ -23,9 +23,9 @@ public function __construct() /** * @param array $elements data array to be validated - * @return $this + * @return $this|false */ - public function structure(array $elements): ?ArraySchema + public function structure(array $elements): ?ArrayRules { if (isset($this->schema[$this->class_name]['string']) || isset($this->schema[$this->class_name]['number'])) { return false; @@ -36,9 +36,9 @@ public function structure(array $elements): ?ArraySchema /** * check if array content are(is) string - * @return $this|null + * @return $this|false */ - public function string(): ?ArraySchema + public function string(): ?ArrayRules { if (isset($this->schema[$this->class_name]['number'])) { return false; @@ -48,9 +48,9 @@ public function string(): ?ArraySchema } /** - * @return $this|null + * @return $this|false */ - public function number(): ?ArraySchema + public function number(): ?ArrayRules { if (isset($this->schema[$this->class_name]['string'])) { return false; diff --git a/src/Core/Validation/Schema/BooleanSchema.php b/src/Core/Validation/Rules/BooleanRules.php similarity index 75% rename from src/Core/Validation/Schema/BooleanSchema.php rename to src/Core/Validation/Rules/BooleanRules.php index 6d75a74..72a1212 100644 --- a/src/Core/Validation/Schema/BooleanSchema.php +++ b/src/Core/Validation/Rules/BooleanRules.php @@ -4,21 +4,21 @@ * @author Boss Ibrahim Mussa */ -namespace Wepesi\Core\Validation\Schema; +namespace Wepesi\Core\Validation\Rules; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ -use Wepesi\Core\Validation\Providers\SChemaProvider; +use Wepesi\Core\Validation\Providers\RulesProvider; /** * Description of String * * @author Domeshow */ -final class BooleanSchema extends SChemaProvider +final class BooleanRules extends RulesProvider { /** @@ -32,7 +32,7 @@ function __construct() /** * @return $this */ - function isValid(): BooleanSchema + function isValid(): BooleanRules { $this->schema[$this->class_name]['isValid'] = true; return $this; diff --git a/src/Core/Validation/Schema/DateSchema.php b/src/Core/Validation/Rules/DateRules.php similarity index 71% rename from src/Core/Validation/Schema/DateSchema.php rename to src/Core/Validation/Rules/DateRules.php index 7e70dae..c4a706d 100644 --- a/src/Core/Validation/Schema/DateSchema.php +++ b/src/Core/Validation/Rules/DateRules.php @@ -4,15 +4,15 @@ * @author Boss Ibrahim Mussa */ -namespace Wepesi\Core\Validation\Schema; +namespace Wepesi\Core\Validation\Rules; -use Wepesi\Core\Validation\Providers\SChemaProvider; +use Wepesi\Core\Validation\Providers\RulesProvider; /** * Schema datetime * */ -final class DateSchema extends SChemaProvider +final class DateRules extends RulesProvider { /** * @@ -24,9 +24,9 @@ function __construct() /** * @param string $rule - * @return DateSchema + * @return DateRules */ - public function min($rule): DateSchema + public function min($rule): DateRules { $this->schema[$this->class_name]['min'] = $rule; return $this; @@ -36,7 +36,7 @@ public function min($rule): DateSchema * @param string $rule * @return $this */ - public function max($rule): DateSchema + public function max($rule): DateRules { $this->schema[$this->class_name]['max'] = $rule; return $this; @@ -45,7 +45,7 @@ public function max($rule): DateSchema /** * @return $this */ - function now(): DateSchema + function now(): DateRules { $this->schema[$this->class_name]["now"] = true; return $this; @@ -54,7 +54,7 @@ function now(): DateSchema /** * @return $this */ - function today(): DateSchema + function today(): DateRules { $this->schema[$this->class_name]["today"] = true; return $this; diff --git a/src/Core/Validation/Schema/NumberSchema.php b/src/Core/Validation/Rules/NumberRules.php similarity index 63% rename from src/Core/Validation/Schema/NumberSchema.php rename to src/Core/Validation/Rules/NumberRules.php index 5755ef4..5aa9a36 100644 --- a/src/Core/Validation/Schema/NumberSchema.php +++ b/src/Core/Validation/Rules/NumberRules.php @@ -4,15 +4,15 @@ * @author Boss Ibrahim Mussa */ -namespace Wepesi\Core\Validation\Schema; +namespace Wepesi\Core\Validation\Rules; -use Wepesi\Core\Validation\Providers\SChemaProvider; +use Wepesi\Core\Validation\Providers\RulesProvider; /** * Schema number validation - * validate any format number + * validates any format number */ -final class NumberSchema extends SChemaProvider +final class NumberRules extends RulesProvider { /** @@ -26,7 +26,7 @@ function __construct() /** * @return $this */ - function positive(): NumberSchema + function positive(): NumberRules { $this->schema[$this->class_name]['positive'] = true; return $this; diff --git a/src/Core/Validation/Schema/StringSchema.php b/src/Core/Validation/Rules/StringRules.php similarity index 75% rename from src/Core/Validation/Schema/StringSchema.php rename to src/Core/Validation/Rules/StringRules.php index 6245cf7..6c3f23d 100644 --- a/src/Core/Validation/Schema/StringSchema.php +++ b/src/Core/Validation/Rules/StringRules.php @@ -4,21 +4,21 @@ * @author Boss Ibrahim Mussa */ -namespace Wepesi\Core\Validation\Schema; +namespace Wepesi\Core\Validation\Rules; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ -use Wepesi\Core\Validation\Providers\SChemaProvider; +use Wepesi\Core\Validation\Providers\RulesProvider; /** - * String valiation schema + * String validation rules * validate string value */ -final class StringSchema extends SChemaProvider +final class StringRules extends RulesProvider { /** @@ -32,7 +32,7 @@ public function __construct() /** * @return $this */ - public function email(): StringSchema + public function email(): StringRules { $this->schema[$this->class_name]["email"] = true; return $this; @@ -42,7 +42,7 @@ public function email(): StringSchema * * @return $this */ - public function url(): StringSchema + public function url(): StringRules { $this->schema[$this->class_name]["url"] = true; return $this; @@ -53,7 +53,7 @@ public function url(): StringSchema * @param string $key_to_match * @return $this */ - public function match(string $key_to_match): StringSchema + public function match(string $key_to_match): StringRules { $this->schema[$this->class_name]["match"] = $key_to_match; return $this; @@ -63,7 +63,7 @@ public function match(string $key_to_match): StringSchema * @param bool $ipv6 * @return $this */ - public function addressIp(bool $ipv6 = false): StringSchema + public function addressIp(bool $ipv6 = false): StringRules { if ($ipv6) { $this->schema[$this->class_name]['addressIpv6'] = true; @@ -77,7 +77,7 @@ public function addressIp(bool $ipv6 = false): StringSchema * @param string $table_name * @return $this */ - public function unique(string $table_name): StringSchema + public function unique(string $table_name): StringRules { $this->schema[$this->class_name]['unique'] = $table_name; return $this; diff --git a/src/Core/Validation/Schema.php b/src/Core/Validation/Schema.php deleted file mode 100644 index 25e500b..0000000 --- a/src/Core/Validation/Schema.php +++ /dev/null @@ -1,69 +0,0 @@ - true]; - } - - /** - * @return StringSchema - */ - public function string(): StringSchema - { - return new StringSchema(); - } - - /** - * @return NumberSchema - */ - public function number(): NumberSchema - { - return new NumberSchema(); - } - - /** - * @return DateSchema - */ - public function date(): DateSchema - { - return new DateSchema(); - } - - /** - * @return BooleanSchema - */ - public function boolean(): BooleanSchema - { - return new BooleanSchema(); - } - - /** - * @return ArraySchema - */ - public function array(): ArraySchema - { - return new ArraySchema(); - } - // TODO add support for file validation -} \ No newline at end of file diff --git a/src/Core/Validation/Traits/InitTrait.php b/src/Core/Validation/Traits/InitTrait.php index dff231e..ade2638 100644 --- a/src/Core/Validation/Traits/InitTrait.php +++ b/src/Core/Validation/Traits/InitTrait.php @@ -6,28 +6,30 @@ namespace Wepesi\Core\Validation\Traits; +use Wepesi\Core\Exceptions\ValidationException; -use Exception; - +/** + * + */ trait InitTrait { /** * @param $source * @param $schema - * @throws Exception + * @throws ValidationException */ 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 ValidationException('Your Source Data should not be en empty array', 500); } if (!is_array($schema) || count($schema) == 0) { - throw new Exception('Your Schema should not be en empty array'); + throw new ValidationException('Your Schema should not be en empty array', 500); } $fields = array_keys($schema); if (!isset($source[$fields[0]])) { - throw new Exception('field not defined'); + throw new ValidationException('field not defined',500); } $this->extract_data($schema); diff --git a/src/Core/Validation/Validate.php b/src/Core/Validation/Validate.php index 6a40218..5798b54 100644 --- a/src/Core/Validation/Validate.php +++ b/src/Core/Validation/Validate.php @@ -7,6 +7,7 @@ namespace Wepesi\Core\Validation; use Wepesi\Core\Application; +use Wepesi\Core\Exceptions\ValidationException; use Wepesi\Core\Http\Response; use Wepesi\Core\Resolver\Option; use Wepesi\Core\Resolver\OptionsResolver; @@ -25,7 +26,7 @@ final class Validate * @var bool */ private bool $passed; - private MessageErrorBuilder $message; + private MessageBuilderContracts $message; /** * @@ -67,13 +68,13 @@ function check(array $resource, array $schema) foreach ($schema as $item => $rules) { 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"); + throw new ValidationException("This rule is not a valid! 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); + $validator_class = str_replace("Rules", "Validator", $class_namespace); $reflexion = new \ReflectionClass($validator_class); $instance = $reflexion->newInstance($item, $resource); @@ -92,7 +93,7 @@ function check(array $resource, array $schema) $this->passed = true; } } - } catch (\Exception $ex) { + } catch (ValidationException $ex) { Application::dumper($ex); Response::setStatusCode(500); exit;