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
}
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 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
}