实用函数集合

3.0.0 2019-07-17 02:39 UTC

This package is auto-updated.

Last update: 2024-09-04 17:01:05 UTC


README

实用函数集合

安装

composer require cosmologist/gears

数组函数

将元素推送到数组的末尾并返回修改后的数组
$a = [1,2];
ArrayType::push($a, 3); // [1,2,3]
将元素添加到数组的开头并返回修改后的数组
$a = [2,3];
ArrayType::unshift($a, 1); // [1,2,3]
计算数组中值的平均值(array_avg)
ArrayType::average([1, 2, 3]); // 3
检查数组是否为关联数组
ArrayType::checkAssoc([1, 2, 3]); // false
ArrayType::checkAssoc(['foo' => 'bar']); // true
检查值是否存在于数组中
ArrayType::contains(array $list, mixed $item);
通过键获取数组中的项
ArrayType::get(['fruit' => 'apple', 'color' => 'red'], 'fruit'); // apple
ArrayType::get(['fruit' => 'apple', 'color' => 'red'], 'weight'); // null
ArrayType::get(['fruit' => 'apple', 'color' => 'red'], 'weight', 15); // 15
在键后插入数组
ArrayType::insertAfter(['a' => 1, 'c' => 3], 'a', ['b' => 2]); // ['a' => 1, 'b' => 2, 'c' => 3]
// If the key doesn't exist
ArrayType::insertAfter(['a' => 1, 'b' => 2], 'c', ['foo' => 'bar']); // ['a' => 1, 'b' => 2, 'foo' => 'bar']
在键前插入数组
ArrayType::insertBefore(['a' => 1, 'c' => 3], 'c', ['b' => 2]); // ['a' => 1, 'b' => 2, 'c' => 3]
// If the key doesn't exist
ArrayType::insertBefore(['a' => 1, 'b' => 2], 'c', ['foo' => 'bar']); // ['foo' => 'bar', 'a' => 1, 'b' => 2]
将项目列表转换为范围
ArrayType::ranges([1, 3, 7, 9]); // [[1, 3], [3, 7], [7, 9]]
通过值取消设置数组项
ArrayType::unsetValue(['a', 'b', 'c'], 'b'); // ['a', 'c']
列表遍历器

遍历列表并对每个项目调用回调函数。

ArrayType::each(iterable $list, callable $callback)
列表及其子项的递归遍历器

遍历列表并对每个项目和每个子项(递归)调用回调函数。

ArrayType::eachDescendantOrSelf(iterable $list, callable $callback, string $childrenKey)
递归收集子项

收集列表中每个项目的子项,以及项目本身。

ArrayType::descendantOrSelf(iterable $list, string $childrenKey): ArrayObject
转换为数组

