From 5d17b09a27ba379e9cc9fcb7a131855559ae3460 Mon Sep 17 00:00:00 2001 From: jaimz Date: Mon, 4 Oct 2021 16:48:00 -0400 Subject: [PATCH] Initial Commit --- .gitignore | 33 +++++++ composer.json | 26 +++++ .../ValidationViolationException.php | 17 ++++ src/ValidationAware.php | 98 +++++++++++++++++++ src/ValidationAwareInterface.php | 11 +++ 5 files changed, 185 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Exception/ValidationViolationException.php create mode 100644 src/ValidationAware.php create mode 100644 src/ValidationAwareInterface.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7396b6b --- /dev/null +++ b/.gitignore @@ -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 + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..262f36d --- /dev/null +++ b/composer.json @@ -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": "jaimz@vertigolabs.org" + } + ], + "require": { + "php": ">=8.0.0", + "symfony/validator": "5.3.8" + } + , + "require-dev": { + "roave/security-advisories": "dev-latest" + }, + "autoload": { + "psr-4": { + "VertigoLabs\\ValidationAware\\": "src" + } + } +} diff --git a/src/Exception/ValidationViolationException.php b/src/Exception/ValidationViolationException.php new file mode 100644 index 0000000..d16b1e9 --- /dev/null +++ b/src/Exception/ValidationViolationException.php @@ -0,0 +1,17 @@ +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; + } +} diff --git a/src/ValidationAwareInterface.php b/src/ValidationAwareInterface.php new file mode 100644 index 0000000..e26d047 --- /dev/null +++ b/src/ValidationAwareInterface.php @@ -0,0 +1,11 @@ +