diff --git a/README.md b/README.md index 3241d16..ac2ddd1 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ A simple validation. Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-validation][1]. ```sh -composer require chubbyphp/chubbyphp-validation "^3.6" +composer require chubbyphp/chubbyphp-validation "^3.7" ``` ## Usage @@ -69,19 +69,20 @@ composer require chubbyphp/chubbyphp-validation "^3.6" ### Error - * [Error][4] - * [ErrorMessages][5] - * [NestedErrorMessages][6] + * [ApiProblemErrorMessages][4] + * [Error][5] + * [ErrorMessages][6] + * [NestedErrorMessages][7] ### Mapping - * [CallableValidationMappingProvider][7] - * [LazyValidationMappingProvider][8] - * [ValidationClassMapping][9] - * [ValidationClassMappingBuilder][10] - * [ValidationMappingProviderRegistry][11] - * [ValidationPropertyMapping][12] - * [ValidationPropertyMappingBuilder][13] + * [CallableValidationMappingProvider][8] + * [LazyValidationMappingProvider][9] + * [ValidationClassMapping][10] + * [ValidationClassMappingBuilder][11] + * [ValidationMappingProviderRegistry][12] + * [ValidationPropertyMapping][13] + * [ValidationPropertyMappingBuilder][14] #### ValidationMappingProvider @@ -158,16 +159,16 @@ final class ModelValidationMappingProvider implements ValidationMappingProviderI ### ServiceFactory - * [ValidationServiceFactory][14] + * [ValidationServiceFactory][15] ### ServiceProvider - * [ValidationServiceProvider][15] + * [ValidationServiceProvider][16] ### Validator - * [ValidatorContext][16] - * [ValidatorContextBuilder][17] + * [ValidatorContext][17] + * [ValidatorContextBuilder][18] ```php 'array'] +); + +$errorMessages = new ApiProblemErrorMessages([$error]); + +$errorMessages->getMessages(); +// [ +// [ +// 'name' => 'path.to.property', +// 'reason' => 'constraint.constraint.invalidtype', +// 'details' => ['type' => 'array'], +// ], +// ] +``` diff --git a/src/Error/ApiProblemErrorMessages.php b/src/Error/ApiProblemErrorMessages.php new file mode 100644 index 0000000..93fc51b --- /dev/null +++ b/src/Error/ApiProblemErrorMessages.php @@ -0,0 +1,53 @@ + + */ + private $errors; + + /** + * @var array> + */ + private $errorMessages; + + /** + * @param array $errors + */ + public function __construct(array $errors) + { + $this->errors = []; + foreach ($errors as $error) { + $this->addError($error); + } + } + + /** + * @return array> + */ + public function getMessages(): array + { + if (null === $this->errorMessages) { + $this->errorMessages = []; + foreach ($this->errors as $error) { + $this->errorMessages[] = [ + 'name' => $error->getPath(), + 'reason' => $error->getKey(), + 'details' => $error->getArguments(), + ]; + } + } + + return $this->errorMessages; + } + + private function addError(ErrorInterface $error): void + { + $this->errors[] = $error; + } +} diff --git a/tests/Unit/Error/ApiProblemErrorMessagesTest.php b/tests/Unit/Error/ApiProblemErrorMessagesTest.php new file mode 100644 index 0000000..f6a71ab --- /dev/null +++ b/tests/Unit/Error/ApiProblemErrorMessagesTest.php @@ -0,0 +1,80 @@ +getMessages()); + } + + public function testWithMessages(): void + { + $errors = [ + $this->getError('collection[_all]', 'constraint.collection.all'), + $this->getError('collection[0].field1', 'constraint.collection0.constraint1', ['key' => 'value']), + $this->getError('collection[0].field1', 'constraint.collection0.constraint2'), + $this->getError('collection[1].field1', 'constraint.collection1.constraint1'), + $this->getError('collection[1].field1', 'constraint.collection1.constraint2'), + ]; + + $errorMessages = new ApiProblemErrorMessages($errors); + + self::assertEquals([ + [ + 'name' => 'collection[_all]', + 'reason' => 'constraint.collection.all', + 'details' => [], + ], + [ + 'name' => 'collection[0].field1', + 'reason' => 'constraint.collection0.constraint1', + 'details' => ['key' => 'value'], + ], + [ + 'name' => 'collection[0].field1', + 'reason' => 'constraint.collection0.constraint2', + 'details' => [], + ], + [ + 'name' => 'collection[1].field1', + 'reason' => 'constraint.collection1.constraint1', + 'details' => [], + ], + [ + 'name' => 'collection[1].field1', + 'reason' => 'constraint.collection1.constraint2', + 'details' => [], + ], + ], $errorMessages->getMessages()); + } + + private function getError(string $path, string $key, array $arguments = []): ErrorInterface + { + /* @var ErrorInterface|MockObject $error */ + return $this->getMockByCalls(ErrorInterface::class, [ + Call::create('getPath')->with()->willReturn($path), + Call::create('getKey')->with()->willReturn($key), + Call::create('getArguments')->with()->willReturn($arguments), + ]); + } +}