|
1 |
| -# KaririCode Framework: ProcessorPipeline Component |
| 1 | +# KaririCode Framework: Processor Pipeline Component |
2 | 2 |
|
3 | 3 | [](README.md) [](README.pt-br.md)
|
4 | 4 |
|
@@ -62,49 +62,142 @@ require_once 'vendor/autoload.php';
|
62 | 62 | 1. Define your processors:
|
63 | 63 |
|
64 | 64 | ```php
|
| 65 | +<?php |
| 66 | + |
| 67 | +declare(strict_types=1); |
| 68 | + |
| 69 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 70 | + |
65 | 71 | use KaririCode\Contract\Processor\Processor;
|
| 72 | +use KaririCode\ProcessorPipeline\ProcessorBuilder; |
| 73 | +use KaririCode\ProcessorPipeline\ProcessorRegistry; |
| 74 | +use KaririCode\ProcessorPipeline\Result\ProcessingResultCollection; |
66 | 75 |
|
67 |
| -class EmailNormalizer implements Processor |
| 76 | +// Example of actual processors. |
| 77 | +class UpperCaseProcessor implements Processor |
68 | 78 | {
|
69 |
| - public function process(mixed $input): string |
| 79 | + public function process(mixed $input): mixed |
70 | 80 | {
|
71 |
| - return strtolower(trim($input)); |
| 81 | + return strtoupper((string) $input); |
72 | 82 | }
|
73 | 83 | }
|
74 | 84 |
|
75 |
| -class EmailValidator implements Processor |
| 85 | +class TrimProcessor implements Processor |
76 | 86 | {
|
77 |
| - public function process(mixed $input): bool |
| 87 | + public function process(mixed $input): mixed |
78 | 88 | {
|
79 |
| - return false !== filter_var($input, FILTER_VALIDATE_EMAIL); |
| 89 | + return trim((string) $input); |
80 | 90 | }
|
81 | 91 | }
|
82 |
| -``` |
83 | 92 |
|
84 |
| -2. Set up the processor registry and builder: |
| 93 | +class EmailTransformerProcessor implements Processor |
| 94 | +{ |
| 95 | + public function process(mixed $input): mixed |
| 96 | + { |
| 97 | + return strtolower((string) $input); |
| 98 | + } |
| 99 | +} |
85 | 100 |
|
86 |
| -```php |
87 |
| -use KaririCode\ProcessorPipeline\ProcessorRegistry; |
88 |
| -use KaririCode\ProcessorPipeline\ProcessorBuilder; |
| 101 | +class EmailValidatorProcessor implements Processor |
| 102 | +{ |
| 103 | + public function __construct(private ProcessingResultCollection $resultCollection) |
| 104 | + { |
| 105 | + } |
89 | 106 |
|
90 |
| -$registry = new ProcessorRegistry(); |
91 |
| -$registry->register('user', 'emailNormalizer', new EmailNormalizer()); |
92 |
| -$registry->register('user', 'emailValidator', new EmailValidator()); |
| 107 | + public function process(mixed $input): mixed |
| 108 | + { |
| 109 | + if (!filter_var($input, FILTER_VALIDATE_EMAIL)) { |
| 110 | + $this->resultCollection->addError( |
| 111 | + self::class, |
| 112 | + 'invalidFormat', |
| 113 | + "Invalid email format: $input" |
| 114 | + ); |
| 115 | + } |
| 116 | + return $input; |
| 117 | + } |
| 118 | +} |
93 | 119 |
|
| 120 | +// Function to handle pipeline execution |
| 121 | +function executePipeline(ProcessorBuilder $builder, ProcessorRegistry $registry, array $processorSpecs, string $input): void |
| 122 | +{ |
| 123 | + $resultCollection = new ProcessingResultCollection(); |
| 124 | + $context = 'example_context'; |
| 125 | + |
| 126 | + $registry->register($context, 'upper_case', new UpperCaseProcessor()) |
| 127 | + ->register($context, 'trim', new TrimProcessor()) |
| 128 | + ->register($context, 'email_transform', new EmailTransformerProcessor()) |
| 129 | + ->register($context, 'email_validate', new EmailValidatorProcessor($resultCollection)); |
| 130 | + |
| 131 | + try { |
| 132 | + $pipeline = $builder->buildPipeline($context, $processorSpecs); |
| 133 | + $output = $pipeline->process($input); |
| 134 | + |
| 135 | + // Displaying the results |
| 136 | + echo "Original Input: '$input'\n"; |
| 137 | + echo "Pipeline Output: '$output'\n"; |
| 138 | + |
| 139 | + // Display errors if any |
| 140 | + if ($resultCollection->hasErrors()) { |
| 141 | + echo "\nProcessing Errors:\n"; |
| 142 | + print_r($resultCollection->getErrors()); |
| 143 | + } else { |
| 144 | + echo "\nNo processing errors encountered.\n"; |
| 145 | + } |
| 146 | + } catch (\Exception $e) { |
| 147 | + echo "Error executing the pipeline: " . $e->getMessage() . "\n"; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// Register processors to a context in the registry. |
| 152 | +$registry = new ProcessorRegistry(); |
94 | 153 | $builder = new ProcessorBuilder($registry);
|
95 |
| -``` |
96 | 154 |
|
97 |
| -3. Build and use a pipeline: |
| 155 | +// Execute scenario 1 - Valid input |
| 156 | +$processorSpecs = [ |
| 157 | + 'upper_case' => false, |
| 158 | + 'trim' => true, |
| 159 | + 'email_transform' => true, |
| 160 | + 'email_validate' => true, |
| 161 | +]; |
| 162 | + |
98 | 163 |
|
99 |
| -```php |
100 |
| -$pipeline = $builder->buildPipeline('user', ['emailNormalizer', 'emailValidator']); |
| 164 | +echo "Scenario 1 - Valid Input\n"; |
| 165 | +executePipeline($builder, $registry, $processorSpecs, $input); |
| 166 | + |
| 167 | +// Execute scenario 2 - Invalid input |
| 168 | +$input = " InvalidEmail@@@ "; |
101 | 169 |
|
102 |
| - |
103 |
| -$normalizedEmail = $pipeline->process($email); |
104 |
| -$isValid = $pipeline->process($normalizedEmail); |
| 170 | +echo "\nScenario 2 - Invalid Input:\n"; |
| 171 | +executePipeline($builder, $registry, $processorSpecs, $input); |
| 172 | +``` |
| 173 | + |
| 174 | +### Test Output |
105 | 175 |
|
106 |
| -echo "Normalized: $normalizedEmail\n"; |
107 |
| -echo "Valid: " . ($isValid ? 'Yes' : 'No') . "\n"; |
| 176 | +```bash |
| 177 | +php ./tests/application.php |
| 178 | +Scenario 1 - Valid Input |
| 179 | +Original Input: ' [email protected] ' |
| 180 | +Pipeline Output: '[email protected]' |
| 181 | + |
| 182 | +No processing errors encountered. |
| 183 | + |
| 184 | +Scenario 2 - Invalid Input: |
| 185 | +Original Input: ' InvalidEmail@@@ ' |
| 186 | +Pipeline Output: 'invalidemail@@@' |
| 187 | + |
| 188 | +Processing Errors: |
| 189 | +Array |
| 190 | +( |
| 191 | + [EmailValidatorProcessor] => Array |
| 192 | + ( |
| 193 | + [0] => Array |
| 194 | + ( |
| 195 | + [errorKey] => invalidFormat |
| 196 | + [message] => Invalid email format: invalidemail@@@ |
| 197 | + ) |
| 198 | + |
| 199 | + ) |
| 200 | +) |
108 | 201 | ```
|
109 | 202 |
|
110 | 203 | ### Advanced Usage
|
@@ -151,16 +244,67 @@ The ProcessorPipeline component is designed to work seamlessly with other Kariri
|
151 | 244 |
|
152 | 245 | Example using ProcessorPipeline with Validator:
|
153 | 246 |
|
| 247 | +1. Define your data class with validation attributes: |
| 248 | + |
| 249 | +```php |
| 250 | +use KaririCode\Validator\Attribute\Validate; |
| 251 | + |
| 252 | +class UserProfile |
| 253 | +{ |
| 254 | + #[Validate( |
| 255 | + processors: [ |
| 256 | + 'required', |
| 257 | + 'length' => ['minLength' => 3, 'maxLength' => 20], |
| 258 | + ], |
| 259 | + messages: [ |
| 260 | + 'required' => 'Username is required', |
| 261 | + 'length' => 'Username must be between 3 and 20 characters', |
| 262 | + ] |
| 263 | + )] |
| 264 | + private string $username = ''; |
| 265 | + |
| 266 | + #[Validate( |
| 267 | + processors: ['required', 'email'], |
| 268 | + messages: [ |
| 269 | + 'required' => 'Email is required', |
| 270 | + 'email' => 'Invalid email format', |
| 271 | + ] |
| 272 | + )] |
| 273 | + private string $email = ''; |
| 274 | + |
| 275 | + // Getters and setters... |
| 276 | +} |
| 277 | +``` |
| 278 | + |
| 279 | +2. Set up the validator and use it: |
| 280 | + |
154 | 281 | ```php
|
155 |
| -use KaririCode\Validator\Validators\EmailValidator; |
156 |
| -use KaririCode\Validator\Validators\NotEmptyValidator; |
| 282 | +use KaririCode\ProcessorPipeline\ProcessorRegistry; |
| 283 | +use KaririCode\Validator\Validator; |
| 284 | +use KaririCode\Validator\Processor\Logic\RequiredValidator; |
| 285 | +use KaririCode\Validator\Processor\Input\LengthValidator; |
| 286 | +use KaririCode\Validator\Processor\Input\EmailValidator; |
157 | 287 |
|
158 |
| -$registry->register('validation', 'email', new EmailValidator()); |
159 |
| -$registry->register('validation', 'notEmpty', new NotEmptyValidator()); |
| 288 | +$registry = new ProcessorRegistry(); |
| 289 | +$registry->register('validator', 'required', new RequiredValidator()) |
| 290 | + ->register('validator', 'length', new LengthValidator()) |
| 291 | + ->register('validator', 'email', new EmailValidator()); |
| 292 | + |
| 293 | +$validator = new Validator($registry); |
| 294 | + |
| 295 | +$userProfile = new UserProfile(); |
| 296 | +$userProfile->setUsername('wa'); // Too short |
| 297 | +$userProfile->setEmail('invalid-email'); // Invalid format |
160 | 298 |
|
161 |
| -$validationPipeline = $builder->buildPipeline('validation', ['notEmpty', 'email']); |
| 299 | +$result = $validator->validate($userProfile); |
162 | 300 |
|
163 |
| -$isValid = $validationPipeline->process($userInput); |
| 301 | +if ($result->hasErrors()) { |
| 302 | + foreach ($result->getErrors() as $property => $errors) { |
| 303 | + foreach ($errors as $error) { |
| 304 | + echo "$property: {$error['message']}\n"; |
| 305 | + } |
| 306 | + } |
| 307 | +} |
164 | 308 | ```
|
165 | 309 |
|
166 | 310 | ## Development and Testing
|
|
0 commit comments