Guard container object for dealing with optional/null values.
use Xtompie\Guard\Guard;
$user = Guard::of(request()->input('id'))
->filter(fn($id) => ctype_digit($id))
->map(fn($id) => User::find($id))
->not(fn() => abort(404))
->is(fn($user) => info("User found {$user->id}"))
->get();
Using composer
composer require xtompie/guard
Guard protects for calling methods on nulls values. The value might or might not be present. It has elegant fluent chaining syntax. No need for if/else statments and additional variables. Guard syntax is cleaner and more readable. Guard object is immutable.
Methods:
of
- Creates guardofEmpty
- Creates empty guardis
- Tells if value present or runs callback when value presentnot
- Same asis
but negationget
- Gets raw valuegetFn
- Gets raw value with fallback callbackfilter
- Filter the value if presentreject
- Same asfilter
but negationmap
- Maps value if presentassert
- Throw exception if value is not presentblank
- Gets guard with provided value if current is not presentblankFn
- Same asblank
but with callbacklet
- Returns guard capture mechanism for nested properties, calls, array offsets
More info in source Guard.php
use Xtompie\Guard\Guard;
Guard::of(null)->assert()->get(); // NoValueException will be thrown
use Xtompie\Guard\Guard;
echo Guard::of(null)->get('default'); // -> default
use Xtompie\Guard\Guard;
function divide($a, $b) {
$b = Guard::of($b)
->map(fn($i) => (int)$i)
->reject(fn($i) => $i === 0)
->assert(\UnexpectedValueException::class)
->get();
return $a / $b;
}
let()
Returns Let object. Offset get, property get, method call can be called on Let.
After that operation new Guard with operation result will be returned.
When offset, property, method not exist, empty Guard will be returned.
use Xtompie\Guard\Guard;
$options = [
'a' => 'A',
'b' => 'B',
];
$key = 'c';
echo Guard::of($options)->let()[$key]->get();
use Xtompie\Guard\Guard;
echo Guard::of(new \stdClass())
->let()->nonExistingMethod()
->let()->nonExistingProperty
->let()['nonExistingOffset']
->get('Undefined')
;
namespace MyApp\Util;
use Xtompie\Guard\Guard as BaseGuard;
class Guard extends BaseGuard
{
public function or404()
{
$this->not(fn() => abort(404));
}
public function reject0()
{
return $this->reject(fn($i) => $i === 0);
}
}
echo gettype(Guard::of(0)->reject0()->get());