Skip to content

Commit

Permalink
Merge pull request #196 from kivudesign/refactor_validation
Browse files Browse the repository at this point in the history
Refactor validation
  • Loading branch information
bim-g authored Sep 2, 2024
2 parents f4e3e2f + 35e5d70 commit 724053b
Show file tree
Hide file tree
Showing 22 changed files with 272 additions and 160 deletions.
10 changes: 5 additions & 5 deletions middleware/Validation/exampleValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
49 changes: 42 additions & 7 deletions src/Core/Event/EventEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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) {
Expand Down
47 changes: 41 additions & 6 deletions src/Core/Event/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions src/Core/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/

namespace Wepesi\Core\Exceptions;

use Wepesi\Core\Exceptions\WepesiException;

class ValidationException extends WepesiException
{

}
8 changes: 4 additions & 4 deletions src/Core/Http/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Wepesi\Core\Http;

use Wepesi\Core\Escape;
use Wepesi\Core\Validation\Schema;
use Wepesi\Core\Validation\Rules;
use Wepesi\Core\Validation\Validate;

/**
Expand All @@ -17,13 +17,13 @@ class Redirect
*/
static function to($location = null)
{
$schema = new Schema();
$rule = new Rules();
$validate = new Validate();
$rules = ['link' => $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);
Expand Down
6 changes: 3 additions & 3 deletions src/Core/MiddleWare.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
8 changes: 4 additions & 4 deletions src/Core/Validation/MessageErrorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
*
*/
interface SchemaContracts extends Contracts
interface RulesValidationContracts extends ValidationContracts
{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
*
*/
interface Contracts
interface ValidationContracts
{
/**
* @param int $rule
Expand Down
Loading

0 comments on commit 724053b

Please sign in to comment.