Skip to content

Commit

Permalink
Merge pull request #61 from lion-packages/new
Browse files Browse the repository at this point in the history
Rule Validation
  • Loading branch information
Santiago1010 authored May 7, 2024
2 parents 44af89e + 197ae6b commit 728066b
Show file tree
Hide file tree
Showing 20 changed files with 667 additions and 353 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
},
"require-dev": {
"lion/test": "^1.1",
"phpunit/phpunit": "^10.1"
"phpunit/phpunit": "^11.1"
},
"suggest": {
"ext-gd": "Required to use LionSpreadsheet.",
Expand Down
452 changes: 230 additions & 222 deletions composer.lock

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<directory>tests/Enums/</directory>
</testsuite>

<testsuite name="Exceptions">
<directory>tests/Exceptions/</directory>
</testsuite>

<testsuite name="Kernel">
<directory>tests/Kernel/</directory>
</testsuite>

<testsuite name="Helpers">
<directory>tests/Helpers/</directory>
</testsuite>
Expand Down
6 changes: 4 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
require_once(__DIR__ . '/../vendor/autoload.php');

use Lion\Bundle\Helpers\ExceptionCore;
use Lion\Bundle\HttpKernel;
use Lion\Bundle\Kernel\HttpKernel;
use Lion\Dependency\Injection\Container;

(new ExceptionCore)->exceptionHandler();

((new Container)->injectDependencies(new HttpKernel))->validateRules();
include_once(__DIR__ . '/../routes/rules.php');

((new Container())->injectDependencies(new HttpKernel()))->validateRules();

include_once(__DIR__ . '/../routes/middleware.php');
include_once(__DIR__ . '/../routes/web.php');
2 changes: 1 addition & 1 deletion routes/rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

Routes::setRules([
Route::GET => [],
Route::POST => []
Route::POST => [],
]);
67 changes: 48 additions & 19 deletions src/LionBundle/Commands/Lion/Migrations/FreshMigrationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,27 @@
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Drop all tables and re-run all migrations
*
* @property OutputInterface $output [OutputInterface is the interface
* implemented by all Output classes]
* @property Container $container [Container class object]
* @property Store $store [Store class object]
*
* @package Lion\Bundle\Commands\Lion\Migrations
*/
class FreshMigrationsCommand extends Command
{
/**
* [OutputInterface is the interface implemented by all Output classes]
*
* @var OutputInterface $output
*/
private OutputInterface $output;

/**
* [Container class object]
*
Expand Down Expand Up @@ -72,6 +83,29 @@ protected function configure(): void
->addOption('seed', 's', InputOption::VALUE_OPTIONAL, 'Do you want to run the seeds?', 'none');
}

/**
* Initializes the command after the input has been bound and before the
* input is validated
*
* This is mainly useful when a lot of commands extends one main command
* where some things need to be initialized based on the input arguments and
* options
*
* @param InputInterface $input [InputInterface is the interface implemented
* by all input classes]
* @param OutputInterface $output [OutputInterface is the interface
* implemented by all Output classes]
*
* @see InputInterface::bind()
* @see InputInterface::validate()
*
* @return void
*/
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->output = $output;
}

/**
* Executes the current command
*
Expand Down Expand Up @@ -99,7 +133,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::FAILURE;
}

$this->dropTables($output);
$this->dropTables();

/** @var array<string, array<string, MigrationUpInterface>> $migrations */
$migrations = $this->getMigrations();
Expand All @@ -110,11 +144,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::INVALID;
}

$this->executeMigrations($output, $this->orderList($migrations[TableInterface::class]));
$this->executeMigrations($this->orderList($migrations[TableInterface::class]));

$this->executeMigrations($output, $migrations[ViewInterface::class]);
$this->executeMigrations($migrations[ViewInterface::class]);

$this->executeMigrations($output, $migrations[StoreProcedureInterface::class]);
$this->executeMigrations($migrations[StoreProcedureInterface::class]);

$output->writeln($this->infoOutput("\n\t>> Migrations executed successfully"));

