imj / filter
A library provides tools to filter variable
v1.0.3
2016-06-23 06:07 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ~4.3.0
This package is not auto-updated.
Last update: 2024-11-07 01:39:02 UTC
README
Installation
composer require imj/filter
Basic Usage
filter string
use Imj\Filter $str = ' str'; echo Filter::string($str); // str $str = 'abcdefg'; echo Filter::string($str, ['length'=>2]); // ab $str = 'abc'; echo Filter::string($str, ['regex'=>"/\d+/"]); // null
filter int/uint/float/ufloat
use Imj\Filter $int = '10'; echo Filter::int($int, ['max'=>11, 'min'=>8]); // 10 $int = '10'; echo Filter::int($int, ['max'=>8]); // 8 $int = '10'; echo Filter::int($int, ['min'=>11]); // 11 $int = '10'; echo Filter::int($int, ['min'=>11, 'default'=>100]); // 100 $int = -1; echo Filter::uint($int, ['default'=>100]); // uint/float/ufloat method is the same
filter by enum
use Imj\Filter $v = 'foo'; echo Filter::enum($v, ['enum'=>['foo', 'bar']]); // foo $v = 'baz'; echo Filter::enum($v, ['enum'=>['foo', 'bar']]); // foo $v = 'baz'; echo Filter::enum($v, ['enum'=>['foo', 'bar'], 'default'=>'fbb']); //fbb
filter by enums key
use Imj\Filter $enum = [ 'a' => 'foo', 'b' => 'bar', 'c' => 'baz' ]; $v = 'a'; echo Filter::enumByKey($v, ['enum'=>$enum]); // foo $v = 'd'; echo Filter::enumByKey($v, ['enum'=>$enum]); // foo $v = 'd'; echo Filter::enumByKey($v, ['enum'=>$enum, 'default_key'=>'b']); // bar $v = 'd'; echo Filter::enumByKey($v, ['enum'=>$enum, 'default'=>'baz']); // baz $v = 'd'; echo Filter::enumByKey($v, ['enum'=>$enum, 'default_key'=>'b', 'enum_key' => true]); // b
filter json
use Imj\Filter $arr = ['foo'=>1]; $v = json_encode($arr); var_dump(Filter::json($v, ['json_assoc'=>true])); // ['foo'=>1] $arr = ['foo'=>'1', 'bar'=>'2']; $v = json_encode($arr); var_dump(Filter::json($v, ['json_assoc'=>true, 'json_schema'=>['foo' => [Filter::UINT_TYPE]]])); // ['foo'=>1, 'bar'=>'2']
In addition, you can also use validate
method and the effect is the same.
use Imj\Filter $str = 'abcdefg'; echo Filter::validate($str, Filter::STRING_TYPE, ['length'=>2]); // ab // ...
The second parameter indicates the variable type, it could be:
Filter::STRING_TYPE Filter::INT_TYPE Filter::UINT_TYPE Filter::FLOAT_TYPE Filter::UFLOAT_TYPE Filter::ENUM_TYPE Filter::ENUM_KEYS_TYPE Filter::JSON_TYPE Filter::NONE_TYPE
License
licensed under the MIT License - see the LICENSE
file for details