PHP code example of kariricode / property-inspector

1. Go to this page and download the library: Download kariricode/property-inspector library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kariricode / property-inspector example snippets




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(['

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);