diff --git a/src/Processor/Attribute/BaseProcessorAttribute.php b/src/Processor/Attribute/BaseProcessorAttribute.php new file mode 100644 index 0000000..d98e682 --- /dev/null +++ b/src/Processor/Attribute/BaseProcessorAttribute.php @@ -0,0 +1,43 @@ +processors = $this->normalizeProcessors($processors); + $this->messages = $messages ?? []; + } + + public function getProcessors(): array + { + return $this->processors; + } + + public function getMessage(string $processorName): ?string + { + return $this->messages[$processorName] ?? null; + } + + protected function normalizeProcessors(array $processors): array + { + $normalized = []; + foreach ($processors as $key => $value) { + if (is_int($key)) { + $normalized[$value] = []; + } elseif (is_string($value)) { + $normalized[$key] = []; + } else { + $normalized[$key] = $value; + } + } + + return $normalized; + } +} diff --git a/src/Processor/Attribute/CustomizableMessageAttribute.php b/src/Processor/Attribute/CustomizableMessageAttribute.php new file mode 100644 index 0000000..1e24c74 --- /dev/null +++ b/src/Processor/Attribute/CustomizableMessageAttribute.php @@ -0,0 +1,29 @@ + + * @license MIT + * + * @see https://kariricode.org/ + */ +interface CustomizableMessageAttribute +{ + /** + * Retrieves a custom message for a specific validator. + * + * @param string $validatorName The name of the validator + * + * @return string|null The custom message if set, null otherwise + */ + public function getMessage(string $validatorName): ?string; +} diff --git a/src/Processor/ProcessableAttribute.php b/src/Processor/Attribute/ProcessableAttribute.php similarity index 69% rename from src/Processor/ProcessableAttribute.php rename to src/Processor/Attribute/ProcessableAttribute.php index 95d7b61..b0c8a51 100644 --- a/src/Processor/ProcessableAttribute.php +++ b/src/Processor/Attribute/ProcessableAttribute.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace KaririCode\Contract\Processor; +namespace KaririCode\Contract\Processor\Attribute; /** * Interface ProcessableAttribute. @@ -24,11 +24,4 @@ interface ProcessableAttribute * @return array The list of processors */ public function getProcessors(): array; - - /** - * Provides a fallback value for the attribute if processing fails or is not available. - * - * @return mixed The fallback value - */ - public function getFallbackValue(): mixed; } diff --git a/src/Validator/Validator.php b/src/Validator/Validator.php index ff8e0f2..8d94c33 100644 --- a/src/Validator/Validator.php +++ b/src/Validator/Validator.php @@ -19,11 +19,11 @@ interface Validator { /** - * Validates the provided input and returns a boolean indicating if the input is valid. + * Validates the provided input and returns the validated result. * * @param mixed $input The value to be validated * - * @return bool True if the input is valid, false otherwise + * @return mixed The validated result */ - public function validate(mixed $input): bool; + public function validate(mixed $input): mixed; } diff --git a/tests/Processor/Attribute/CustomizableMessageAttributeTest.php b/tests/Processor/Attribute/CustomizableMessageAttributeTest.php new file mode 100644 index 0000000..69acad3 --- /dev/null +++ b/tests/Processor/Attribute/CustomizableMessageAttributeTest.php @@ -0,0 +1,43 @@ +createMock(CustomizableMessageAttribute::class); + + $validatorName = 'requiredValidator'; + $expectedMessage = 'This field is required.'; + + $customizableMessageAttribute->expects($this->once()) + ->method('getMessage') + ->with($validatorName) + ->willReturn($expectedMessage); + + $this->assertSame($expectedMessage, $customizableMessageAttribute->getMessage($validatorName)); + } + + public function testGetMessageReturnsNull(): void + { + /** @var CustomizableMessageAttribute|MockObject */ + $customizableMessageAttribute = $this->createMock(CustomizableMessageAttribute::class); + + $validatorName = 'nonExistentValidator'; + + $customizableMessageAttribute->expects($this->once()) + ->method('getMessage') + ->with($validatorName) + ->willReturn(null); + + $this->assertNull($customizableMessageAttribute->getMessage($validatorName)); + } +} diff --git a/tests/Processor/ProcessableAttributeTest.php b/tests/Processor/Attribute/ProcessableAttributeTest.php similarity index 52% rename from tests/Processor/ProcessableAttributeTest.php rename to tests/Processor/Attribute/ProcessableAttributeTest.php index 9551ae8..a919dda 100644 --- a/tests/Processor/ProcessableAttributeTest.php +++ b/tests/Processor/Attribute/ProcessableAttributeTest.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace KaririCode\Contract\Tests\Processor; +namespace KaririCode\Contract\Tests\Processor\Attribute; -use KaririCode\Contract\Processor\ProcessableAttribute; +use KaririCode\Contract\Processor\Attribute\ProcessableAttribute; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -22,17 +22,4 @@ public function testGetProcessors(): void $this->assertSame($processors, $processableAttribute->getProcessors()); } - - public function testGetFallbackValue(): void - { - /** @var ProcessableAttribute|MockObject */ - $processableAttribute = $this->createMock(ProcessableAttribute::class); - $fallbackValue = 'default value'; - - $processableAttribute->expects($this->once()) - ->method('getFallbackValue') - ->willReturn($fallbackValue); - - $this->assertSame($fallbackValue, $processableAttribute->getFallbackValue()); - } }