- Complete GGZ Ecademy Laravel backend application - RESTful API for learning products, members, filters - Authentication and authorization system - Database migrations and seeders - Custom CRUD generator commands - Email notification system - Integration with frontend applications
This commit is contained in:
31
app/Data/Validation/NetteValidator.php
Normal file
31
app/Data/Validation/NetteValidator.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Data\Validation;
|
||||
|
||||
use Nette\Schema\Expect;
|
||||
use Nette\Schema\Processor;
|
||||
use Nette\Schema\Schema;
|
||||
use Nette\Schema\ValidationException;
|
||||
|
||||
class NetteValidator extends Validator implements ValidatesArray
|
||||
{
|
||||
private Processor $schemaProcessor;
|
||||
private Schema $schema;
|
||||
|
||||
public function __construct(Processor $schemaProcessor, Schema $schema)
|
||||
{
|
||||
$this->schemaProcessor = $schemaProcessor;
|
||||
$this->schema = $schema;
|
||||
}
|
||||
|
||||
public function validateArray(array $items): ValidationResult
|
||||
{
|
||||
try {
|
||||
$this->schemaProcessor->process($this->schema, $items);
|
||||
|
||||
return $this->success();
|
||||
} catch (ValidationException $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
12
app/Data/Validation/ValidatesArray.php
Normal file
12
app/Data/Validation/ValidatesArray.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Data\Validation;
|
||||
|
||||
interface ValidatesArray
|
||||
{
|
||||
/**
|
||||
* @param mixed[] $items
|
||||
* @return ValidationResult
|
||||
*/
|
||||
public function validateArray(array $items): ValidationResult;
|
||||
}
|
||||
8
app/Data/Validation/ValidationException.php
Normal file
8
app/Data/Validation/ValidationException.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Data\Validation;
|
||||
|
||||
class ValidationException extends \Exception
|
||||
{
|
||||
|
||||
}
|
||||
40
app/Data/Validation/ValidationResult.php
Normal file
40
app/Data/Validation/ValidationResult.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Data\Validation;
|
||||
|
||||
class ValidationResult
|
||||
{
|
||||
private bool $error;
|
||||
private ?string $errorMessage;
|
||||
|
||||
public function __construct(bool $error, ?string $errorMessage = null)
|
||||
{
|
||||
$this->error = $error;
|
||||
$this->errorMessage = $errorMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void|never
|
||||
*/
|
||||
final public function assertIsSuccess(): void
|
||||
{
|
||||
if ($this->isError()) {
|
||||
throw new ValidationException($this->getErrorMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function isError(): bool
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
public function isSuccess(): bool
|
||||
{
|
||||
return !$this->isError();
|
||||
}
|
||||
|
||||
public function getErrorMessage(): ?string
|
||||
{
|
||||
return $this->errorMessage;
|
||||
}
|
||||
}
|
||||
16
app/Data/Validation/Validator.php
Normal file
16
app/Data/Validation/Validator.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Data\Validation;
|
||||
|
||||
abstract class Validator
|
||||
{
|
||||
protected function error(string $message): ValidationResult
|
||||
{
|
||||
return new ValidationResult(true, $message);
|
||||
}
|
||||
|
||||
protected function success(): ValidationResult
|
||||
{
|
||||
return new ValidationResult(false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user