PHPackages                             shalvah/ensure - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. shalvah/ensure

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

shalvah/ensure
==============

Clean alternative syntax for enforcing business requirements

1.0.0(7y ago)19891MITPHPPHP &gt;=5.6CI passing

Since Aug 5Pushed 5y ago1 watchersCompare

[ Source](https://github.com/shalvah/ensure)[ Packagist](https://packagist.org/packages/shalvah/ensure)[ RSS](/packages/shalvah-ensure/feed)WikiDiscussions master Synced yesterday

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

ensure ✋🛑
=========

[](#ensure-)

[![Latest Stable Version](https://camo.githubusercontent.com/d0695199918e6355f9aa1ce11bc47269182dde2305344a30d1ada2bfe47a6e0b/68747470733a2f2f706f7365722e707567782e6f72672f7368616c7661682f656e737572652f762f737461626c65)](https://packagist.org/packages/shalvah/ensure)[![Total Downloads](https://camo.githubusercontent.com/8b0f37113e83079ddbe3a31bdb7818ae2dbd838735c579a3332f67a926a8db52/68747470733a2f2f706f7365722e707567782e6f72672f7368616c7661682f656e737572652f646f776e6c6f616473)](https://packagist.org/packages/shalvah/ensure)[![Build Status](https://camo.githubusercontent.com/832014b4e092031e35b8c1547a4df577f35706af7e2747d85ecdb496f441ecec/68747470733a2f2f7472617669732d63692e636f6d2f7368616c7661682f656e737572652e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/shalvah/ensure)

✌🤩🤩Clean alternative syntax for enforcing business requirements. Useful for when your `if`s get too clunky.

```
use function Shalvah\Ensure\when;
use function Shalvah\Ensure\ensure;

ensure($pilot->isInUniform())
  ->orElseDeny('Please put on your uniform', $pilot->uniform);

when(!$pilot->isLicensed())
  ->ensure($flight->isTestFlight())
  ->orElseDeny('You are only allowed to fly test flights.');

when(!$flightPlanHasBeenSubmitted)
  ->ensure(
    $whitelistedAirports->includes($destinationAirport)
    || $flight->isTestFlight()
  )
  ->orElseDeny("You need to submit a flight plan!");
```

How to use
----------

[](#how-to-use)

- Specify a **requirement** using `ensure()`. This rule must either be an expression which evaluates to **strict boolean `true`/`false`**, or a callable which returns **strict boolean `true`/`false`**:

```
ensure($day == 'Saturday')->orElseDeny('Visiting hours are Saturdays only.');
ensure(function () use ($passenger) {
  $flight = Flight::getFlight($passenger->flightName);
  return $flight && $flight->passengers->includes($passenger->id);
})->orElseDeny('You are not listed for this flight.');
```

- You can also use `when` before calling `ensure`, to specify that the rule applies only in certain conditions:

```
when($couponCodeWasApplied)
  ->ensure($product->isEligibleForDiscount())
  ->orElseDeny('Sorry, this product is not eligible for promotions.');
```

- In `orElseDeny()`, specify a `message` and any additional `data`. This package will throw a `RequirementFailedException` with the specified message and data. You can then listen for this in your code (preferably at one central location) and send the appropriate response to the user:

```
try {
  ensure($user->isAllowedToView($product))
    ->orElseDeny('This product is not available in your region.', $this->suggestSimilarProducts($product, $user));
} catch (\Shalvah\Ensure\RequirementFailedException $e) {
    return response()->json([
      'message' => $e->getMessage(),
      'similar_products' => $e->getData()
    ], 400);
}

// or you could use set_exception_handller()
// or whatever mechanism your framework uses
```

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

[](#installation)

```
composer require shalvah/ensure
```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.9% 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

Unknown

Total

1

Last Release

2886d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ef367e338eb001771b4d439508b7a355d62dc2712ff94017393913e853816abb?d=identicon)[shalvah](/maintainers/shalvah)

---

Top Contributors

[![shalvah](https://avatars.githubusercontent.com/u/14361073?v=4)](https://github.com/shalvah "shalvah (20 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/shalvah-ensure/health.svg)

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

PHPackages © 2026

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