PHPackages                             larapie/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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. larapie/guard

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

larapie/guard
=============

Guard objects for conditional exception throwing.

v1.1(7y ago)25791MITPHPPHP ^7.1CI failing

Since Mar 30Pushed 5y ago1 watchersCompare

[ Source](https://github.com/larapie/guard)[ Packagist](https://packagist.org/packages/larapie/guard)[ Docs](https://github.com/larapie/guard)[ RSS](/packages/larapie-guard/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)Dependencies (1)Versions (2)Used By (0)

Guard
=====

[](#guard)

[![Latest Stable Version](https://camo.githubusercontent.com/dbd0fa883c04632ad0772f2a812235194a21eddd977f5d027f2fc767a797f949/68747470733a2f2f706f7365722e707567782e6f72672f6c6172617069652f67756172642f762f737461626c65)](https://packagist.org/packages/larapie/guard)[![Total Downloads](https://camo.githubusercontent.com/551b4f7139a0454d53047b218b42e6a0cd81a77833af27c9f444791803b5471d/68747470733a2f2f706f7365722e707567782e6f72672f6c6172617069652f67756172642f646f776e6c6f616473)](https://packagist.org/packages/larapie/guard)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/9be629b60900a37fd440ab893d433eb52131c21a28b7414cc3d512b332e7aacb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c6172617069652f67756172642f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/larapie/guard/?branch=master)[![Build Status](https://camo.githubusercontent.com/3d45c966337a4751d3dcc786d2f896f4eac829905e66b49dc2de2200cc6748fb/68747470733a2f2f7472617669732d63692e6f72672f6c6172617069652f67756172642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/larapie/guard)[![Code Coverage](https://camo.githubusercontent.com/ce9d979a1f26f024a517b944ecb22e28619369ae64e6463ac25c0c9bcd7caf49/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c6172617069652f67756172642f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/larapie/guard/?branch=master)[![StyleCI](https://camo.githubusercontent.com/95af48bab61d886a0afa0d7601472283c979478f242062184884761dc2744e66/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3137383535303233312f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/178550231)

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

[](#installation)

You can install the package via composer:

```
composer require larapie/guard
```

Have you ever…
--------------

[](#have-you-ever)

… encountered a situation where you had countless if statements that needed to throw specific errors when conditions were met?

Here's an example:

```
public function foo()
{
    if($user==null)
        throw new UserNotFoundException();

    if($user->age < 18)
        throw new NotOldEnoughException(18);

    ...
}
```

The goal of this package is to decouple these conditions and their exceptions in a guard object. This guard object can then be handled by the GuardHandler. By structuring this code in a guard object, we gain several advantages:

- We're able to reuse the same conditions in the code elsewhere.
- The code becomes a lot easier to read.
- More fine grained control of what exceptions will be thrown.

Let's rewrite the example from above with guards:

```
public function foo()
{
    guard(new UserDoesNotExistsGuard($user), new UserInsufficientAgeGuard($user, 18));
}
```

Note that the order of the guards will determine what exception will be thrown first.

```
class UserDoesNotExistsGuard extends Guard
{

    /**
     * The exception that will be thrown when the condition is met
     *
     * @var string
     */
    protected $exception = UserNotFoundException::class;

    /**
     * @var User
     */
    protected $user;

    /**
     * UserDoesNotExistsGuard constructor.
     * @param $user
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * The condition that needs to be satisfied in order to throw the exception.
     *
     * @return bool
     */
    public function condition(): bool
    {
        return $this->user===null;
    }
}
```

```
class UserInsufficientAgeGuard extends Guard
{
    /**
     * @var User
     */
    protected $user;

    /**
     * @var int
     */
    protected $age;

    /**
     * UserDoesNotExistsGuard constructor.
     * @param $user
     */
    public function __construct(User $user, int $age)
    {
        $this->user = $user;
        $this->age = $age;
    }

    /**
     * The condition that needs to be satisfied in order to throw the exception.
     *
     * @return bool
     */
    public function condition(): bool
    {
        return $this->user->age < this->age;
    }

    /**
     * The exception that gets thrown when the condition is satisfied.
     *
     * @return \Throwable
     */
    public function exception(): \Throwable
    {
        return new NotOldEnoughException(18);
    }
}
```

Future
------

[](#future)

- Implement a feature that allows the guard handler to handle OR statements.

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Anthony Vancauwenberghe](https://github.com/larapie)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 60% 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 ~0 days

Total

2

Last Release

2602d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7df91709af6a5763e0a798d92645cfa35a20f18444cce401225f5a889c9d63ad?d=identicon)[larapie](/maintainers/larapie)

---

Top Contributors

[![anthonyvancauwenberghe](https://avatars.githubusercontent.com/u/7100315?v=4)](https://github.com/anthonyvancauwenberghe "anthonyvancauwenberghe (12 commits)")[![larapie](https://avatars.githubusercontent.com/u/48988808?v=4)](https://github.com/larapie "larapie (8 commits)")

---

Tags

guardlaravelphpsoftware-engineeringphplaravelexceptionsguardlarapiethrowing

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[yorcreative/laravel-argonaut-dto

Argonaut is a lightweight Data Transfer Object (DTO) package for Laravel that supports nested casting, recursive serialization, and validation out of the box. Ideal for service layers, APIs, and clean architecture workflows.

1062.8k1](/packages/yorcreative-laravel-argonaut-dto)[reducktion/socrates

A package to validate, and extract citizen information from, national identification numbers.

488.5k](/packages/reducktion-socrates)

PHPackages © 2026

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