Expand Down Expand Up @@ -172,22 +206,19 @@ private function getMigrations(): array
/**
* Clears all tables of all available connections
*
* @param OutputInterface $output [OutputInterface is the interface
* implemented by all Output classes]
*
* @return void
*/
private function dropTables(OutputInterface $output): void
private function dropTables(): void
{
$connections = (object) Schema::getConnections();

foreach ($connections->connections as $connection) {
$response = Schema::dropTables()->execute();

if (isError($response)) {
$output->writeln($this->warningOutput("\t>> DATABASE: {$connection->dbname}"));
$this->output->writeln($this->warningOutput("\t>> DATABASE: {$connection->dbname}"));

$output->writeln($this->errorOutput("\t>> DATABASE: {$response->message}"));
$this->output->writeln($this->errorOutput("\t>> DATABASE: {$response->message}"));
}
}
}
Expand Down Expand Up @@ -223,27 +254,25 @@ private function orderList(array $files): array
/**
* Run the migrations
*
* @param OutputInterface $output [OutputInterface is the interface
* implemented by all Output classes]
* @param array<int, MigrationUpInterface> $files [description]
*
* @return [type] [description]
* @return void
*/
private function executeMigrations(Output $output, array $files): void
private function executeMigrations(array $files): void
{
foreach ($files as $namespace => $classObject) {
if ($classObject instanceof MigrationUpInterface) {
/** @var MigrationUpInterface $classObject */
$response = $classObject->up();

if (isError($response)) {
$output->writeln($this->warningOutput("\t>> MIGRATION: {$namespace}"));
$this->output->writeln($this->warningOutput("\t>> MIGRATION: {$namespace}"));

$output->writeln($this->errorOutput("\t>> MIGRATION: {$response->message}"));
$this->output->writeln($this->errorOutput("\t>> MIGRATION: {$response->message}"));
} else {
$output->writeln($this->warningOutput("\t>> MIGRATION: {$namespace}"));
$this->output->writeln($this->warningOutput("\t>> MIGRATION: {$namespace}"));

$output->writeln($this->successOutput("\t>> MIGRATION: {$response->message}"));
$this->output->writeln($this->successOutput("\t>> MIGRATION: {$response->message}"));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/LionBundle/Commands/Lion/New/RulesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class {$class} extends Rules implements RulesInterface
*/
public function passes(): void
{
\$this->validate(function(Validator \$validator) {
\$this->validate(function (Validator \$validator): void {
\$validator
->rule('required', \$this->field)
->message('the "" property is required');
Expand Down
31 changes: 18 additions & 13 deletions src/LionBundle/Enums/StatusResponseEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,72 @@
enum StatusResponseEnum: string
{
/**
* Represents a correct response object
* [Represents a correct response object]
*/
case SUCCESS = Response::SUCCESS;

/**
* Represents an error response object
* [Represents an error response object]
*/
case ERROR = Response::ERROR;

/**
* Represents a warning response object
* [Represents a warning response object]
*/
case WARNING = Response::WARNING;

/**
* Represents an information response object
* [Represents an information response object]
*/
case INFO = Response::INFO;

/**
* Represents a database error response object
* [Represents a database error response object]
*/
case DATABASE_ERROR = Response::DATABASE_ERROR;

/**
* Represents a sesion error response object
* [Represents a sesion error response object]
*/
case SESSION_ERROR = Response::SESSION_ERROR;

/**
* Represents a route error response object
* [Represents a route error response object]
*/
case ROUTE_ERROR = Response::ROUTE_ERROR;

/**
* Represents a file error response object
* [Represents a file error response object]
*/
case FILE_ERROR = Response::FILE_ERROR;

/**
* Represents a mail error response object
* [Represents a mail error response object]
*/
case MAIL_ERROR = Response::MAIL_ERROR;

/**
* [Represents a rules error response object]
*/
case RULE_ERROR = 'rule-error';

/**
* Return a list with the different types of responses available
*
* @return array<string>
* @return array<int, string>
*/
public static function values(): array
{
return array_map(fn(object $value) => $value->value, self::cases());
return array_map(fn (object $value) => $value->value, self::cases());
}

/**
* Return a list of available errors
*
* @return array<string>
* @return array<int, string>
*/
public static function errors(): array
{
return (new Response)->getErrors();
return [...(new Response)->getErrors(), self::RULE_ERROR->value];
}
}
26 changes: 26 additions & 0 deletions src/LionBundle/Exceptions/RulesException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Lion\Bundle\Exceptions;

use Exception;
use JsonSerializable;

/**
* Description of 'ExampleException'
*
* @package Lion\Bundle\Exceptions
*/
class RulesException extends Exception implements JsonSerializable
{
/**
* {@inheritdoc}
*/
public function jsonSerialize(): mixed
{
$json = json_decode($this->getMessage());

return error($json->message, $this->getCode(), $json->data);
}
}
33 changes: 23 additions & 10 deletions src/LionBundle/Helpers/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
namespace Lion\Bundle\Helpers;

use Closure;
use Lion\Bundle\Enums\LogTypeEnum;
use Lion\Request\Request;
use Lion\Security\Validation;

/**
* Define the rules and execute their validations
*
* @property Validation $validation [Validation class object]
* @property Request $request [Allows you to obtain data captured in an HTTP
* request and modify headers]
* @property array $responses [Array containing all answers]
*
* @package Lion\Bundle\Helpers
Expand All @@ -25,6 +27,13 @@ abstract class Rules
*/
private Validation $validation;

/**
* Allows you to obtain data captured in an HTTP request and modify headers
*
* @var Request $request
*/
private Request $request;

/**
* [Array containing all answers]
*
Expand All @@ -40,6 +49,14 @@ public function setValidation(Validation $validation): void
$this->validation = $validation;
}

/**
* @required
*/
public function setRequest(Request $request): void
{
$this->request = $request;
}

/**
* Executes the validation of the Validate object of Validator
*
Expand All @@ -50,22 +67,18 @@ public function setValidation(Validation $validation): void
*/
protected function validate(Closure $validateFunction): void
{
$response = $this->validation->validate((array) request, $validateFunction);
$response = $this->validation->validate((array) $this->request->capture(), $validateFunction);

$this->responses = isError($response) ? $response->messages : [];
}

/**
* Shows the available error responses and adds them to the log record
* Gets the list of rule errors
*
* @return void
* @return array
*/
public function display(): void
public function getErrors(): array
{
foreach ($this->responses as $errors) {
logger($errors[0], LogTypeEnum::ERROR);

finish(error($errors[0]));
}
return $this->responses;
}
}
Loading

0 comments on commit 728066b

Please sign in to comment.