wookieb/type-check

Object oriented way to check the type of data

0.2 2013-09-13 21:34 UTC

This package is not auto-updated.

Last update: 2024-12-16 16:04:50 UTC


README

Build Status Provide objects oriented and more abstract way to check the type of data.

Note! This library it's not a replacement for functions is_* and other type check functions!

Use cases

Install

Via composer

    "require": {
        "wookieb/type-check": "0.*"
    }

Usage

use Wookieb\TypeCheck\SimpleTypeCheck;
use Wookieb\TypeCheck\ObjectTypeCheck;
use Wookieb\TypeCheck\MultipleTypesCheck;

$string = new SimpleTypeCheck('string'); // accepts only string
$exceptions = new ObjectTypeCheck('\Exception'); // accepts every object that is instance of \Exception
$onlyPureExceptions = new ObjectTypeCheck('\Exception', true); // accepts only \Exception objects (not children)
$multi = new MultipleTypesCheck($string, $exceptions);

$string->isValidType('foo'); // true
$string->isValidType(true); // false
echo $string->getTypeDescription(); // strings

$exceptions->isValidType(new \InvalidArgumentException('foo')); // true
$exceptions->isValidType(null); // false
echo $exceptions->getTypeDescription(); // instances of Exception

$onlyPureExceptions->isValidType(new \Exception('foo')); // true
$onlyPureExceptions->isValidType(new \InvalidArgumentException('foo')); // false
echo $onlyPureExceptions->getTypeDescription(); // objects of class Exception

$multi->isValidType(new \InvalidArgumentException('foo')); // true
$multi->isValidType('foo'); // true
$multi->isValidType(array()); // false
echo $multi->getTypeDescription(); // strings, instances of Exception

Memory usage

You don't need to always create objects of SimpleTypeCheck for common data types. Instead you can use an singleton instance for each basic type.

TypeCheck::strings();
TypeCheck::integers();
TypeCheck::floats();
TypeCheck::objects();
TypeCheck::booleans();
TypeCheck::arrays();
TypeCheck::resources();

Other type checks

TraversableOf

Allows to check the type of traversable (arrays or instances of \Traversable interface) elements

use Wookieb\TypeCheck\TraversableOf;
use Wookieb\TypeCheck\TypeCheck;

$check = new TraversableOf(TypeCheck::strings());
$check->isValidType(array(1, 'foo', 3)); // false
$check->isValidType(array('foo', 'bar', 'zee')); // true
echo $check->getTypeDescription(); // traversable structures that contains strings

CallbackTypeCheck

Uses callback as validation function

use Wookieb\TypeCheck\CallbackTypeCheck;

$check = new CallbackTypeCheck(function ($value) {
    return is_array($value) && reset($value) === 'foo';
}, 'arrays with foo string');

$check->isValidType(array('bar')); // false
$check->isValidType(array('foo')); // true
echo $check->getTypeDescription(); // arrays with foo string

AllChecks

Value must pass all defined type check

use Wookieb\TypeCheck\TraversableOf;
use Wookieb\TypeCheck\TypeCheck;
use Wookieb\TypeCheck\AllChecks;

// narrow the field of "traversable" data types to arrays
$check = new AllChecks(TypeCheck::arrays(), new TraversableOf(TypeCheck::strings()));
$check->isValidType(new ArrayIterator(array('foo', 'bar'))); // false
$check->isValidType(array('foo', 'bar')); // true
echo $check->getTypeDescription(); // arrays traversable structures that contains strings

Changelog

0.2

  • added AllChecks, CallbackTypeCheck, TraversableOf