phannaly / laravel-helpers
PHP 辅助函数从 Laravel 中提取
Requires (Dev)
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-09-29 05:02:09 UTC
README
此项目独立,除非您想贡献,否则不需要安装任何东西(composer)。
大部分代码来自 Laravel 代码库。
要求
- PHP 7.0 或更高版本
设置
如果您的项目没有 composer,则不需要通过 composer 安装。
只需手动将其导入 src
文件夹。
但是,如果您想通过 composer 安装,请按照以下命令操作
composer require phannaly/laravel-helpers
安装后,此库将自动加载。完成!您已经可以使用了。
可用方法
数组和对象
- array_add
- array_collapse
- array_divide
- array_dot
- array_except
- array_first
- array_flatten
- array_forget
- array_get
- array_has
- array_last
- array_only
- array_pluck
- array_prepend
- array_pull
- array_random
- array_set
- array_sort
- array_sort_recursive
- array_where
- array_wrap
- data_fill
- data_get
- data_set
- head
- last
字符串
- camel_case
- ends_with
- kebab_case
- preg_replace_array
- snake_case
- starts_with
- str_after
- str_before
- str_contains
- str_finish
- str_is
- str_limit
- str_random
- str_replace_array
- str_replace_first
- str_replace_last
- str_slug
- str_start
- studly_case
- title_case
数组和对象
array_add()
array_add
函数在数组中添加一个给定的键/值对,如果该键已存在于数组中则不添加
$array = array_add(['name' => 'Desk'], 'price', 100); // ['name' => 'Desk', 'price' => 100]
array_collapse()
array_collapse
函数将一个数组中的数组折叠成一个单一的数组
$array = array_collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
array_divide()
array_divide
函数返回两个数组,一个包含键,另一个包含值
[$keys, $values] = array_divide(['name' => 'Desk']); // $keys: ['name'] // $values: ['Desk']
array_dot()
array_dot
函数将多维数组展平成一个单层数组,使用 "点" 符号表示深度
$array = ['products' => ['desk' => ['price' => 100]]]; $flattened = array_dot($array); // ['products.desk.price' => 100]
array_except()
array_except
函数从数组中删除给定的键/值对
$array = ['name' => 'Desk', 'price' => 100]; $filtered = array_except($array, ['price']); // ['name' => 'Desk']
array_first()
array_first
函数返回通过给定真值测试的数组中的第一个元素
$array = [100, 200, 300]; $first = array_first($array, function ($value, $key) { return $value >= 150; }); // 200
方法也可以传递一个默认值作为第三个参数。如果没有值通过真值测试,将返回此值
$first = array_first($array, $callback, $default);
array_flatten()
array_flatten
函数将多维数组展平成一个单层数组
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]; $flattened = array_flatten($array); // ['Joe', 'PHP', 'Ruby']
array_forget()
array_forget
函数使用 "点" 符号从深度嵌套的数组中删除给定的键/值对
$array = ['products' => ['desk' => ['price' => 100]]]; array_forget($array, 'products.desk'); // ['products' => []]
array_get()
array_get
函数使用 "点" 符号从深度嵌套的数组中检索值
$array = ['products' => ['desk' => ['price' => 100]]]; $price = array_get($array, 'products.desk.price'); // 100
array_get
函数还可以接受一个默认值,如果未找到特定键,则返回此值
$discount = array_get($array, 'products.desk.discount', 0); // 0
array_has()
array_has
函数使用 "点" 符号检查给定的项目或项目是否存在于数组中
$array = ['product' => ['name' => 'Desk', 'price' => 100]]; $contains = array_has($array, 'product.name'); // true $contains = array_has($array, ['product.price', 'product.discount']); // false
array_last()
array_last
函数返回通过给定真值测试的数组中的最后一个元素
$array = [100, 200, 300, 110]; $last = array_last($array, function ($value, $key) { return $value >= 150; }); // 300
方法也可以传递一个默认值作为第三个参数。如果没有值通过真值测试,将返回此值
$last = array_last($array, $callback, $default);
array_only()
array_only
函数返回给定数组中指定的键/值对
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; $slice = array_only($array, ['name', 'price']); // ['name' => 'Desk', 'price' => 100]
array_pluck()
array_pluck
函数从数组中检索给定键的所有值
$array = [ ['developer' => ['id' => 1, 'name' => 'Taylor']], ['developer' => ['id' => 2, 'name' => 'Abigail']], ]; $names = array_pluck($array, 'developer.name'); // ['Taylor', 'Abigail']
您还可以指定结果列表的键索引方式
$names = array_pluck($array, 'developer.name', 'developer.id'); // [1 => 'Taylor', 2 => 'Abigail']
array_prepend()
array_prepend
函数将一个项推送到数组的开头
$array = ['one', 'two', 'three', 'four']; $array = array_prepend($array, 'zero'); // ['zero', 'one', 'two', 'three', 'four']
如果需要,您可以指定用于值的键
$array = ['price' => 100]; $array = array_prepend($array, 'Desk', 'name'); // ['name' => 'Desk', 'price' => 100]
array_pull()
array_pull
函数返回并从数组中移除一个键/值对
$array = ['name' => 'Desk', 'price' => 100]; $name = array_pull($array, 'name'); // $name: Desk // $array: ['price' => 100]
可以将默认值作为方法的第三个参数传递。如果键不存在,将返回此值
$value = array_pull($array, $key, $default);
array_random()
array_random
函数从数组中返回一个随机值
$array = [1, 2, 3, 4, 5]; $random = array_random($array); // 4 - (retrieved randomly)
您还可以指定作为可选第二个参数返回的项目数量。请注意,提供此参数将返回一个数组,即使只想返回一个项目
$items = array_random($array, 2); // [2, 5] - (retrieved randomly)
array_set()
array_set
函数使用点符号设置深层嵌套数组中的值
$array = ['products' => ['desk' => ['price' => 100]]]; array_set($array, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]]
array_sort()
array_sort 函数根据值对数组进行排序
$array = ['Desk', 'Table', 'Chair']; $sorted = array_sort($array); // ['Chair', 'Desk', 'Table']
您还可以根据给定的闭包对数组进行排序
$array = [ ['name' => 'Desk'], ['name' => 'Table'], ['name' => 'Chair'], ]; $sorted = array_values(array_sort($array, function ($value) { return $value['name']; })); /* [ ['name' => 'Chair'], ['name' => 'Desk'], ['name' => 'Table'], ] */
array_sort_recursive()
array_sort_recursive
函数通过使用 sort
函数对数字子数组进行递归排序,以及使用 ksort
对关联子数组进行排序
$array = [
['Roman', 'Taylor', 'Li'],
['PHP', 'Ruby', 'JavaScript'],
['one' => 1, 'two' => 2, 'three' => 3],
];
$sorted = array_sort_recursive($array);
/*
[
['JavaScript', 'PHP', 'Ruby'],
['one' => 1, 'three' => 3, 'two' => 2],
['Li', 'Roman', 'Taylor'],
]
*/
array_where()
array_where
函数使用给定的闭包过滤数组
$array = [100, '200', 300, '400', 500]; $filtered = array_where($array, function ($value, $key) { return is_string($value); }); // [1 => '200', 3 => '400']
array_wrap()
array_wrap
函数将给定的值包装在数组中。如果给定的值已经是数组,则不会更改
$string = 'Laravel'; $array = array_wrap($string); // ['Laravel']
如果给定的值为 null,则返回一个空数组
$nothing = null; $array = array_wrap($nothing); // []
data_fill()
data_fill
函数使用点符号设置嵌套数组或对象中的缺失值
$data = ['products' => ['desk' => ['price' => 100]]]; data_fill($data, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 100]]] data_fill($data, 'products.desk.discount', 10); // ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]
此函数还接受星号作为通配符,并将相应地填充目标
$data = [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2'], ], ]; data_fill($data, 'products.*.price', 200); /* [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2', 'price' => 200], ], ] */
data_get()
data_get
函数使用点符号从嵌套数组或对象中检索值
$data = ['products' => ['desk' => ['price' => 100]]]; $price = data_get($data, 'products.desk.price'); // 100
data_get
函数还接受默认值,如果指定的键找不到,则返回此值
$discount = data_get($data, 'products.desk.discount', 0); // 0
data_set()
data_set
函数使用点符号设置嵌套数组或对象中的值
$data = ['products' => ['desk' => ['price' => 100]]]; data_set($data, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]]
此函数还接受通配符,并将相应地设置目标值
$data = [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2', 'price' => 150], ], ]; data_set($data, 'products.*.price', 200); /* [ 'products' => [ ['name' => 'Desk 1', 'price' => 200], ['name' => 'Desk 2', 'price' => 200], ], ] */
默认情况下,任何现有值都将被覆盖。如果您只想在不存在时设置值,则可以将 false
作为第三个参数传递
$data = ['products' => ['desk' => ['price' => 100]]]; data_set($data, 'products.desk.price', 200, false); // ['products' => ['desk' => ['price' => 100]]]
head()
head
函数返回给定数组中的第一个元素
$array = [100, 200, 300]; $first = head($array); // 100
last()
last
函数返回给定数组中的最后一个元素
$array = [100, 200, 300]; $last = last($array); // 300
字符串
camel_case()
camel_case
函数将给定的字符串转换为 camelCase
$converted = camel_case('foo_bar'); // fooBar
ends_with()
ends_with
函数确定给定的字符串是否以给定的值结尾
$result = ends_with('This is my name', 'name'); // true
kebab_case()
kebab_case
函数将给定的字符串转换为 kebab-case
$converted = kebab_case('fooBar'); // foo-bar
preg_replace_array()
preg_replace_array
函数使用数组按顺序替换字符串中的给定模式
$string = 'The event will take place between :start and :end'; $replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string); // The event will take place between 8:30 and 9:00
snake_case()
snake_case
函数将给定的字符串转换为 snake_case
$converted = snake_case('fooBar'); // foo_bar
starts_with()
starts_with
函数确定给定的字符串是否以给定的值开头
$result = starts_with('This is my name', 'This'); // true
str_after()
str_after
函数返回字符串中给定值之后的所有内容
$slice = str_after('This is my name', 'This is'); // ' my name'
str_before()
str_before
函数返回字符串中给定值之前的所有内容
$slice = str_before('This is my name', 'my name'); // 'This is '
str_contains()
str_contains
函数确定给定的字符串是否包含给定的值(区分大小写)
$contains = str_contains('This is my name', 'my'); // true
您还可以传递一个值数组,以确定给定的字符串是否包含这些值中的任何一个
$contains = str_contains('This is my name', ['my', 'foo']); // true
str_finish()
str_finish
函数如果字符串不以给定值结束,则将给定值添加到字符串中
$adjusted = str_finish('this/string', '/'); // this/string/ $adjusted = str_finish('this/string/', '/'); // this/string/
str_is()
str_is
函数确定给定的字符串是否与给定的模式匹配。可以使用星号表示通配符
$matches = str_is('foo*', 'foobar'); // true $matches = str_is('baz*', 'foobar'); // false
str_limit()
str_limit
函数将给定的字符串截断到指定的长度
$truncated = str_limit('The quick brown fox jumps over the lazy dog', 20); // The quick brown fox...
您还可以传递第三个参数来更改将被附加到末尾的字符串
$truncated = str_limit('The quick brown fox jumps over the lazy dog', 20, ' (...)'); // The quick brown fox (...)
str_random()
str_random
函数生成指定长度的随机字符串。此函数使用 PHP 的 random_bytes
函数
$random = str_random(40);
str_replace_array()
str_replace_array
函数使用数组逐个替换字符串中的给定值
$string = 'The event will take place between ? and ?'; $replaced = str_replace_array('?', ['8:30', '9:00'], $string); // The event will take place between 8:30 and 9:00
str_replace_first()
str_replace_first
函数替换字符串中给定值的首次出现
$replaced = str_replace_first('the', 'a', 'the quick brown fox jumps over the lazy dog'); // a quick brown fox jumps over the lazy dog
str_replace_last()
str_replace_last
函数替换字符串中给定值的最后出现
$replaced = str_replace_last('the', 'a', 'the quick brown fox jumps over the lazy dog'); // the quick brown fox jumps over a lazy dog
str_slug()
str_slug
函数从给定字符串生成一个 URL 友好的 "slug"(短链接标识符)
$slug = str_slug('Laravel 5 Framework', '-'); // laravel-5-framework
str_start()
str_start
函数在给定值不存在于字符串开头时,向字符串中添加单个实例的给定值
$adjusted = str_start('this/string', '/'); // /this/string $adjusted = str_start('/this/string', '/'); // /this/string
studly_case()
studly_case
函数将给定字符串转换为 StudlyCase
格式
$converted = studly_case('foo_bar'); // FooBar
title_case()
title_case
函数将给定字符串转换为 Title Case
格式
$converted = title_case('a nice title uses the correct case'); // A Nice Title Uses The Correct Case
贡献
欢迎通过 PR 贡献。
有关详细信息,请参阅 行为准则。
许可证
此软件包在 MIT 许可证(MIT)下运行。有关详细信息,请参阅 许可证文件。