Skip to content

Commit 7103d09

Browse files
committed
feat(validator): implement ProcessorValidator and update AttributeHandler
- Add ProcessorValidator class to handle validation of processors - Update AttributeHandler to use ProcessorValidator - Refactor AttributeHandler to improve error handling and message processing - Update AttributeHandlerTest to reflect new ProcessorValidator integration - Create ProcessorValidatorTest to ensure proper functionality of ProcessorValidator - Adjust method names in AttributeHandler for better semantic meaning: * getProcessedValues -> getProcessedPropertyValues * getProcessingErrors -> getProcessingResultErrors * getProcessingMessages -> getProcessingResultMessages
1 parent a9a57ab commit 7103d09

File tree

6 files changed

+259
-80
lines changed

6 files changed

+259
-80
lines changed

composer.lock

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AttributeHandler.php

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@
1010
use KaririCode\PropertyInspector\Contract\PropertyAttributeHandler;
1111
use KaririCode\PropertyInspector\Contract\PropertyChangeApplier;
1212
use KaririCode\PropertyInspector\Processor\ProcessorConfigBuilder;
13+
use KaririCode\PropertyInspector\Processor\ProcessorValidator;
1314
use KaririCode\PropertyInspector\Utility\PropertyAccessor;
1415

1516
class AttributeHandler implements PropertyAttributeHandler, PropertyChangeApplier
1617
{
17-
private array $processedValues = [];
18-
private array $processingErrors = [];
19-
private array $processingMessages = [];
18+
private array $processedPropertyValues = [];
19+
private array $processingResultErrors = [];
20+
private array $processingResultMessages = [];
2021

2122
public function __construct(
2223
private readonly string $processorType,
2324
private readonly ProcessorBuilder $builder,
25+
private readonly ProcessorValidator $validator = new ProcessorValidator(),
2426
private readonly ProcessorConfigBuilder $configBuilder = new ProcessorConfigBuilder()
2527
) {
2628
}
@@ -36,20 +38,49 @@ public function handleAttribute(string $propertyName, object $attribute, mixed $
3638

3739
try {
3840
$processedValue = $this->processValue($value, $processorsConfig);
39-
$this->storeProcessedValue($propertyName, $processedValue, $messages);
41+
$errors = $this->validateProcessors($processorsConfig, $messages);
4042

41-
return $processedValue; // Return the processed value, not the original
43+
$this->storeProcessedPropertyValue($propertyName, $processedValue, $messages);
44+
45+
if (!empty($errors)) {
46+
$this->storeProcessingResultErrors($propertyName, $errors);
47+
}
48+
49+
return $processedValue;
4250
} catch (\Exception $e) {
43-
$this->storeProcessingError($propertyName, $e->getMessage());
51+
$this->storeProcessingResultError($propertyName, $e->getMessage());
4452

4553
return $value;
4654
}
4755
}
4856

57+
private function validateProcessors(array $processorsConfig, array $messages): array
58+
{
59+
$errors = [];
60+
foreach ($processorsConfig as $processorName => $config) {
61+
$processor = $this->builder->build($this->processorType, $processorName, $config);
62+
$validationError = $this->validator->validate(
63+
$processor,
64+
$processorName,
65+
$messages
66+
);
67+
68+
if (null !== $validationError) {
69+
$errors[$processorName] = $validationError;
70+
}
71+
}
72+
73+
return $errors;
74+
}
75+
76+
private function storeProcessingResultErrors(string $propertyName, array $errors): void
77+
{
78+
$this->processingResultErrors[$propertyName] = $errors;
79+
}
80+
4981
private function extractCustomMessages(ProcessableAttribute $attribute, array &$processorsConfig): array
5082
{
5183
$messages = [];
52-
5384
if ($attribute instanceof CustomizableMessageAttribute) {
5485
foreach ($processorsConfig as $processorName => &$config) {
5586
$customMessage = $attribute->getMessage($processorName);
@@ -65,45 +96,48 @@ private function extractCustomMessages(ProcessableAttribute $attribute, array &$
6596

6697
private function processValue(mixed $value, array $processorsConfig): mixed
6798
{
68-
$pipeline = $this->builder->buildPipeline($this->processorType, $processorsConfig);
99+
$pipeline = $this->builder->buildPipeline(
100+
$this->processorType,
101+
$processorsConfig
102+
);
69103

70104
return $pipeline->process($value);
71105
}
72106

73-
private function storeProcessedValue(string $propertyName, mixed $processedValue, array $messages): void
107+
private function storeProcessedPropertyValue(string $propertyName, mixed $processedValue, array $messages): void
74108
{
75-
$this->processedValues[$propertyName] = [
109+
$this->processedPropertyValues[$propertyName] = [
76110
'value' => $processedValue,
77111
'messages' => $messages,
78112
];
79-
$this->processingMessages[$propertyName] = $messages;
113+
$this->processingResultMessages[$propertyName] = $messages;
80114
}
81115

82-
private function storeProcessingError(string $propertyName, string $errorMessage): void
116+
private function storeProcessingResultError(string $propertyName, string $errorMessage): void
83117
{
84-
$this->processingErrors[$propertyName][] = $errorMessage;
118+
$this->processingResultErrors[$propertyName][] = $errorMessage;
85119
}
86120

87121
public function applyChanges(object $entity): void
88122
{
89-
foreach ($this->processedValues as $propertyName => $data) {
123+
foreach ($this->processedPropertyValues as $propertyName => $data) {
90124
$accessor = new PropertyAccessor($entity, $propertyName);
91125
$accessor->setValue($data['value']);
92126
}
93127
}
94128

95-
public function getProcessedValues(): array
129+
public function getProcessedPropertyValues(): array
96130
{
97-
return $this->processedValues;
131+
return $this->processedPropertyValues;
98132
}
99133

100-
public function getProcessingErrors(): array
134+
public function getProcessingResultErrors(): array
101135
{
102-
return $this->processingErrors;
136+
return $this->processingResultErrors;
103137
}
104138

105-
public function getProcessingMessages(): array
139+
public function getProcessingResultMessages(): array
106140
{
107-
return $this->processingMessages;
141+
return $this->processingResultMessages;
108142
}
109143
}

src/Contract/PropertyAttributeHandler.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,25 @@ interface PropertyAttributeHandler
1616
* @return mixed The result of handling the attribute
1717
*/
1818
public function handleAttribute(string $propertyName, object $attribute, mixed $value): mixed;
19+
20+
/**
21+
* Retrieves the values after processing.
22+
*
23+
* @return array<string, mixed> The processed values indexed by property name
24+
*/
25+
public function getProcessedPropertyValues(): array;
26+
27+
/**
28+
* Retrieves the messages generated during processing.
29+
*
30+
* @return array<string, array<string, string>> The processing messages indexed by property name and processor
31+
*/
32+
public function getProcessingResultMessages(): array;
33+
34+
/**
35+
* Retrieves the errors encountered during processing.
36+
*
37+
* @return array<string, array<string, string>> The processing errors indexed by property name and processor
38+
*/
39+
public function getProcessingResultErrors(): array;
1940
}

src/Processor/ProcessorValidator.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\PropertyInspector\Processor;
6+
7+
use KaririCode\Contract\Processor\Processor;
8+
use KaririCode\Contract\Processor\ProcessorValidator as ProcessorValidatorContract;
9+
use KaririCode\Contract\Processor\ValidatableProcessor;
10+
11+
class ProcessorValidator implements ProcessorValidatorContract
12+
{
13+
public function validate(Processor $processor, string $processorName, array $messages): ?array
14+
{
15+
if ($processor instanceof ValidatableProcessor && !$processor->isValid()) {
16+
$errorKey = $processor->getErrorKey();
17+
18+
return [
19+
'errorKey' => $errorKey,
20+
'message' => $messages[$processorName] ?? "Validation failed for $processorName",
21+
];
22+
}
23+
24+
return null;
25+
}
26+
}

0 commit comments

Comments
 (0)