不同类型的行为

  • 数组 - 返回原样
  • 可迭代 - 转换为本地数组(iterator_to_array()
  • 另一个 - 创建一个具有参数的数组([值])
ArrayType::toArray($value);
验证变量的内容是否为可计数值

如果PHP >= 7.3.0,则使用is_countable函数

ArrayType::isCountable($arrayOrCountable): bool;

JSON函数

解码JSON字符串,使用异常代替错误。

try {
    Json::decode($json);
    // or
    Json::decodeToArray($json);
} catch (JsonParseException $e) {
    throw $e;
}

对象函数

读取对象图属性路径末尾的值
ObjectType::get($person, 'address.street');

使用Symfony PropertyAccessor

设置对象图属性路径末尾的值
ObjectType::set($person, 'address.street', 'Abbey Road');

使用Symfony PropertyAccessor

读取内部对象属性值(受保护和私有)

阅读 ocramius

ObjectType::readInternalProperty($object, $property);
将值写入内部对象属性(受保护和私有)

阅读 ocramius

ObjectType::writeInternalProperty($object, $property, $value);
对象字符串表示形式

返回__toString的结果或如果该方法未定义则返回null。
PHP默认行为:如果未定义该方法,则触发错误(“无法将X类对象转换为字符串”)。

ObjectType::toString($instance);
将对象或FQCN转换为FQCN

返回__toString的结果或如果该方法未定义则返回null。
PHP默认行为:如果未定义该方法,则触发错误(“无法将X类对象转换为字符串”)。

ObjectType::toClassName($objectOrClass): string;

字符串函数

确定给定的字符串是否包含给定的子串
StringType::contains('Foo', 'Bar'); // false
StringType::contains('FooBar', 'Bar'); // true
使用libsodium的密钥简单对称解密字符串
StringType::decrypt(StringType::encrypt('The sensitive string', 'qwerty123456'), 'qwerty123456'); // 'The sensitive string'
使用libsodium的密钥简单对称加密字符串
StringType::encrypt('The sensitive string', 'qwerty123456');
执行正则表达式匹配的便捷方式

默认行为类似于preg_match_all(..., ..., PREG_SET_ORDER)

StringType::regexp('a1b2', '\S(\d)'); // [0 => [0 => 'a1', 1 => '1'], 1 => [0 => 'b2', 1 => '2']]

排除正则表达式匹配中的完整匹配

StringType::regexp('a1b2', '\S(\d)', true); // [0 => [0 => '1'], 1 => [0 => '2']]

只从正则表达式匹配中获取第一个集合

StringType::regexp('a1b2', '(\S)(\d)', true, true); // [0 => 'a', 1 => '1']

只从正则表达式匹配中获取每个集合的第一个匹配项(排除完整匹配)

StringType::regexp('a1b2', '(\S)(\d)', true, false, true); // [0 => 'a', 1 => 'b']

只从正则表达式匹配中获取第一个集合的第一个匹配项作为单个标量值

StringType::regexp('a1b2', '(\S)(\d)', true, true, true); // 'a'
替换字符串中的第一个字符串出现
StringType::replaceFirst('name name name', 'name', 'title'); // 'title name name'
包装字符串
StringType::wrap('target', '/'); // '/target/'
猜测字符串的类型
StringType::guessMime(file_get_contents('/foo/bar.baz'));
猜测适合字符串的文件扩展名
StringType::guessExtension('Foo bar baz'); // txt
检查字符串是否为二进制字符串
StringType::isBinary('Foo bar baz'); // false
将字符串转换为驼峰式
StringType::toCamelCase('string like this'); // 'StringLikeThis'
StringType::toCamelCase('string_like_this'); // 'StringLikeThis'
将字符串转换为snake_case
StringType::toSnakeCase('StringLikeThis'); // 'string_like_this'
StringType::toSnakeCase('string Like this'); // 'string_like_this'
ltrim()/rtrim()/trim()替换支持UTF-8字符在字符列表中

仅当您提供可选的charlist参数并且它包含UTF-8字符时才使用这些。否则,trim将在UTF-8字符串上正常工作。

trim('«foo»', '»'); // "�foo"
StringType::trim('«foo»', '»'); // "«foo"

数字函数

从参数中解析浮点数或整数值

移除参数中除了数字、+-、.、eE之外的所有字符,并返回浮点数值或解析失败时返回NULL。

NumberType::parse(" 123 "); // int(123)
NumberType::parse(" 123.45 "); // float(123.45)
NumberType::parse(" 123.00 "); // int(123)

从参数中解析浮点数值

移除参数中除了数字、+-、.、eE之外的所有字符,并返回浮点数值或解析失败时返回NULL。

NumberType::parseFloat(" 123 "); // float(123)
NumberType::parseFloat(" 123.45 "); // float(123.45)

从参数中解析整数值

移除参数中除了数字、正负号之外的所有字符,并返回整数值或解析失败时返回NULL。

NumberType::parseInteger(" 123 "); // int(123)
NumberType::parseFloat(" 123.45 "); // int(12345)

返回浮点数值的分数部分

NumberType::fractions(123.45); // float(0.45)
NumberType::parseFloat(123); // float(0)

检查值是否为奇数

NumberType::odd(2); // false
NumberType::odd(3); // true

检查值是否为偶数

NumberType::even(2); // true
NumberType::even(3); // false
四舍五入到最接近的倍数
NumberType::roundStep(50, 5); // 50
NumberType::roundStep(52, 5); // 50
NumberType::roundStep(53, 5); // 55
向下四舍五入到最接近的倍数
NumberType::floorStep(50, 5); // 50
NumberType::floorStep(52, 5); // 50
NumberType::floorStep(53, 5); // 50
向上四舍五入到最接近的倍数
NumberType::ceilStep(50, 5); // 50
NumberType::ceilStep(52, 5); // 55
NumberType::ceilStep(53, 5); // 55
拼读
// Current locale used
NumberType::spellout(123.45); // one hundred twenty-three point four five

// Specific locale used
NumberType::spellout(123.45, 'ru'); // сто двадцать три целых сорок пять сотых
零容忍度除法
NumberType::divideSafely(1, 0); // null
NumberType::divideSafely(1, null); // null
NumberType::divideSafely(1, 0, 'zero'); // 'zero'
百分比计算

第一个参数是用于计算百分比的值。第二个参数是对应于100%的基数。

NumberType::percentage(10, 100); // 10 
NumberType::percentage(100, 100); // 100  
NumberType::percentage(200, 100); // 200  
去除数的符号

负值将被转换为零,正数或零值将返回未改变。

NumberType::unsign(-1); // 0
NumberType::unsign(-0.99); // 0
NumberType::unsign(0); // 0
NumberType::unsign(0.99); // 0.99
NumberType::unsign(1); // 1
将数字转换为带符号的字符串。
NumberType::toStringWithSign(-1); // "-1"
NumberType::toStringWithSign(1); // "+1"
NumberType::toStringWithSign(0); // "0"

可调用函数

获取适合调用的反射对象

CallableType::reflection('is_null'); // Returns a ReflectionFunction instance
CallableType::reflection([$foo, 'bar']); // Returns a ReflectionMethod instance

类函数

获取类的简称

ClassType::shortName('Foo\Bar'); // "Bar"
ClassType::shortName(Foo\Bar::class); // "Bar"
ClassType::shortName(new Foo\Bar()); // "Bar"

获取类及其父类

namespace Foo;

class Bar {};
class Baz extends Foo {};
...
ClassType::selfAndParents('Foo\Bar'); // ["Foo\Bar"]
ClassType::selfAndParents(Foo\Bar::class); // ["Foo\Bar"]
ClassType::selfAndParents(new Foo\Bar()); // ["Foo\Bar"]
ClassType::selfAndParents('Foo\Baz'); // ["Foo\Baz", "Foo\Bar"]
ClassType::selfAndParents(Foo\Baz::class); // ["Foo\Baz", "Foo\Bar"]
ClassType::selfAndParents(new Foo\Baz()); // ["Foo\Baz", "Foo\Bar"]