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.
- Features
- Installation
- Usage
- Integration with Other KaririCode Components
- Development and Testing
- License
- Support and Community
- Acknowledgements
- Easy inspection and processing of object properties based on custom attributes
- Support for both validation and sanitization of property values
- Flexible attribute handling through custom attribute handlers
- Seamless integration with other KaririCode components (Serializer, Validator, Normalizer)
- Extensible architecture allowing custom attributes and handlers
- Built on top of the KaririCode\Contract interfaces for maximum flexibility
The PropertyInspector component can be easily installed via Composer, which is the recommended dependency manager for PHP projects.
To install the PropertyInspector component in your project, run the following command in your terminal:
composer require kariricode/property-inspector
This command will automatically add PropertyInspector to your project and install all necessary dependencies.
- PHP 8.1 or higher
- Composer
If you prefer not to use Composer, you can download the source code directly from the GitHub repository and include it manually in your project. However, we strongly recommend using Composer for easier dependency management and updates.
After installation, you can start using PropertyInspector in your PHP project immediately. Make sure to include the Composer autoloader in your script:
require_once 'vendor/autoload.php';
- Define your custom attributes and entity:
use Attribute;
#[Attribute(Attribute::TARGET_PROPERTY)]
class Validate
{
public function __construct(public readonly array $rules) {}
}
#[Attribute(Attribute::TARGET_PROPERTY)]
class Sanitize
{
public function __construct(public readonly string $method) {}
}
class User
{
public function __construct(
#[Validate(['required', 'string', 'min:3'])]
#[Sanitize('trim')]
public string $name,
#[Validate(['required', 'email'])]
#[Sanitize('lowercase')]
public string $email,
#[Validate(['required', 'integer', 'min:18'])]
public int $age
) {}
}
- Create a custom attribute handler:
use KaririCode\PropertyInspector\Contract\PropertyAttributeHandler;
class CustomAttributeHandler implements PropertyAttributeHandler
{
public function handleAttribute(object $object, string $propertyName, object $attribute, mixed $value): ?string
{
if ($attribute instanceof Validate) {
return $this->validate($propertyName, $value, $attribute->rules);
}
if ($attribute instanceof Sanitize) {
return $this->sanitize($value, $attribute->method);
}
return null;
}
// Implement validate and sanitize methods...
}
- Use the PropertyInspector:
use KaririCode\PropertyInspector\AttributeAnalyzer;
use KaririCode\PropertyInspector\PropertyInspector;
$attributeAnalyzer = new AttributeAnalyzer(Validate::class);
$propertyInspector = new PropertyInspector($attributeAnalyzer);
$handler = new CustomAttributeHandler();
$user = new User('Walmir Silva', 'walmir@example.com', 25);
$results = $propertyInspector->inspect($user, $handler);
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.
The PropertyInspector component is designed to work seamlessly with other KaririCode components:
- KaririCode\Serializer: Use PropertyInspector to validate and sanitize data before serialization.
- KaririCode\Validator: Integrate custom validation logic with PropertyInspector attributes.
- KaririCode\Normalizer: Use PropertyInspector to normalize object properties based on attributes.
For development and testing purposes, this package uses Docker and Docker Compose to ensure consistency across different environments. A Makefile is provided for convenience.
- Docker
- Docker Compose
- Make (optional, but recommended for easier command execution)
-
Clone the repository:
git clone https://github.com/KaririCode-Framework/kariricode-property-inspector.git cd kariricode-property-inspector
-
Set up the environment:
make setup-env
-
Start the Docker containers:
make up
-
Install dependencies:
make composer-install
make up
: Start all services in the backgroundmake down
: Stop and remove all containersmake build
: Build Docker imagesmake shell
: Access the PHP container shellmake test
: Run testsmake coverage
: Run test coverage with visual formattingmake cs-fix
: Run PHP CS Fixer to fix code stylemake quality
: Run all quality commands (cs-check, test, security-check)
For a full list of available commands, run:
make help
This project is licensed under the MIT License - see the LICENSE file for details.
- Documentation: https://kariricode.org/docs/property-inspector
- Issue Tracker: GitHub Issues
- Community: KaririCode Club Community
- The KaririCode Framework team and contributors.
- Inspired by attribute-based programming and reflection patterns in modern PHP applications.
Built with ❤️ by the KaririCode team. Empowering developers to create more robust and flexible PHP applications.