sharoff45 / library-support
辅助库
v1.0.0
2022-06-09 11:22 UTC
Requires
- php: >=8.0
- ext-json: *
- ramsey/uuid: ^3.4 || ^4.1
- symfony/property-access: ^4.3 || ^5.0 || ^6.0
Requires (Dev)
- icanhazstring/composer-unused: ^0.7.5
- phpunit/phpunit: ^9.5
- sharoff45/library-cs-fixer: ^1.0
- sharoff45/library-phpstan-rules: ^1.0
This package is auto-updated.
Last update: 2024-09-09 16:00:55 UTC
README
composer
composer require sharoff45/library-support
函数和同名类的描述
isEmptyArrayFilter
[] === array_filter($array)
isNotEmptyArrayFilter
[] !== array_filter($array)
isEmptyArray [IsEmptyArray]
[] === $value
isNotEmptyArray [IsNotEmptyArray]
true === is_array($value) && [] !== $value
isNotEmptyString [IsNotEmptyString]
true === is_string($value) && '' !== $value
isNotNull [IsNotNull]
null !== $value
isNull [IsNull]
null === $value
isNullOrEmptyString [IsNullOrEmptyString]
null === $value || '' === $value
isNullOrZeroNumber [IsNullOrZeroNumber]
null === $value || 0 === $value || 0.0 === $value
其他
strStartsWith
检查字符串是否以指定的字符(子字符串)开头objectToArray
返回对象数据,不使用反射
class A
{
private $a = 1;
protected $b = 2;
public $c = 3;
}
var_dump(objectToArray(new A));
结果
array:3 [▼
"a" => 1
"b" => 2
"c" => 3
]
all
如果数组中的每个元素都满足 $callback 函数中的条件,则返回 TRUE
$isBelowThreshold = function ($value) {
return $value < 40;
};
$items = [1, 30, 39, 29, 10, 13];
var_dump(all($items, $isBelowThreshold));
结果
true
- MethodHelper::isSetter - 如果方法名称以 set[A-Z] 开头,则为 true
- MethodHelper::isAdder - 如果方法名称以 add[A-Z] 开头,则为 true
- MethodHelper::isGetter - 如果方法名称以 get[A-Z] 开头,则为 true
- MethodHelper::isLogic - 如果方法名称以 is[A-Z] 或 getIs[A-Z] 开头,则为 true
使用 array_filter 的示例
use Sharoff45\Library\Support\IsNotEmptyArray; use Sharoff45\Library\Support\IsNotEmptyString; use Sharoff45\Library\Support\IsNotNull; use Sharoff45\Library\Support\IsNull; use Sharoff45\Library\Support\IsNullOrEmptyString; use Sharoff45\Library\Support\IsEmptyArray; // ... $result = array_filter($list, new IsNotEmptyArray()); //... $result = array_filter($list, new IsNotEmptyString()); // ... $result = array_filter($list, new IsNotNull()); //... $result = array_filter($list, new IsNull()); // ... $result = array_filter($list, new IsNullOrEmptyString()); // ... $result = array_filter($list, new IsEmptyArray());
条件中使用示例
use function Sharoff45\Library\Support\isEmptyArray; use function Sharoff45\Library\Support\isNotEmptyArray; use function Sharoff45\Library\Support\isNotEmptyString; use function Sharoff45\Library\Support\isNotNull; use function Sharoff45\Library\Support\isNull; use function Sharoff45\Library\Support\isNullOrEmptyString; use function Sharoff45\Library\Support\isEmptyArrayFilter; use function Sharoff45\Library\Support\isNotEmptyArrayFilter; if (isEmptyArray($value)) {} if (isNotEmptyArray($value)) {} if (isNotEmptyString($value)) {} if (isNotNull($value)) {} if (isNull($value)) {} if (isNullOrEmptyString($value)) {} if (isEmptyArrayFilter($array)) {} if (isNotEmptyArrayFilter($array)) {}
处理 JSON 的函数
抛出异常
- Json::encode - 返回数据的 JSON 表示形式
echo Json::encode(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]);
结果
{"a":1,"b":2,"c":3,"d":4,"e":5}
解码
- Json::decodeAsArray - 将 JSON 对象返回为关联数组(array)
var_dump(Json::decodeAsArray('{"a":1,"b":2,"c":3,"d":4,"e":5}'));
结果
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
- Json::decodeAsObject - 将 JSON 对象返回为对象(object)
var_dump(Json::decodeAsObject('{"a":1,"b":2,"c":3,"d":4,"e":5}'));
结果
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
В случае ошибки возвращается `JsonException`
生成 Uuid
UuidGenerator::getUuid 方法用于获取 uuid,无论输入是 ID 还是已经准备好的 uuid