Powerful attribute-based property analysis and inspection tool for KaririCode Framework, enabling dynamic validation, normalization, and processing of object properties
MIT License
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.
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.
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';
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
) {}
}
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 KaririCode\PropertyInspector\AttributeAnalyzer;
use KaririCode\PropertyInspector\PropertyInspector;
$attributeAnalyzer = new AttributeAnalyzer(Validate::class);
$propertyInspector = new PropertyInspector($attributeAnalyzer);
$handler = new CustomAttributeHandler();
$user = new User('Walmir Silva', '[email protected]', 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:
For development and testing purposes, this package uses Docker and Docker Compose to ensure consistency across different environments. A Makefile is provided for convenience.
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.
Built with ❤️ by the KaririCode team. Empowering developers to create more robust and flexible PHP applications.