PHP code example of temkaa / validator

1. Go to this page and download the library: Download temkaa/validator 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/ */

    

temkaa / validator example snippets




declare(strict_types=1);

namespace App;

use Temkaa\Validator\Constraint\Assert;
use Temkaa\Validator\Constraint\ViolationInterface;
use Temkaa\Validator\Constraint\ViolationListInterface;
use Temkaa\Validator\Validator;

final class Test
{
    #[Assert\Length(minLength: 2, minMessage: 'min message')]
    #[Assert\NotBlank(message: 'message')]
    public string $name;
    
    #[Assert\Count(expected: 1, message: 'message')]
    public array $preferences;
    
    #[Assert\Positive(message: 'message')]
    #[Assert\GreaterThan(threshold: 18, message: 'message', allowEquality: true)]
    public int $age;
    
    #[Assert\Initialized(message: 'message')]
    public string $middleName;
    
    #[Assert\LessThan(threshold: 95.5, message: 'message')]
    public float $weight;
    
    #[Assert\Regex('/any_pattern/', message: 'message')]
    public string $username;

    #[Assert\Cascade]
    public iterable $arrayOfObjects;
    
    public function __construct()
    {
        $testObject = new TestObject();
        $testObject->string = 'string';

        $this->arrayOfObjects = [new TestObject()];
    }
}

final class TestObject
{
    #[Assert\Length(minLength: 2, minMessage: 'min message')]
    public string $string;
}

$validator = new Validator();
/** @var ViolationListInterface<ViolationInterface> $errors */
$errors = $validator->validate(new Test());

// or to perform specific assertions

$validator = new Validator();
/** @var ViolationListInterface<ViolationInterface> $errors */
$errors = $validator->validate(new Test(), new CustomAssertion());
$errors = $validator->validate(new Test(), [new CustomAssertion1(), new CustomAssertion2()]);



declare(strict_types=1);

namespace App;

use Attribute;
use Temkaa\Validator\AbstractConstraintValidator;
use Temkaa\Validator\Constraint\ConstraintInterface;
use Temkaa\Validator\Constraint\ConstraintValidatorInterface;
use Temkaa\Validator\Constraint\ViolationInterface;
use Temkaa\Validator\Constraint\ViolationListInterface;
use Temkaa\Validator\Model\ValidatedValueInterface;
use Temkaa\Validator\Validator;

/**
 * @template-implements ConstraintInterface<ConstraintHandler>
 */
#[Attribute(Attribute::TARGET_CLASS)]
final readonly class Constraint implements ConstraintInterface
{
    public function __construct(
        public string $message,
    ) {
    }

    public function getHandler(): string
    {
        return ConstraintHandler::class;
    }
}

/**
 * @extends AbstractConstraintValidator<Constraint>
 */
final class ConstraintHandler extends AbstractConstraintValidator
{
    /**
     * @param Constraint $constraint
     */
    public function validate(ValidatedValueInterface $validatedValue, ConstraintInterface $constraint): void
    {
        if ($validatedValue->isInitialized() && $validatedValue->getValue()->age !== 18) {
            $this->addViolation(
                new Violation(
                    invalidValue: $validatedValue->getValue(),
                    message: $constraint->message,
                    path: $validatedValue->getPath()
                ),
            );
        }
    }
}

// OR

/**
 * @template-implements ConstraintValidatorInterface<Constraint>
 */
final class ConstraintHandler implements ConstraintValidatorInterface
{
    /**
     * @param ViolationListInterface<int, ViolationInterface> $violationList
     */
    public function __construct(
        private readonly ViolationListInterface $violationList = new ViolationList(),
    ) {
    }

    /**
     * @return ViolationListInterface<int, ViolationInterface>
     */
    public function getViolations(): ViolationListInterface
    {
        return $this->violationList;
    }

    /**
     * @param Constraint $constraint
     */
    public function validate(ValidatedValueInterface $validatedValue, ConstraintInterface $constraint): void
    {
        if ($validatedValue->isInitialized() && $validatedValue->getValue()->age !== 18) {
            $this->violationList->add(
                new Violation(
                  invalidValue: $validatedValue->getValue(),
                  message: $constraint->message,
                  path: $validatedValue->getPath()
                ),
            );
        }
    }
}

#[Constraint(message: 'message')]
final class Test
{
    public int $age = 17;
}

$validator = new Validator();
/** @var ViolationListInterface<int, ViolationInterface> $errors */
$errors = $validator->validate(new Test());



declare(strict_types=1);

use Temkaa\Validator\Validator;

$validator = new Validator($container);