PHPackages                             jcfrane/policy - 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. jcfrane/policy

ActiveProject

jcfrane/policy
==============

A simple and naive policy implementation

v0.1.0(8y ago)212MITPHPPHP &gt;=7.0

Since Mar 28Pushed 8y ago1 watchersCompare

[ Source](https://github.com/jcfrane/policy)[ Packagist](https://packagist.org/packages/jcfrane/policy)[ RSS](/packages/jcfrane-policy/feed)WikiDiscussions master Synced 2mo ago

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

Policy
======

[](#policy)

A naive and simple implementation for Policy classes.

Benefits
--------

[](#benefits)

- Avoid nested if's in your code.
- Aggregate conditional logic in once place.

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

[](#how-to-use)

Create a class that extends **AbstractPolicy**

```
use JcFrane\Policy\AbstractPolicy;

class BlogPolicy extends AbstractPolicy
{

}
```

Create methods inside the your Policy class that represent each conditional logic for the said policy.

```
use JcFrane\Policy\AbstractPolicy;

class BlogPolicy extends AbstractPolicy
{
    public function canCreate($subject)
    {
        $this->allow($subject->canCreate, 'Cannot create a subject.');
    }

    public function canView($subject)
    {
        $this->allow($subject->canView, 'Cannot view the subject.');
    }

    public function canDelete($subject)
    {
        $this->forbid($subject->canDelete, 'Cannot delete the subject.');
    }

    public function canUpdate($subject)
    {
        $this->forbid($subject->canUpdate, 'Cannot update the subject');
    }
}
```

Evaluate your Policy Class

```
use JcFrane\Polcy\PolicyChecker;

use Some\Namespace\BlogPolicy;

$checker = new PolicyChecker();

$subject = new \stdClass();
$subject->canCreate = true;
$subject->canView = false;
$subject->canDelete = true;
$subject->canUpdate = false;

$result = $checker->check(BlogPolicy::class, $subject, 'create'); // returns true

$result = $checker->check(BlogPolicy::class, $subject, 'view'); // throws JcFrane/PolicyViolationException

$result = $checker->check(BlogPolicy::class, $subject, 'delete'); // returns true

$result = $checker->check(BlogPolicy::class, $subject, 'update'); // throws JcFrane/PolicyViolationException
```

That's it! You can now create multiple policy classes that aggregates your conditional logic inside your code.

The allow() and forbid() methods
--------------------------------

[](#the-allow-and-forbid-methods)

The **allow()** accepts a boolean and a violation message. If first argument is true, then the **check()** will return true. Otherwise, will throw a ViolationException.

The **forbid()** accepts a boolean and a violation message. If first argument is false, then the **check()** will throw a ViolationException. Otherwise, will return true.

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

[](#installation)

```
$ composer install jcfrane/policy
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

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

2970d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2dfbe5a9ae770e600c736bec7beef2ad46eca3cc19b3cb0a8de9816b615b8c96?d=identicon)[jcfrane](/maintainers/jcfrane)

---

Top Contributors

[![jcfranezmtsys](https://avatars.githubusercontent.com/u/93380752?v=4)](https://github.com/jcfranezmtsys "jcfranezmtsys (6 commits)")[![jcfrane](https://avatars.githubusercontent.com/u/7079154?v=4)](https://github.com/jcfrane "jcfrane (1 commits)")

---

Tags

phpPolicy

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jcfrane-policy/health.svg)

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

###  Alternatives

[pestphp/pest-plugin-stressless

Stressless plugin for Pest

67792.6k16](/packages/pestphp-pest-plugin-stressless)

PHPackages © 2026

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