PHP code example of aegisora / in-array-rule-guardian
1. Go to this page and download the library: Download aegisora/in-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 / in-array-rule-guardian example snippets
$guardian->check($value, InArrayRule::createStrict($actualArray), new NotInArrayException());
$inArrayRuleGuardian->checkStrict($value, $actualArray, new NotInArrayException());
use Aegisora\Guardian\Guardian;
use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\RuleGuardians\InArrayRule\InArrayRuleGuardian;
$guardian = new Guardian();
$inArrayRuleGuardian = new InArrayRuleGuardian($guardian);
try {
$inArrayRuleGuardian->checkStrict(2, [1, 2, 3]);
// value is present in the array
} catch (GuardianValidationException $exception) {
// value is not present in the array
}
use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\InArrayRule\InArrayRuleGuardian;
use App\Exceptions\NotInArrayException;
$guardian = new Guardian();
$inArrayRuleGuardian = new InArrayRuleGuardian($guardian);
$inArrayRuleGuardian->checkStrict(4, [1, 2, 3], new NotInArrayException());
use Aegisora\RuleGuardians\InArrayRule\InArrayRuleGuardian;
use App\Exceptions\InvalidStatusException;
final class OrderService
{
private const ALLOWED_STATUSES = ['pending', 'paid', 'shipped', 'cancelled'];
private InArrayRuleGuardian $inArrayRuleGuardian;
public function __construct(
InArrayRuleGuardian $inArrayRuleGuardian
) {
$this->inArrayRuleGuardian = $inArrayRuleGuardian;
}
/**
* @param mixed $status
*/
public function updateStatus($status): void
{
$this->inArrayRuleGuardian->checkStrict($status, self::ALLOWED_STATUSES, new InvalidStatusException());
// business logic for a valid status
}
}
use App\Exceptions\NotInArrayException;
try {
$inArrayRuleGuardian->checkStrict(4, [1, 2, 3], new NotInArrayException());
} catch (NotInArrayException $exception) {
// domain-specific handling
}
use Aegisora\Guardian\Exceptions\GuardianExecutingRuleException;
try {
$inArrayRuleGuardian->checkStrict($value, $actualArray);
} catch (GuardianExecutingRuleException $exception) {
// the rule could not be executed
}