PHP code example of hylianshield / validator

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

    

hylianshield / validator example snippets


public function getIdentifier(): string

public function validate($subject): bool

/** ValidatorInterface $validator */
$notValidator = new NotValidator($validator);

$validator->validate('something'); // true
$notValidator->validate('something'); // false

$validator->validate('somethingElse'); // false
$notValidator->validate('somethingElse'); // true

echo $validator->getIdentifier(); // something
echo $notValidator->getIdentifier(); // not(<something>)

use HylianShield\Validator\ValidatorInterface;
use Acme\User\UserManagerInterface;

$userValidator = new class($userManager) implements ValidatorInterface {
    /**
     * @var UserManagerInterface
     */
    protected $userManager;
    
    /**
     * Initialize a new user validator.
     *
     * @param UserManagerInterface $userManager
     */*
    public function __construct(UserManagerInterface $userManager)
    {
        $this->userManager = $userManager;
    }
    
    /**
     * Get the identifier for the validator.
     *
     * @return string
     */
    public function getIdentifier(): string
    {
        return 'user';
    }

    /**
     * Validate the given subject.
     *
     * @param mixed $subject
     * @return bool
     */
    public function validate($subject): bool
    {
        return $this->userManager->contains($subject);
    }
};

use HylianShield\Validator\Collection\MatchAllCollection;
use Acme\User\Role\RoleValidator;
use Acme\User\Role\AdminRole;

$adminValidator = new MatchAllCollection();
$adminValidator->addValidator($userValidator);
$adminValidator->addValidator(
    new RoleValidator(AdminRole::IDENTIFIER)
);

$adminValidator->validate($nonexistentUser); // false
$adminValidator->validate($normalUser);      // false
$adminValidator->validate($adminUser);       // true
echo $adminValidator->getIdentifier();       // all(<user>, <role:admin>)

use HylianShield\Validator\Invoker;

$filtered = array_filter($input, new Invoker($validator));