ft / utils
PHP的个人实用工具包
Requires
- php: >=8
Requires (Dev)
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-09-18 21:55:50 UTC
README
> composer require ft/utils:dev-master
FT\Utils\Optional
用于处理可空值的类。
模仿Java的Optional<>
的实现
请参阅tests/OptionalTest.php
以获取使用示例和意图
FT\Utils
函数
-
last(mixed $value, ?callable $predicate = null, ?int $qty = null) : mixed
获取给定元素的最后一个组件
Utils::last(['a','b','c']); // 'c' Utils::last(['a','b','c'], qty: 2); //['b','c'] Utils::last(['a val','b val','c val'], fn($i) => str_starts_with($i, 'b'), qty: 2); // ['b val'] Utils::last([['key' => 'value', 'foo' => 'bar']]); //['foo' => 'bar'] Utils::last("foobar"); // 'r' Utils::last("foobar", 3); // 'bar' $class = new class { public function __construct( public readonly $foo = "bar", public readonly $bazz = "buzz" ) } Utils::last($class); // ['bazz' => 'buzz']
-
first
获取给定元素的第一个组件
Utils::first(['a','b','c']); // 'a' Utils::first(['a','b','c'], qty: 2); //['a','b'] Utils::first(['a val','b val','c val'], fn($i) => str_starts_with($i, 'b'), qty: 2); // ['b val'] Utils::first([['key' => 'value', 'foo' => 'bar']]); //['key' => 'value'] Utils::first("foobar"); // 'f' Utils::first("foobar", 3); // 'foo' $class = new class { public function __construct( public readonly $foo = "bar", public readonly $bazz = "buzz" ) } Utils::first($class); // ['foo' => 'bar']
-
guidv4
数组
-
flatten(array $haystack, int $depth = 1) : array
$arr = [[1,2,3],[4,5,6],[7,[8,9]]]; Utils::$Array::flatten($arr); //[1,2,3,4,5,6,7,[8,9]]
-
anyMatch(array $haystack, callable $predicate) : bool
$arr = [1,2,3,4,5] Utils::$Array::anyMatch($arr, 'is_int'); //true Utils::$Array::anyMatch($arr, 'is_string'); //false
-
allMatch(array $haystack, callable $predicate) : bool
$arr = [1,2,3,4,5] Utils::$Array::allMatch($arr, 'is_int'); //true Utils::$Array::allMatch($arr, fn($i) => $i > 3); //false
-
noneMatch(array $haystack, callable $predicate) : bool
$arr = [1,2,3,4,5] Utils::$Array::noneMatch($arr, 'is_int'); //false Utils::$Array::noneMatch($arr, 'is_string'); //true
-
assc_array_map(array $haystack, callable $callback): array
$headers = [ 'Content-Type' => 'application/json', 'Accept' => 'application/json' ]; Utils::$Array::assc_array_map($headers, fn ($k, $v) => "$k: $v"); /* ['Content-Type: application/json', 'Accept: application/json'] */
-
find_by_property_values(array $haystack, array $pvps) : mixed
返回第一个符合给定标准的结果元素
class User { public function __construct( public readonly string $fname, public readonly string $lname, public readonly int $age, ) } $users = [ new User('john', 'doe', 99), new User('jane', 'doe', 77), new User('jerry', 'doe', 88) ]; Utils::$Array::find_by_property_values($users, ['lname', 'doe']); // object(fname=john,lname=doe,age=99) Utils::$Array::find_by_property_values($users, ['lname' => 'doe', 'fname' => 'jerry']); // object(fname=jerry,lname=doe,age=88)
-
sort_by_property(array &$haystack, string $property, string $direction = 'asc')
按属性对对象数组进行排序
class User { public function __construct( public readonly string $fname, public readonly string $lname, public readonly int $age, ) } $users = [ new User('john', 'doe', 99), new User('jane', 'doe', 77), new User('jerry', 'doe', 88) ]; Utils::$Array::sort_by_property($users, 'age'); /* [ object(fname=jane), object(fname=jerry), object(fname=john) ] */ Utils::$Array::sort_by_property($users, 'age', 'desc'); /* [ object(fname=john), object(fname=jerry), object(fname=jane) ] */
-
sort_by_value_key(array &$haystack, string $key, string $direction = 'asc')
按值键对值数组进行排序
$users = [ [ 'fname' => 'john', 'lname' => 'doe', 'age' => 99 ], [ 'fname' => 'jane', 'lname' => 'doe', 'age' => 77 ], [ 'fname' => 'jerry', 'lname' => 'doe', 'age' => 88 ] ]; Utils::$Array::sort_by_value_key($users, 'age'); /* [ array(fname=jane), array(fname=jerry), array(fname=john) ] */ Utils::$Array::sort_by_value_key($users, 'age', 'desc'); /* [ array(fname=john), array(fname=jerry), array(fname=jane) ] */
路径
-
join(string ...$segments): string
使用原生
DIRECTORY_SEPARATOR
常量连接路径 -
scan_for_files(string $directory, string $file_regexp, array $ignore = []): array
递归地在给定的目录中搜索匹配给定模式的文件。返回一个包含
SplFileInfo
的数组此功能支持忽略文件
注意 忽略模式中的
/
无需转义所有路径都转换为真实路径,所有分隔符都转换为正斜杠
/
//return all php files Utils::$Path::scan_for_files(__DIR__, "\.php$"); Utils::$Path::scan_for_files(__DIR__, "\.php$", ignore: ['/vendor/?.*'] );
-
getFileSystemIteratorFilter
此自定义FileSystemIterator允许忽略模式(忽略是叶类型无关的,模式将验证目录和路径)
FileSystemIterator构造函数
public function __construct( RecursiveIterator $iterator, private readonly array $ignore = [] ) {}
FileSystemIterator示例用法
$dir_it = new RecursiveDirectoryIterator(__DIR__, RecursiveDirectoryIterator::SKIP_DOTS); $fs_it = Utils::$Path::getFileSystemIteratorFilter($dir_it, $ignore_array);
Preg
-
count(string $pattern, string $subject, int $flags = 0, int $offset = 0) : int
返回给定模式匹配的数量
这保证返回一个整数
-
match_all(string $pattern, string $subject, &$matches, int $flags = 0, int $offset = 0)
返回给定模式的全部匹配
$str = <<<'A' https://johndoe:pa$$w0rd@example.com https://janedoe:pa$$w0rd@example.com A; Utils::$Regex::match_all('/https:\/\/(?:(?<username>\w+):(?<password>\S+))@example\.com/', $str, $matches); $john_match = $matches[0]; //accessing match metadata $john_match->value; // https://johndoe:pa$$w0rd@example.com $john_match->start; // 0 $john_match->end; // 36 //accessing groups $john_match->groups->{'1'}; // johndoe $john_match->groups->username; // johndoe $john_match->g1; //johndoe $john_match->groups->{'2'}; // pa$$w0rd $john_match->groups->password; // pa$$w0rd $john_match->g2; //pa$$w0rd
字符串
-
charAt(string $value, int $index) : ?string
-
utf8(string $value): ?string
-
explode_implode( string $value, string $explode_delimiter, ?callable $foreach = null, ?string $implode_delimiter = null, int $limit = PHP_INT_MAX ) : string
快速拆分字符串的实用工具,可选地对每个值执行映射操作,然后再将其组合回字符串
注意 foreach映射应用于拆分操作
$str = "key=value; key2=value2; key3=value3"; $result = Utils::$String::explode_implode( $str, explode_delimiter: ";", implode_delimiter: " / ", foreach: fn($i) => Utils::$String::explode_implode($i, "=", null, " => ") ); echo $result; // "key => value / key2 => value2 / key3 => value3"