|
4 | 4 |
|
5 | 5 |   
|
6 | 6 |
|
| 7 | +A powerful and flexible component for inspecting and processing object properties based on custom attributes in the KaririCode Framework, providing advanced features for property validation, sanitization, and analysis 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 inspection and processing of object properties based on custom attributes |
| 25 | +- Support for both validation and sanitization of property values |
| 26 | +- Flexible attribute handling through custom attribute handlers |
| 27 | +- Seamless integration with other KaririCode components (Serializer, Validator, Normalizer) |
| 28 | +- Extensible architecture allowing custom attributes and handlers |
| 29 | +- Built on top of the KaririCode\Contract interfaces for maximum flexibility |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +The PropertyInspector component can be easily installed via Composer, which is the recommended dependency manager for PHP projects. |
| 34 | + |
| 35 | +To install the PropertyInspector component in your project, run the following command in your terminal: |
| 36 | + |
| 37 | +```bash |
| 38 | +composer require kariricode/property-inspector |
| 39 | +``` |
| 40 | + |
| 41 | +This command will automatically add PropertyInspector 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-property-inspector) 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 PropertyInspector 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 custom attributes and entity: |
| 63 | + |
| 64 | +```php |
| 65 | +use Attribute; |
| 66 | + |
| 67 | +#[Attribute(Attribute::TARGET_PROPERTY)] |
| 68 | +class Validate |
| 69 | +{ |
| 70 | + public function __construct(public readonly array $rules) {} |
| 71 | +} |
| 72 | + |
| 73 | +#[Attribute(Attribute::TARGET_PROPERTY)] |
| 74 | +class Sanitize |
| 75 | +{ |
| 76 | + public function __construct(public readonly string $method) {} |
| 77 | +} |
| 78 | + |
| 79 | +class User |
| 80 | +{ |
| 81 | + public function __construct( |
| 82 | + #[Validate(['required', 'string', 'min:3'])] |
| 83 | + #[Sanitize('trim')] |
| 84 | + public string $name, |
| 85 | + #[Validate(['required', 'email'])] |
| 86 | + #[Sanitize('lowercase')] |
| 87 | + public string $email, |
| 88 | + #[Validate(['required', 'integer', 'min:18'])] |
| 89 | + public int $age |
| 90 | + ) {} |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +2. Create a custom attribute handler: |
| 95 | + |
| 96 | +```php |
| 97 | +use KaririCode\PropertyInspector\Contract\PropertyAttributeHandler; |
| 98 | + |
| 99 | +class CustomAttributeHandler implements PropertyAttributeHandler |
| 100 | +{ |
| 101 | + public function handleAttribute(object $object, string $propertyName, object $attribute, mixed $value): ?string |
| 102 | + { |
| 103 | + if ($attribute instanceof Validate) { |
| 104 | + return $this->validate($propertyName, $value, $attribute->rules); |
| 105 | + } |
| 106 | + if ($attribute instanceof Sanitize) { |
| 107 | + return $this->sanitize($value, $attribute->method); |
| 108 | + } |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + // Implement validate and sanitize methods... |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +3. Use the PropertyInspector: |
| 117 | + |
| 118 | +```php |
| 119 | +use KaririCode\PropertyInspector\AttributeAnalyzer; |
| 120 | +use KaririCode\PropertyInspector\PropertyInspector; |
| 121 | + |
| 122 | +$attributeAnalyzer = new AttributeAnalyzer(Validate::class); |
| 123 | +$propertyInspector = new PropertyInspector($attributeAnalyzer); |
| 124 | +$handler = new CustomAttributeHandler(); |
| 125 | + |
| 126 | +$user = new User('John Doe', 'john@example.com', 25); |
| 127 | + |
| 128 | +$results = $propertyInspector->inspect($user, $handler); |
| 129 | +``` |
| 130 | + |
| 131 | +### Advanced Usage |
| 132 | + |
| 133 | +You can create more complex validation and sanitization rules, and even combine the PropertyInspector with other components like the ProcessorPipeline for more advanced processing flows. |
| 134 | + |
| 135 | +## Integration with Other KaririCode Components |
| 136 | + |
| 137 | +The PropertyInspector component is designed to work seamlessly with other KaririCode components: |
| 138 | + |
| 139 | +- **KaririCode\Serializer**: Use PropertyInspector to validate and sanitize data before serialization. |
| 140 | +- **KaririCode\Validator**: Integrate custom validation logic with PropertyInspector attributes. |
| 141 | +- **KaririCode\Normalizer**: Use PropertyInspector to normalize object properties based on attributes. |
| 142 | + |
| 143 | +## Development and Testing |
| 144 | + |
| 145 | +For development and testing purposes, this package uses Docker and Docker Compose to ensure consistency across different environments. A Makefile is provided for convenience. |
| 146 | + |
| 147 | +### Prerequisites |
| 148 | + |
| 149 | +- Docker |
| 150 | +- Docker Compose |
| 151 | +- Make (optional, but recommended for easier command execution) |
| 152 | + |
| 153 | +### Development Setup |
| 154 | + |
| 155 | +1. Clone the repository: |
| 156 | + |
| 157 | + ```bash |
| 158 | + git clone https://github.com/KaririCode-Framework/kariricode-property-inspector.git |
| 159 | + cd kariricode-property-inspector |
| 160 | + ``` |
| 161 | + |
| 162 | +2. Set up the environment: |
| 163 | + |
| 164 | + ```bash |
| 165 | + make setup-env |
| 166 | + ``` |
| 167 | + |
| 168 | +3. Start the Docker containers: |
| 169 | + |
| 170 | + ```bash |
| 171 | + make up |
| 172 | + ``` |
| 173 | + |
| 174 | +4. Install dependencies: |
| 175 | + ```bash |
| 176 | + make composer-install |
| 177 | + ``` |
| 178 | + |
| 179 | +### Available Make Commands |
| 180 | + |
| 181 | +- `make up`: Start all services in the background |
| 182 | +- `make down`: Stop and remove all containers |
| 183 | +- `make build`: Build Docker images |
| 184 | +- `make shell`: Access the PHP container shell |
| 185 | +- `make test`: Run tests |
| 186 | +- `make coverage`: Run test coverage with visual formatting |
| 187 | +- `make cs-fix`: Run PHP CS Fixer to fix code style |
| 188 | +- `make quality`: Run all quality commands (cs-check, test, security-check) |
| 189 | + |
| 190 | +For a full list of available commands, run: |
| 191 | + |
| 192 | +```bash |
| 193 | +make help |
| 194 | +``` |
| 195 | + |
7 | 196 | ## License
|
8 | 197 |
|
9 | 198 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
10 | 199 |
|
11 | 200 | ## Support and Community
|
12 | 201 |
|
13 |
| -- **Documentation**: [https://kariricode.org/docs/dotenv](https://kariricode.org/docs/dotenv) |
| 202 | +- **Documentation**: [https://kariricode.org/docs/property-inspector](https://kariricode.org/docs/property-inspector) |
14 | 203 | - **Issue Tracker**: [GitHub Issues](https://github.com/KaririCode-Framework/kariricode-property-inspector/issues)
|
15 | 204 | - **Community**: [KaririCode Club Community](https://kariricode.club)
|
16 | 205 |
|
17 |
| -## Acknowledgments |
| 206 | +## Acknowledgements |
18 | 207 |
|
19 | 208 | - The KaririCode Framework team and contributors.
|
20 |
| -- Inspired by other popular PHP Dotenv libraries. |
| 209 | +- Inspired by attribute-based programming and reflection patterns in modern PHP applications. |
21 | 210 |
|
22 | 211 | ---
|
23 | 212 |
|
24 |
| -Built with ❤️ by the KaririCode team. Empowering developers to build more robust and flexible PHP applications. |
| 213 | +Built with ❤️ by the KaririCode team. Empowering developers to create more robust and flexible PHP applications. |
0 commit comments