PHP code example of aegisora / instanceof-rule-guardian
1. Go to this page and download the library: Download aegisora/instanceof-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 / instanceof-rule-guardian example snippets
$guardian->check($value, InstanceofRule::create(User::class), new InvalidUserObjectException());
$instanceofRuleGuardian->check($value, User::class, new InvalidUserObjectException());
use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\InstanceofRule\InstanceofRuleGuardian;
class User {}
$guardian = new Guardian();
$instanceofRuleGuardian = new InstanceofRuleGuardian($guardian);
try {
$instanceofRuleGuardian->check(new User(), User::class);
// value is instance of User
} catch (GuardianValidationException $exception) {
// value is not instance of User
}
use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\InstanceofRule\InstanceofRuleGuardian;
use App\Exceptions\InvalidUserObjectException;
class User {}
$guardian = new Guardian();
$instanceofRuleGuardian = new InstanceofRuleGuardian($guardian);
$instanceofRuleGuardian->check(new \stdClass(), User::class, new InvalidUserObjectException());
use Aegisora\RuleGuardians\InstanceofRule\InstanceofRuleGuardian;
use App\Exceptions\InvalidUserObjectException;
class User {}
final class UserService
{
private InstanceofRuleGuardian $instanceofRuleGuardian;
public function __construct(
InstanceofRuleGuardian $instanceofRuleGuardian
) {
$this->instanceofRuleGuardian = $instanceofRuleGuardian;
}
/**
* @param mixed $value
*/
public function process($value): void
{
$this->instanceofRuleGuardian->check($value, User::class, new InvalidUserObjectException());
// business logic for valid User object
}
}
use Aegisora\Guardian\Exceptions\GuardianException;
try {
$instanceofRuleGuardian->check($value, User::class);
} catch (GuardianException $exception) {
// handles GuardianValidationException and GuardianExecutingRuleException
}