PHPackages                             ntwalibas/contracts - 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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. ntwalibas/contracts

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

ntwalibas/contracts
===================

Contracts libray, a design by contract libray.

0.1.1(10y ago)422MITPHPPHP &gt;=5.3.0

Since Jun 19Pushed 10y ago1 watchersCompare

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

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

Contracts - an assertion library
================================

[](#contracts---an-assertion-library)

**STATUS:** Full test coverage, more predicates are the next goal and a better documentation.

> This library is *somewhat* production ready. Please don't use it yet for sensitive data validation like in the money related domain.

Contracts is a library to help you write quite intersting assertions. Initially, I was looking at making design by contract possible in PHP but due to the nature of task, I ended up settling with assertions but kept the name.

It is quite powerful because it implements [first-order predicate calculus](http://en.wikipedia.com/wiki/First-order_logic) in an intuitive way, a feature I have failed to find in many assertion and validation librairies. Heck, you can even implement your own predicates.
It is still in its early stages (many native predicates are not implemented) but it is meant to grow!

Install
-------

[](#install)

The library is available on [packagist](https://packagist.org/) and installable via [composer](http://getcomposer.org).

```
{
    "require": {
        "ntwalibas/contracts": "0.1.1"
    }
}
```

Concepts
--------

[](#concepts)

You generally write propositions that will be evaluated when you want to use them. Each proposition is made of predicates that can joined by logical operators such as `AND`, `OR`, `IMPLIES` and `EQUIVALENT`. The `NOT` logical operator is not implement and is rather replaced by negated predicates.

Usage
-----

[](#usage)

Usage is fairly simple: pass your predicates or quantifiers to the `AssertThat` funtion or `Assert::That` static method and it will throw an `AssertionFailedException` exception if there was any failure.
So let's understand first the idea of predicates, quantifiers and computations.

### Predicates

[](#predicates)

A predicate is simply a function that returns `true` or `false`. In our case, the said functions will actually be methods on specific objects.

In predicate calculus, a predicate will have variables that take different values for evaluation. The same goes for us here, with a few syntactical differences. An example shall illustrate.

Assume we want to evaluate whether a given number is greater than 18. So how do we do that?

```
