|
4 | 4 |
|
5 | 5 |   
|
6 | 6 |
|
| 7 | +A robust and flexible data sanitization component for PHP, part of the KaririCode Framework. It utilizes configurable processors and native functions to ensure data integrity and security in your 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 | +- [Available Sanitizers](#available-sanitizers) |
| 17 | +- [Integration with Other KaririCode Components](#integration-with-other-kariricode-components) |
| 18 | +- [Development and Testing](#development-and-testing) |
| 19 | +- [License](#license) |
| 20 | +- [Support and Community](#support-and-community) |
| 21 | + |
| 22 | +## Features |
| 23 | + |
| 24 | +- Flexible attribute-based sanitization for object properties |
| 25 | +- Comprehensive set of built-in sanitizers for common use cases |
| 26 | +- Easy integration with other KaririCode components |
| 27 | +- Configurable processors for customized sanitization logic |
| 28 | +- Support for fallback values in case of sanitization failures |
| 29 | +- Extensible architecture allowing custom sanitizers |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +You can install the Sanitizer component via Composer: |
| 34 | + |
| 35 | +```bash |
| 36 | +composer require kariricode/sanitizer |
| 37 | +``` |
| 38 | + |
| 39 | +### Requirements |
| 40 | + |
| 41 | +- PHP 8.3 or higher |
| 42 | +- Composer |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +### Basic Usage |
| 47 | + |
| 48 | +1. Define your data class with sanitization attributes: |
| 49 | + |
| 50 | +```php |
| 51 | +use KaririCode\Sanitizer\Attribute\Sanitize; |
| 52 | + |
| 53 | +class UserProfile |
| 54 | +{ |
| 55 | + #[Sanitize(sanitizers: ['trim', 'html_special_chars'])] |
| 56 | + private string $name = ''; |
| 57 | + |
| 58 | + #[Sanitize(sanitizers: ['trim', 'normalize_line_breaks'])] |
| 59 | + private string $email = ''; |
| 60 | + |
| 61 | + // Getters and setters... |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +2. Set up the sanitizer and use it: |
| 66 | + |
| 67 | +```php |
| 68 | +use KaririCode\ProcessorPipeline\ProcessorRegistry; |
| 69 | +use KaririCode\Sanitizer\Sanitizer; |
| 70 | +use KaririCode\Sanitizer\Processor\Input\TrimSanitizer; |
| 71 | +use KaririCode\Sanitizer\Processor\Input\HtmlSpecialCharsSanitizer; |
| 72 | +use KaririCode\Sanitizer\Processor\Input\NormalizeLineBreaksSanitizer; |
| 73 | + |
| 74 | +$registry = new ProcessorRegistry(); |
| 75 | +$registry->register('sanitizer', 'trim', new TrimSanitizer()); |
| 76 | +$registry->register('sanitizer', 'html_special_chars', new HtmlSpecialCharsSanitizer()); |
| 77 | +$registry->register('sanitizer', 'normalize_line_breaks', new NormalizeLineBreaksSanitizer()); |
| 78 | + |
| 79 | +$sanitizer = new Sanitizer($registry); |
| 80 | + |
| 81 | +$userProfile = new UserProfile(); |
| 82 | +$userProfile->setName(" Walmir Silva "); |
| 83 | +$userProfile->setEmail(" [email protected]\r\n"); |
| 84 | + |
| 85 | +$sanitizer->sanitize($userProfile); |
| 86 | + |
| 87 | +echo $userProfile->getName(); // Output: "Walmir Silva" |
| 88 | +echo $userProfile->getEmail(); // Output: " [email protected]\n" |
| 89 | +``` |
| 90 | + |
| 91 | +### Advanced Usage |
| 92 | + |
| 93 | +You can create custom sanitizers by implementing the `Processor` or `ConfigurableProcessor` interfaces: |
| 94 | + |
| 95 | +```php |
| 96 | +use KaririCode\Contract\Processor\ConfigurableProcessor; |
| 97 | +use KaririCode\Sanitizer\Processor\AbstractSanitizerProcessor; |
| 98 | + |
| 99 | +class CustomSanitizer extends AbstractSanitizerProcessor implements ConfigurableProcessor |
| 100 | +{ |
| 101 | + private $option; |
| 102 | + |
| 103 | + public function configure(array $options): void |
| 104 | + { |
| 105 | + $this->option = $options['custom_option'] ?? 'default'; |
| 106 | + } |
| 107 | + |
| 108 | + public function process(mixed $input): string |
| 109 | + { |
| 110 | + $input = $this->guardAgainstNonString($input); |
| 111 | + // Custom sanitization logic here |
| 112 | + return $input; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// Register and use the custom sanitizer |
| 117 | +$registry->register('sanitizer', 'custom', new CustomSanitizer()); |
| 118 | + |
| 119 | +class AdvancedProfile |
| 120 | +{ |
| 121 | + #[Sanitize(sanitizers: ['custom' => ['custom_option' => 'value']])] |
| 122 | + private string $customField = ''; |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +## Available Sanitizers |
| 127 | + |
| 128 | +The Sanitizer component provides various built-in sanitizers: |
| 129 | + |
| 130 | +### Input Sanitizers |
| 131 | + |
| 132 | +- TrimSanitizer |
| 133 | +- HtmlSpecialCharsSanitizer |
| 134 | +- NormalizeLineBreaksSanitizer |
| 135 | +- StripTagsSanitizer |
| 136 | + |
| 137 | +### Domain Sanitizers |
| 138 | + |
| 139 | +- HtmlPurifierSanitizer |
| 140 | +- JsonSanitizer |
| 141 | +- MarkdownSanitizer |
| 142 | + |
| 143 | +### Security Sanitizers |
| 144 | + |
| 145 | +- FilenameSanitizer |
| 146 | +- SqlInjectionSanitizer |
| 147 | +- XssSanitizer |
| 148 | + |
| 149 | +Each sanitizer is designed to handle specific types of data and security concerns. For detailed information on each sanitizer, please refer to the [documentation](https://kariricode.org/docs/sanitizer). |
| 150 | + |
| 151 | +## Integration with Other KaririCode Components |
| 152 | + |
| 153 | +The Sanitizer component is designed to work seamlessly with other KaririCode components: |
| 154 | + |
| 155 | +- **KaririCode\Contract**: Provides interfaces and contracts for consistent component integration. |
| 156 | +- **KaririCode\ProcessorPipeline**: Utilized for building and executing sanitization pipelines. |
| 157 | +- **KaririCode\PropertyInspector**: Used for analyzing and processing object properties with sanitization attributes. |
| 158 | + |
| 159 | +Example of integration: |
| 160 | + |
| 161 | +```php |
| 162 | +use KaririCode\ProcessorPipeline\ProcessorRegistry; |
| 163 | +use KaririCode\ProcessorPipeline\ProcessorBuilder; |
| 164 | +use KaririCode\PropertyInspector\AttributeAnalyzer; |
| 165 | +use KaririCode\PropertyInspector\AttributeHandler; |
| 166 | +use KaririCode\PropertyInspector\Utility\PropertyInspector; |
| 167 | +use KaririCode\Sanitizer\Sanitizer; |
| 168 | + |
| 169 | +$registry = new ProcessorRegistry(); |
| 170 | +// Register sanitizers... |
| 171 | + |
| 172 | +$builder = new ProcessorBuilder($registry); |
| 173 | +$attributeHandler = new AttributeHandler('sanitizer', $builder); |
| 174 | +$propertyInspector = new PropertyInspector(new AttributeAnalyzer(Sanitize::class)); |
| 175 | + |
| 176 | +$sanitizer = new Sanitizer($registry); |
| 177 | +``` |
| 178 | + |
| 179 | +## Development and Testing |
| 180 | + |
| 181 | +For development and testing purposes, this package uses Docker and Docker Compose to ensure consistency across different environments. A Makefile is provided for convenience. |
| 182 | + |
| 183 | +### Prerequisites |
| 184 | + |
| 185 | +- Docker |
| 186 | +- Docker Compose |
| 187 | +- Make (optional, but recommended for easier command execution) |
| 188 | + |
| 189 | +### Development Setup |
| 190 | + |
| 191 | +1. Clone the repository: |
| 192 | + |
| 193 | + ```bash |
| 194 | + git clone https://github.com/KaririCode-Framework/kariricode-sanitizer.git |
| 195 | + cd kariricode-sanitizer |
| 196 | + ``` |
| 197 | + |
| 198 | +2. Set up the environment: |
| 199 | + |
| 200 | + ```bash |
| 201 | + make setup-env |
| 202 | + ``` |
| 203 | + |
| 204 | +3. Start the Docker containers: |
| 205 | + |
| 206 | + ```bash |
| 207 | + make up |
| 208 | + ``` |
| 209 | + |
| 210 | +4. Install dependencies: |
| 211 | + ```bash |
| 212 | + make composer-install |
| 213 | + ``` |
| 214 | + |
| 215 | +### Available Make Commands |
| 216 | + |
| 217 | +- `make up`: Start all services in the background |
| 218 | +- `make down`: Stop and remove all containers |
| 219 | +- `make build`: Build Docker images |
| 220 | +- `make shell`: Access the PHP container shell |
| 221 | +- `make test`: Run tests |
| 222 | +- `make coverage`: Run test coverage with visual formatting |
| 223 | +- `make cs-fix`: Run PHP CS Fixer to fix code style |
| 224 | +- `make quality`: Run all quality commands (cs-check, test, security-check) |
| 225 | + |
| 226 | +For a full list of available commands, run: |
| 227 | + |
| 228 | +```bash |
| 229 | +make help |
| 230 | +``` |
| 231 | + |
7 | 232 | ## License
|
8 | 233 |
|
9 | 234 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
10 | 235 |
|
11 | 236 | ## Support and Community
|
12 | 237 |
|
13 |
| -- **Documentation**: [https://kariricode.org/docs/dotenv](https://kariricode.org/docs/dotenv) |
| 238 | +- **Documentation**: [https://kariricode.org/docs/sanitizer](https://kariricode.org/docs/sanitizer) |
14 | 239 | - **Issue Tracker**: [GitHub Issues](https://github.com/KaririCode-Framework/kariricode-sanitizer/issues)
|
15 | 240 | - **Community**: [KaririCode Club Community](https://kariricode.club)
|
16 | 241 |
|
17 |
| -## Acknowledgments |
18 |
| - |
19 |
| -- The KaririCode Framework team and contributors. |
20 |
| -- Inspired by other popular PHP Dotenv libraries. |
21 |
| - |
22 | 242 | ---
|
23 | 243 |
|
24 |
| -Built with ❤️ by the KaririCode team. Empowering developers to build more robust and flexible PHP applications. |
| 244 | +Built with ❤️ by the KaririCode team. Empowering developers to create more secure and robust PHP applications. |
0 commit comments