-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5d17b09
Showing
5 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |