reestyle-it/examiner

允许您检查变量或类,并根据类型执行操作

v0.2-rc1 2024-02-20 12:53 UTC

This package is auto-updated.

Last update: 2024-09-28 19:52:57 UTC


README

让您快速检查,或者更准确地说,评估对象和/或数据类型。主要依赖于基本功能和反射类以确保快速评估。

请记住,当与对象一起工作时,您只能检查公共成员。尝试从对象外部(除继承外)访问私有或受保护的成员是没有意义的。

编程用例

检查提供的 thing 是否为给定的类型,然后调用给定的参数。如果不是给定的类型,则返回 null 或执行给定的闭包。

示例

$bool = true;
examine($bool)->whenBoolean(
    fn () => true,   // True block 
    fn () => false   // False block
);

对象

$var = new SomeObject();
examine($var)->instanceOf(AbstractObject);
examine($var)->hasTrait(AbstractObject);
examine($var)->hasMethod('objectMethod');
examine($var)->hasProperty('objectProperty');

数据类型

布尔值

$bool = true;
examine($bool)->isTrue(); // true
examine($bool)->isFalse(); // false (duh)
examine($bool)->isEmpty(); // MethodNotFoundException
examine($bool)->ignoreType()->isEmpty(); // null

字符串

examine(' ')->couldBeTrue(); // true
examine('')->couldBeTrue(); // false
examine(0)->couldBeTrue(); // false
examine(0)->couldBeFalse(); // true
examine(' ')->isEmpty(); // false
examine('')->isEmpty(); // true
examine(null)->isEmpty(); // MethodNotFoundException
examine(0)->couldBeTrue(); // false
examine(0)->couldBeFalse(); // true

整数 & 浮点数

examine(1)->isInt(); // true
examine(1)->isFloat(); // false
examine(1.0)->isFloat(); // true
examine(1.0)->isInt(); // false
examine(1)->isTrue(); // false
examine(1)->couldBeTrue(); // true

examine(1.0)->whenFloat(fn () => 13.45);