PHP code example of aegisora / emptiness-rule-guardian

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


$guardian->check(
    $value,
    EmptyRule::create(),
    new ValueIsNotEmptyException()
);

$guardian->check(
    $value,
    NotEmptyRule::create(),
    new ValueIsEmptyException()
);

$emptinessRuleGuardian->checkEmpty($value, new ValueIsNotEmptyException());
$emptinessRuleGuardian->checkNotEmpty($value, new ValueIsEmptyException());

use Aegisora\Guardian\Guardian;
use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\RuleGuardians\EmptinessRuleGuardian\EmptinessRuleGuardian;

$guardian = new Guardian();

$emptinessRuleGuardian = new EmptinessRuleGuardian($guardian);

try {
    $emptinessRuleGuardian->checkEmpty($value);
    // $value is empty
} catch (GuardianValidationException $exception) {
    // $value is not empty
}

try {
    $emptinessRuleGuardian->checkNotEmpty($value);
    // $value is not empty
} catch (GuardianValidationException $exception) {
    // $value is empty
}

$emptinessRuleGuardian->checkEmpty(null);                // passes (null)
$emptinessRuleGuardian->checkEmpty('');                  // passes (empty string)
$emptinessRuleGuardian->checkEmpty([]);                  // passes (empty array)
$emptinessRuleGuardian->checkEmpty(new ArrayObject([])); // passes (empty countable object)

$emptinessRuleGuardian->checkEmpty('foo');               // fails (non-empty string)
$emptinessRuleGuardian->checkEmpty([1]);                 // fails (non-empty array)
$emptinessRuleGuardian->checkEmpty(0);                   // fails (int)
$emptinessRuleGuardian->checkEmpty(0.0);                 // fails (float)
$emptinessRuleGuardian->checkEmpty(false);               // fails (bool)
$emptinessRuleGuardian->checkEmpty(new stdClass());      // fails (non-countable object)
$emptinessRuleGuardian->checkEmpty(new ArrayObject([1]));// fails (non-empty countable object)
$emptinessRuleGuardian->checkEmpty(static fn () => null);// fails (callable)

use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\EmptinessRuleGuardian\EmptinessRuleGuardian;
use App\Exceptions\ValueIsEmptyException;

$guardian = new Guardian();

$emptinessRuleGuardian = new EmptinessRuleGuardian($guardian);

$emptinessRuleGuardian->checkNotEmpty(
    $value,
    new ValueIsEmptyException()
);

use Aegisora\RuleGuardians\EmptinessRuleGuardian\EmptinessRuleGuardian;
use App\Exceptions\EmptyPayloadException;

final class PayloadProcessor
{
    private EmptinessRuleGuardian $emptinessRuleGuardian;

    public function __construct(
        EmptinessRuleGuardian $emptinessRuleGuardian
    ) {
        $this->emptinessRuleGuardian = $emptinessRuleGuardian;
    }

    /**
     * @param mixed $payload
     */
    public function process($payload): void
    {
        $this->emptinessRuleGuardian->checkNotEmpty(
            $payload,
            new EmptyPayloadException()
        );

        // business logic for processing a non-empty payload
    }
}

use Aegisora\Guardian\Exceptions\GuardianValidationException;

try {
    $emptinessRuleGuardian->checkEmpty($value);
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "empty_rule"
}

try {
    $emptinessRuleGuardian->checkNotEmpty($value);
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "not_empty_rule"
}

use App\Exceptions\ValueIsEmptyException;

try {
    $emptinessRuleGuardian->checkNotEmpty($value, new ValueIsEmptyException());
} catch (ValueIsEmptyException $exception) {
    // domain-specific handling
}

use Aegisora\Guardian\Exceptions\GuardianExecutingRuleException;

try {
    $emptinessRuleGuardian->checkEmpty(tmpfile());
} catch (GuardianExecutingRuleException $exception) {
    // the rule could not be executed
}

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

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