Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimz22 committed Oct 4, 2021
0 parents commit 5d17b09
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.idea
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/**/contentModel.xml
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/**/gradle.xml
.idea/**/libraries
cmake-build-*/
.idea/**/mongoSettings.xml
*.iws
out/
.idea_modules/
atlassian-ide-plugin.xml
.idea/replstate.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
.idea/httpRequests
.idea/caches/build_file_checksums.ser
composer.phar
/vendor/
composer.lock

26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "vertigolabs/validation-aware",
"description": "validation aware traits",
"version": "1.0.0",
"minimum-stability": "stable",
"license": "MIT",
"authors": [
{
"name": "James Murray",
"email": "[email protected]"
}
],
"require": {
"php": ">=8.0.0",
"symfony/validator": "5.3.8"
}
,
"require-dev": {
"roave/security-advisories": "dev-latest"
},
"autoload": {
"psr-4": {
"VertigoLabs\\ValidationAware\\": "src"
}
}
}
17 changes: 17 additions & 0 deletions src/Exception/ValidationViolationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace VertigoLabs\ValidationAware\Exception;

use Exception;

class ValidationViolationException extends Exception
{
public function __construct(string $taskName, int $violationCount, string $logGroup = null)
{
$msg = sprintf('%s failed validation with %d violations', $taskName, $violationCount);
if (null !== $logGroup) {
$msg .= sprintf(' | Log Group: %s',$logGroup);
}
parent::__construct($msg);
}
}
98 changes: 98 additions & 0 deletions src/ValidationAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace VertigoLabs\ValidationAware;

use ReflectionClass;
use ReflectionException;
use RuntimeException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validation;

trait ValidationAware
{
/**
* @var bool The current validity status
*/
protected bool $valid = false;

abstract protected function log(string $message, ?int $level = null): void;

abstract public function getData($directive = null, $default = null);

/**
* Returns a collection of validation constraints
*
* @link https://symfony.com/doc/3.4/validation.html#constraints
*
* @return null|Collection A collection of constraints
*/
protected function defineValidationConstraints(): ?Collection
{
return null;
}

/**
* Returns the current validity of the data
*
* @return bool
*/
public function isValid(): bool
{
return $this->valid;
}

/**
* Validates input data.
*
* @return ConstraintViolationList
*/
final public function validate(): ConstraintViolationList
{
$this->log('Validation | Beginning');
$this->valid = false;
$validator = Validation::createValidator();

$constraints = $this->defineValidationConstraints();

if ($constraints === null) {
$this->valid = true;
$this->log('Validation | Skipped');

return new ConstraintViolationList();
}

if (!($constraints instanceof Constraint)) {
throw new RuntimeException(__CLASS__.'::defineValidationConstraints() must return an instance of '.Constraint::class.' or null');
}

/** @var ConstraintViolationList $violations */
$violations = $validator->validate($this->getData(), $constraints);

$this->valid = ($violations->count() === 0);

if (!$this->valid) {
$this->log('Validation | Violations found.');
foreach ($violations as $violation) {
$this->log(sprintf('Validation | %s: %s', $violation->getPropertyPath(), $violation->getMessage()));
}
}

$this->log('Validation | Completed');
return $violations;
}

/**
* @return array
* @throws ReflectionException
*/
final public static function getValidationConstraintFields(): array
{
$refl = new ReflectionClass(static::class);
$refl->getMethod('defineValidationConstraints')->setAccessible(true);
/** @var Collection $constraints */
$constraints = ($refl->newInstanceWithoutConstructor())->defineValidationConstraints();
return $constraints->fields;
}
}
11 changes: 11 additions & 0 deletions src/ValidationAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace VertigoLabs\ValidationAware;

use Symfony\Component\Validator\ConstraintViolationList;

interface ValidationAwareInterface
{
public function isValid(): bool;
public function validate(): ConstraintViolationList;
}

0 comments on commit 5d17b09

Please sign in to comment.