wookieb/type-check
Composer 安装命令:
composer require wookieb/type-check
包简介
Object oriented way to check the type of data
README 文档
README
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
- type constraints for validators
- definition of type for containers, maps https://github.com/wookieb/map
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
统计信息
- 总下载量: 89
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-09-12