|
1 |
| -# KaririCode Framework: Sanitizer Component |
| 1 | +# KaririCode Framework: ProcessorPipeline Component |
2 | 2 |
|
3 | 3 | [](README.md) [](README.pt-br.md)
|
4 | 4 |
|
5 | 5 |   
|
6 | 6 |
|
| 7 | +A robust and flexible component for creating and managing processing pipelines in the KaririCode Framework, providing advanced features for handling complex data processing tasks in PHP applications. |
| 8 | + |
| 9 | +## Table of Contents |
| 10 | + |
| 11 | +- [Features](#features) |
| 12 | +- [Installation](#installation) |
| 13 | +- [Usage](#usage) |
| 14 | + - [Basic Usage](#basic-usage) |
| 15 | + - [Advanced Usage](#advanced-usage) |
| 16 | +- [Integration with Other KaririCode Components](#integration-with-other-kariricode-components) |
| 17 | +- [Development and Testing](#development-and-testing) |
| 18 | +- [License](#license) |
| 19 | +- [Support and Community](#support-and-community) |
| 20 | +- [Acknowledgements](#acknowledgements) |
| 21 | + |
| 22 | +## Features |
| 23 | + |
| 24 | +- Easy creation and management of processing pipelines |
| 25 | +- Support for both simple and configurable processors |
| 26 | +- Context-based processor registry for organized processor management |
| 27 | +- Seamless integration with other KaririCode components (Serializer, Validator, Normalizer) |
| 28 | +- Extensible architecture allowing custom processors |
| 29 | +- Built on top of the KaririCode\Contract interfaces for maximum flexibility |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +The ProcessorPipeline component can be easily installed via Composer, which is the recommended dependency manager for PHP projects. |
| 34 | + |
| 35 | +To install the ProcessorPipeline component in your project, run the following command in your terminal: |
| 36 | + |
| 37 | +```bash |
| 38 | +composer require kariricode/processor-pipeline |
| 39 | +``` |
| 40 | + |
| 41 | +This command will automatically add ProcessorPipeline to your project and install all necessary dependencies. |
| 42 | + |
| 43 | +### Requirements |
| 44 | + |
| 45 | +- PHP 8.1 or higher |
| 46 | +- Composer |
| 47 | + |
| 48 | +### Manual Installation |
| 49 | + |
| 50 | +If you prefer not to use Composer, you can download the source code directly from the [GitHub repository](https://github.com/KaririCode-Framework/kariricode-processor-pipeline) and include it manually in your project. However, we strongly recommend using Composer for easier dependency management and updates. |
| 51 | + |
| 52 | +After installation, you can start using ProcessorPipeline in your PHP project immediately. Make sure to include the Composer autoloader in your script: |
| 53 | + |
| 54 | +```php |
| 55 | +require_once 'vendor/autoload.php'; |
| 56 | +``` |
| 57 | + |
| 58 | +## Usage |
| 59 | + |
| 60 | +### Basic Usage |
| 61 | + |
| 62 | +1. Define your processors: |
| 63 | + |
| 64 | +```php |
| 65 | +use KaririCode\Contract\Processor\Processor; |
| 66 | + |
| 67 | +class EmailNormalizer implements Processor |
| 68 | +{ |
| 69 | + public function process(mixed $input): string |
| 70 | + { |
| 71 | + return strtolower(trim($input)); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +class EmailValidator implements Processor |
| 76 | +{ |
| 77 | + public function process(mixed $input): bool |
| 78 | + { |
| 79 | + return false !== filter_var($input, FILTER_VALIDATE_EMAIL); |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +2. Set up the processor registry and builder: |
| 85 | + |
| 86 | +```php |
| 87 | +use KaririCode\ProcessorPipeline\ProcessorRegistry; |
| 88 | +use KaririCode\ProcessorPipeline\ProcessorBuilder; |
| 89 | + |
| 90 | +$registry = new ProcessorRegistry(); |
| 91 | +$registry->register('user', 'emailNormalizer', new EmailNormalizer()); |
| 92 | +$registry->register('user', 'emailValidator', new EmailValidator()); |
| 93 | + |
| 94 | +$builder = new ProcessorBuilder($registry); |
| 95 | +``` |
| 96 | + |
| 97 | +3. Build and use a pipeline: |
| 98 | + |
| 99 | +```php |
| 100 | +$pipeline = $builder->buildPipeline('user', ['emailNormalizer', 'emailValidator']); |
| 101 | + |
| 102 | + |
| 103 | +$normalizedEmail = $pipeline->process($email); |
| 104 | +$isValid = $pipeline->process($normalizedEmail); |
| 105 | + |
| 106 | +echo "Normalized: $normalizedEmail\n"; |
| 107 | +echo "Valid: " . ($isValid ? 'Yes' : 'No') . "\n"; |
| 108 | +``` |
| 109 | + |
| 110 | +### Advanced Usage |
| 111 | + |
| 112 | +#### Configurable Processors |
| 113 | + |
| 114 | +Create configurable processors for more flexibility: |
| 115 | + |
| 116 | +```php |
| 117 | +use KaririCode\Contract\Processor\ConfigurableProcessor; |
| 118 | + |
| 119 | +class AgeValidator implements ConfigurableProcessor |
| 120 | +{ |
| 121 | + private int $minAge = 0; |
| 122 | + private int $maxAge = 120; |
| 123 | + |
| 124 | + public function configure(array $options): void |
| 125 | + { |
| 126 | + if (isset($options['minAge'])) { |
| 127 | + $this->minAge = $options['minAge']; |
| 128 | + } |
| 129 | + if (isset($options['maxAge'])) { |
| 130 | + $this->maxAge = $options['maxAge']; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public function process(mixed $input): bool |
| 135 | + { |
| 136 | + return is_numeric($input) && $input >= $this->minAge && $input <= $this->maxAge; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +$registry->register('user', 'ageValidator', new AgeValidator()); |
| 141 | +$pipeline = $builder->buildPipeline('user', ['ageValidator' => ['minAge' => 18, 'maxAge' => 100]]); |
| 142 | +``` |
| 143 | + |
| 144 | +## Integration with Other KaririCode Components |
| 145 | + |
| 146 | +The ProcessorPipeline component is designed to work seamlessly with other KaririCode components: |
| 147 | + |
| 148 | +- **KaririCode\Serializer**: Use processors to transform data before or after serialization. |
| 149 | +- **KaririCode\Validator**: Create validation pipelines for complex data structures. |
| 150 | +- **KaririCode\Normalizer**: Build normalization pipelines for data cleaning and standardization. |
| 151 | + |
| 152 | +Example using ProcessorPipeline with Validator: |
| 153 | + |
| 154 | +```php |
| 155 | +use KaririCode\Validator\Validators\EmailValidator; |
| 156 | +use KaririCode\Validator\Validators\NotEmptyValidator; |
| 157 | + |
| 158 | +$registry->register('validation', 'email', new EmailValidator()); |
| 159 | +$registry->register('validation', 'notEmpty', new NotEmptyValidator()); |
| 160 | + |
| 161 | +$validationPipeline = $builder->buildPipeline('validation', ['notEmpty', 'email']); |
| 162 | + |
| 163 | +$isValid = $validationPipeline->process($userInput); |
| 164 | +``` |
| 165 | + |
| 166 | +## Development and Testing |
| 167 | + |
| 168 | +For development and testing purposes, this package uses Docker and Docker Compose to ensure consistency across different environments. A Makefile is provided for convenience. |
| 169 | + |
| 170 | +### Prerequisites |
| 171 | + |
| 172 | +- Docker |
| 173 | +- Docker Compose |
| 174 | +- Make (optional, but recommended for easier command execution) |
| 175 | + |
| 176 | +### Development Setup |
| 177 | + |
| 178 | +1. Clone the repository: |
| 179 | + |
| 180 | + ```bash |
| 181 | + git clone https://github.com/KaririCode-Framework/kariricode-processor-pipeline.git |
| 182 | + cd kariricode-processor-pipeline |
| 183 | + ``` |
| 184 | + |
| 185 | +2. Set up the environment: |
| 186 | + |
| 187 | + ```bash |
| 188 | + make setup-env |
| 189 | + ``` |
| 190 | + |
| 191 | +3. Start the Docker containers: |
| 192 | + |
| 193 | + ```bash |
| 194 | + make up |
| 195 | + ``` |
| 196 | + |
| 197 | +4. Install dependencies: |
| 198 | + ```bash |
| 199 | + make composer-install |
| 200 | + ``` |
| 201 | + |
| 202 | +### Available Make Commands |
| 203 | + |
| 204 | +- `make up`: Start all services in the background |
| 205 | +- `make down`: Stop and remove all containers |
| 206 | +- `make build`: Build Docker images |
| 207 | +- `make shell`: Access the PHP container shell |
| 208 | +- `make test`: Run tests |
| 209 | +- `make coverage`: Run test coverage with visual formatting |
| 210 | +- `make cs-fix`: Run PHP CS Fixer to fix code style |
| 211 | +- `make quality`: Run all quality commands (cs-check, test, security-check) |
| 212 | + |
| 213 | +For a full list of available commands, run: |
| 214 | + |
| 215 | +```bash |
| 216 | +make help |
| 217 | +``` |
| 218 | + |
7 | 219 | ## License
|
8 | 220 |
|
9 | 221 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
10 | 222 |
|
11 | 223 | ## Support and Community
|
12 | 224 |
|
13 |
| -- **Documentation**: [https://kariricode.org/docs/dotenv](https://kariricode.org/docs/dotenv) |
| 225 | +- **Documentation**: [https://kariricode.org/docs/processor-pipeline](https://kariricode.org/docs/processor-pipeline) |
14 | 226 | - **Issue Tracker**: [GitHub Issues](https://github.com/KaririCode-Framework/kariricode-processor-pipeline/issues)
|
15 | 227 | - **Community**: [KaririCode Club Community](https://kariricode.club)
|
16 | 228 |
|
17 |
| -## Acknowledgments |
| 229 | +## Acknowledgements |
18 | 230 |
|
19 | 231 | - The KaririCode Framework team and contributors.
|
20 |
| -- Inspired by other popular PHP Dotenv libraries. |
| 232 | +- Inspired by pipeline patterns and processing chains in software architecture. |
21 | 233 |
|
22 | 234 | ---
|
23 | 235 |
|
24 |
| -Built with ❤️ by the KaririCode team. Empowering developers to build more robust and flexible PHP applications. |
| 236 | +Built with ❤️ by the KaririCode team. Empowering developers to create more robust and flexible PHP applications. |
0 commit comments