Home

Awesome

Monotype

Build Status SensioLabsInsight License Latest Stable Version Dependency Status Scrutinizer Code Quality Code Coverage Code Climate

Monotype is a small utility library to check if given variable matches specific type requirements. PHP is known for its issues with variable type comparison mechanics so why don't reduce the pain of developers with some useful code?

Requirements

No required dependencies, only PHP >=5.4

PHP 5.3 will work too most of the time. It does not support using $this in closures (which are used in array checking methods), so since it has already reached its EOL I'm not fixing these errors. It is possible, but you need to make manual fixes yourself where applicable.

Installation

This library is registered on Packagist, so you can just execute

composer require thunderer/monotype

in your terminal or manually update your composer.json with

(...)
"require": {
    "thunderer/monotype": "dev-master"
}
(...)

and run composer install or composer update afterwards. If you're not using Composer, then... (really, please just use it).

Usage

Create instance by passing strategy and register array of types:

use Thunder\Monotype\Strategy\AllStrategy;
use Thunder\Monotype\Type\IntegerType;
use Thunder\Monotype\Type\IntegerValueType;
use Thunder\Monotype\Type\StringType;
use Thunder\Monotype\Type\StringValueType;

$monotype = new new Thunder\Monotype\Monotype(new AllStrategy(), array(
    // ...
    new IntegerType(),
    new IntegerValueType(),
    new StringType(),
    new StringValueType(),
    // ...
    ));

No further steps required, please just use it:

$monotype->isValid(12, array('integer')); // true
$monotype->isValid(12, array('string')); // false
$monotype->isValid('12', array('integer')); // false
$monotype->isValid('12', array('@integer')); // true
$monotype->isValid('x', array('string')); // true
$monotype->isValid(null, array('string')); // false

Reference

There are several types and strategies built right into the codebase of this library:

License

See LICENSE file in the main directory of this library.