gcworld / globals
3.3.0
2023-05-05 06:12 UTC
Requires
- php: >=8.0
- ext-json: *
- ext-mbstring: *
- ramsey/uuid: >=4.0
README
GLOBALS 允许您通过自动或特定过滤内容,以及支持数组批量处理,轻松管理 PHP 中的全局变量。
此实现是 Coercive/Globals(可在 https://github.com/Coercive/Globals 找到)的分支。
我一直在 composer.json 中使用覆盖来使用分支而不是原始版本,但由于它们已经分叉,我觉得最好尝试分离命名空间和包。
注意:此包包含一个用于减轻过渡痛苦的类,可能会与原始版本冲突。
获取
composer require gcworld/globals
用法
use GCWorld\Globals; # LOAD $oGlobals = new Globals; # DEFAULT $oGlobals->filter(true); # You can turn off by setting 'false' # SET VAR $oGlobals->GET('name', 'value'); # GET VAR $var = $oGlobals->GET('name'); # FOR EXAMPLE : $_GET['array'] = ['email@email.email', 'not an email']; $_GET['notInt'] = '01234'; $_GET['int'] = '14244'; $_GET['_int'] = '-14244'; $_GET['float'] = '142.24'; $_GET['_float'] = '+142.24'; $_GET['bool'] = 'false'; $_GET['_bool'] = true; $_GET['quote'] = '"'; # FILTER ALL $var = $oGlobals->GET()->filterAll(); /** ["array"] [0]=> string(17) "email@email.email" [1]=> string(12) "not an email" ["notInt"]=> string(5) "01234" ["int"]=> int(14244) ["_int"]=> int(-14244) ["float"]=> float(142.24) ["_float"]=> float(142.24) ["bool"]=> bool(false) ["quote"]=> string(10) """ */ # FILTER ONE (including array of elements) $var = $oGlobals->email()->GET('array'); /** ["array"] [0]=> string(17) "email@email.email" [1]=> bool(false) */
支持的全局变量
COOKIE
ENV
FILES
GET
POST
REQUEST
SERVER
SESSION
可用的过滤器
->octal()->...
->int()->...
->float()->...
->bool()->...
->ip()->...
->ipv4()->...
->ipv6()->...
->callback(Callable $callback)->...
->json(bool $asArray)->...
->array()->...
->email()->...
->url()->...
->date()->...
->dateTime()->...
->mac()->...
->string()->...
->stringSpecial()->...
->stringFull()->...
->uuid()->...
->noFilter()->...
附加功能
# You can filter a variable (or a part of global for re-inject)
$Result = $oGlobals->autoFilterManualVar($YourVar);
关于过滤器的说明
string()
过滤器运行 trim(strip_tags()),可能不是您需要的。stringSpecial()
是过滤器的等效函数。callback
过滤器需要一个可调用的函数。之前,这仅设置过滤器类型,没有正确工作。date()
和dateTime
过滤器在将输入转换为 Y-m-d( H:i:s) 格式之前检查strtotime($input) !== false
。