diff --git a/composer.lock b/composer.lock index 37f757f..2a2bb53 100644 --- a/composer.lock +++ b/composer.lock @@ -1160,16 +1160,16 @@ }, { "name": "league/container", - "version": "4.2.2", + "version": "4.2.3", "source": { "type": "git", "url": "https://github.com/thephpleague/container.git", - "reference": "ff346319ca1ff0e78277dc2311a42107cc1aab88" + "reference": "72f9bebe7bd623007782a40f5ec305661ab706d8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/container/zipball/ff346319ca1ff0e78277dc2311a42107cc1aab88", - "reference": "ff346319ca1ff0e78277dc2311a42107cc1aab88", + "url": "https://api.github.com/repos/thephpleague/container/zipball/72f9bebe7bd623007782a40f5ec305661ab706d8", + "reference": "72f9bebe7bd623007782a40f5ec305661ab706d8", "shasum": "" }, "require": { @@ -1230,7 +1230,7 @@ ], "support": { "issues": "https://github.com/thephpleague/container/issues", - "source": "https://github.com/thephpleague/container/tree/4.2.2" + "source": "https://github.com/thephpleague/container/tree/4.2.3" }, "funding": [ { @@ -1238,7 +1238,7 @@ "type": "github" } ], - "time": "2024-03-13T13:12:53+00:00" + "time": "2024-10-23T12:06:58+00:00" }, { "name": "mockery/mockery", @@ -2096,16 +2096,16 @@ }, { "name": "phpunit/phpunit", - "version": "10.5.36", + "version": "10.5.37", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "aa0a8ce701ea7ee314b0dfaa8970dc94f3f8c870" + "reference": "c7cffa0efa2b70c22366523e6d804c9419eb2400" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/aa0a8ce701ea7ee314b0dfaa8970dc94f3f8c870", - "reference": "aa0a8ce701ea7ee314b0dfaa8970dc94f3f8c870", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c7cffa0efa2b70c22366523e6d804c9419eb2400", + "reference": "c7cffa0efa2b70c22366523e6d804c9419eb2400", "shasum": "" }, "require": { @@ -2126,7 +2126,7 @@ "phpunit/php-timer": "^6.0.0", "sebastian/cli-parser": "^2.0.1", "sebastian/code-unit": "^2.0.0", - "sebastian/comparator": "^5.0.2", + "sebastian/comparator": "^5.0.3", "sebastian/diff": "^5.1.1", "sebastian/environment": "^6.1.0", "sebastian/exporter": "^5.1.2", @@ -2177,7 +2177,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.36" + "source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.37" }, "funding": [ { @@ -2193,7 +2193,7 @@ "type": "tidelift" } ], - "time": "2024-10-08T15:36:51+00:00" + "time": "2024-10-19T13:03:41+00:00" }, { "name": "psr/cache", diff --git a/src/Processor/ProcessingResult.php b/src/Processor/ProcessingResult.php new file mode 100644 index 0000000..8e0feaf --- /dev/null +++ b/src/Processor/ProcessingResult.php @@ -0,0 +1,75 @@ + + * @license MIT + * + * @see https://kariricode.org/ + */ +interface ProcessingResult +{ + /** + * Adds an error associated with a specific property. + * + * @param string $property The property where the error occurred + * @param string $errorKey A unique identifier for the type of error + * @param string $message A human-readable error message + */ + public function addError(string $property, string $errorKey, string $message): void; + + /** + * Stores processed data for a specific property. + * + * @param string $property The property associated with the processed data + * @param mixed $value The processed value to be stored + */ + public function setProcessedData(string $property, mixed $value): void; + + /** + * Checks if there are any errors in the processing result. + * + * @return bool True if there are errors, false otherwise + */ + public function hasErrors(): bool; + + /** + * Retrieves all errors that occurred during processing. + * + * @return array> + * A map of property names to their associated errors + */ + public function getErrors(): array; + + /** + * Retrieves all processed data. + * + * @return array A map of property names to their processed values + */ + public function getProcessedData(): array; + + /** + * Converts the processing result into an array representation. + * + * @return array{ + * isValid: bool, + * errors: array>, + * processedData: array + * } + */ + public function toArray(): array; + + /** + * Clears all stored errors and processed data. + */ + public function clear(): void; +} diff --git a/src/Processor/ValidatableProcessor.php b/src/Processor/ValidatableProcessor.php index 5375041..538c6a0 100644 --- a/src/Processor/ValidatableProcessor.php +++ b/src/Processor/ValidatableProcessor.php @@ -23,6 +23,16 @@ */ interface ValidatableProcessor extends Processor { + /** + * Resets the processor's state to its initial values. + * + * This method should be called before reusing the processor instance to ensure + * that any previous validation state is cleared and the processor is ready for + * new validation. This is particularly important when processors are reused + * across multiple validation cycles. + */ + public function reset(): void; + /** * Checks if the processor is in a valid state. * diff --git a/tests/Processor/ProcessingResultTest.php b/tests/Processor/ProcessingResultTest.php new file mode 100644 index 0000000..b3fda46 --- /dev/null +++ b/tests/Processor/ProcessingResultTest.php @@ -0,0 +1,110 @@ +processingResultMock = $this->createMock(ProcessingResult::class); + } + + public function testAddError(): void + { + $this->processingResultMock->expects($this->once()) + ->method('addError') + ->with('property', 'error_key', 'error message'); + + $this->processingResultMock->addError('property', 'error_key', 'error message'); + } + + public function testSetProcessedData(): void + { + $this->processingResultMock->expects($this->once()) + ->method('setProcessedData') + ->with('property', 'value'); + + $this->processingResultMock->setProcessedData('property', 'value'); + } + + public function testHasErrors(): void + { + $this->processingResultMock->expects($this->once()) + ->method('hasErrors') + ->willReturn(true); + + $this->assertTrue($this->processingResultMock->hasErrors()); + } + + public function testGetErrors(): void + { + $expectedErrors = [ + 'property' => [ + [ + 'errorKey' => 'error_key', + 'message' => 'error message', + ], + ], + ]; + + $this->processingResultMock->expects($this->once()) + ->method('getErrors') + ->willReturn($expectedErrors); + + $this->assertSame($expectedErrors, $this->processingResultMock->getErrors()); + } + + public function testGetProcessedData(): void + { + $expectedData = [ + 'property' => 'processed value', + ]; + + $this->processingResultMock->expects($this->once()) + ->method('getProcessedData') + ->willReturn($expectedData); + + $this->assertSame($expectedData, $this->processingResultMock->getProcessedData()); + } + + public function testToArray(): void + { + $expectedArray = [ + 'isValid' => false, + 'errors' => [ + 'property' => [ + [ + 'errorKey' => 'error_key', + 'message' => 'error message', + ], + ], + ], + 'processedData' => [ + 'property' => 'processed value', + ], + ]; + + $this->processingResultMock->expects($this->once()) + ->method('toArray') + ->willReturn($expectedArray); + + $this->assertSame($expectedArray, $this->processingResultMock->toArray()); + } + + public function testClear(): void + { + $this->processingResultMock->expects($this->once()) + ->method('clear'); + + $this->processingResultMock->clear(); + } +}