PHP code example of aegisora / is-array-rule-guardian

1. Go to this page and download the library: Download aegisora/is-array-rule-guardian 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/ */

    

aegisora / is-array-rule-guardian example snippets


$guardian->check(
    $value,
    IsArrayRule::create(),
    new ValueIsNotArrayException()
);

$isArrayRuleGuardian->check($value, new ValueIsNotArrayException());

use Aegisora\Guardian\Guardian;
use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\RuleGuardians\IsArrayRule\IsArrayRuleGuardian;

$guardian = new Guardian();

$isArrayRuleGuardian = new IsArrayRuleGuardian($guardian);

try {
    $isArrayRuleGuardian->check($value);
    // $value is an array
} catch (GuardianValidationException $exception) {
    // $value is not an array
}

$isArrayRuleGuardian->check([]);              // passes (empty array)
$isArrayRuleGuardian->check([1]);             // passes (non-empty array)

$isArrayRuleGuardian->check(1);               // fails (int)
$isArrayRuleGuardian->check(1.1);             // fails (float)
$isArrayRuleGuardian->check('');              // fails (string)
$isArrayRuleGuardian->check(new stdClass());  // fails (object)
$isArrayRuleGuardian->check(tmpfile());       // fails (resource)
$isArrayRuleGuardian->check(static fn () => null); // fails (callable)

use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\IsArrayRule\IsArrayRuleGuardian;
use App\Exceptions\ValueIsNotArrayException;

$guardian = new Guardian();

$isArrayRuleGuardian = new IsArrayRuleGuardian($guardian);

$isArrayRuleGuardian->check(
    $value,
    new ValueIsNotArrayException()
);

use Aegisora\RuleGuardians\IsArrayRule\IsArrayRuleGuardian;
use App\Exceptions\InvalidPayloadException;

final class PayloadProcessor
{
    private IsArrayRuleGuardian $isArrayRuleGuardian;

    public function __construct(
        IsArrayRuleGuardian $isArrayRuleGuardian
    ) {
        $this->isArrayRuleGuardian = $isArrayRuleGuardian;
    }

    /**
     * @param mixed $payload
     */
    public function process($payload): void
    {
        $this->isArrayRuleGuardian->check(
            $payload,
            new InvalidPayloadException()
        );

        // business logic for processing an array payload
    }
}

use Aegisora\Guardian\Exceptions\GuardianValidationException;

try {
    $isArrayRuleGuardian->check($value);
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "is_array_rule"
}

use App\Exceptions\ValueIsNotArrayException;

try {
    $isArrayRuleGuardian->check($value, new ValueIsNotArrayException());
} catch (ValueIsNotArrayException $exception) {
    // domain-specific handling
}

use Aegisora\Guardian\Exceptions\GuardianExecutingRuleException;

try {
    $isArrayRuleGuardian->check($value);
} catch (GuardianExecutingRuleException $exception) {
    // the rule could not be executed
}

/**
 * @param mixed $value
 * @throws GuardianExecutingRuleException
 * @throws GuardianValidationException
 * @throws \Throwable
 */
public function check($value, ?\Throwable $exception = null): void