PHPackages                             xtompie/guard - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. xtompie/guard

ActiveLibrary

xtompie/guard
=============

Guard container object for dealing with optional values or null values

1.3.1(5y ago)07MITPHPPHP &gt;=7.4

Since Feb 27Pushed 5y ago1 watchersCompare

[ Source](https://github.com/xtompie/guard)[ Packagist](https://packagist.org/packages/xtompie/guard)[ RSS](/packages/xtompie-guard/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

Guard
=====

[](#guard)

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();
```

Installation
------------

[](#installation)

Using [composer](https://getcomposer.org/)

```
composer require xtompie/guard

```

Docs
----

[](#docs)

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 guard
- `ofEmpty` - Creates empty guard
- `is` - Tells if value present or runs callback when value present
- `not` - Same as `is` but negation
- `get` - Gets raw value
- `getFn` - Gets raw value with fallback callback
- `filter` - Filter the value if present
- `reject` - Same as `filter` but negation
- `map` - Maps value if present
- `assert` - Throw exception if value is not present
- `blank` - Gets guard with provided value if current is not present
- `blankFn` - Same as `blank` but with callback
- `let` - Returns guard capture mechanism for nested properties, calls, array offsets

More info in source [Guard.php](src/Guard.php)

### Usage

[](#usage)

#### NoValueException

[](#novalueexception)

```
use Xtompie\Guard\Guard;

Guard::of(null)->assert()->get(); // NoValueException will be thrown
```

#### Default value

[](#default-value)

```
use Xtompie\Guard\Guard;

echo Guard::of(null)->get('default'); // -> default
```

#### Complex type and value

[](#complex-type-and-value)

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

[](#let)

`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')
;
```

#### Extending

[](#extending)

```
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());
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~16 days

Total

5

Last Release

1843d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0c1316cdb671814b72350d75b23d7cafbde97ead0c21ac5613171a298bb15736?d=identicon)[xtompie](/maintainers/xtompie)

---

Top Contributors

[![xtompie](https://avatars.githubusercontent.com/u/69162230?v=4)](https://github.com/xtompie "xtompie (21 commits)")

---

Tags

guardnullnullablenullpointerexceptionoptionalguardoptionalnullnullablenullpointerexception

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/xtompie-guard/health.svg)

```
[![Health](https://phpackages.com/badges/xtompie-guard/health.svg)](https://phpackages.com/packages/xtompie-guard)
```

###  Alternatives

[kartik-v/bootstrap-checkbox-x

An extended checkbox plugin for bootstrap with three states and additional styles.

921.0M1](/packages/kartik-v-bootstrap-checkbox-x)[adaojunior/passport-social-grant

Social grant for Laravel Passport

116279.2k1](/packages/adaojunior-passport-social-grant)[kartik-v/yii2-checkbox-x

Extended checkbox widget for bootstrap with three states and additional styles.

26987.3k11](/packages/kartik-v-yii2-checkbox-x)[mpyw/null-auth

Null Guard for Laravel. Designed for Middleware-based authentication and testing.

18112.3k](/packages/mpyw-null-auth)[prayno/casauth-bundle

Basic CAS (SSO) authenticator for Symfony 3, 4 and 5

1685.9k](/packages/prayno-casauth-bundle)[pechente/kirby-password-guard

Kirby Password Guard

345.0k](/packages/pechente-kirby-password-guard)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
