me-io / php-lodash
一个全功能的PHP操作工具包,提供了对常用函数的支持。
Requires
- php: >=7.1
Requires (Dev)
- overtrue/phplint: ^1.1
- phpstan/phpstan: ^0.11
- phpunit/phpunit: ^7.5
- squizlabs/php_codesniffer: ^3.4
This package is not auto-updated.
Last update: 2024-09-15 04:31:35 UTC
README
PHP Lodash
php-lodash 是一个 PHP 工具库,类似于 Underscore/Lodash,它利用 `namespace`s 和动态自动加载来提高库的性能。
目录
要求
此库需要 php php_mbstring
扩展。要启用此扩展,请打开您的 php.ini
文件,找到行 extension=php_mbstring.dll
并取消注释它。如果该行不存在,则手动将其添加到 php.ini
。
extension=php_mbstring.dll
简介
lodash-php
是一个 PHP 工具库,类似于 Underscore/Lodash
,它利用 namespace
和动态自动加载来提高库的性能。
项目结构
__.php
是lodash-php
工具库的入口点- 所有 lodash-php 方法都存储在
/src/__
中各自的namespace
文件夹内的单独文件中 - 测试反映了库中定义的
namespace
,并使用 phpunit 测试 处理- 要运行测试,请运行以下命令
phpunit
- 要运行测试,请运行以下命令
. ├── images # Place relevant graphics in this folder ├── src # Core code of the application. │ ├── __.php # Entry point for the library │ └── Traits # Contains all lodash-php methods │ ├── Sequence\Chain.php # Methods related to chaining │ ├── Sequence\ChainWrapper.php # Methods related to chaining │ ├── Arrays.php # Methods related to arrays │ ├── Collections.php # Methods related to collections │ ├── Functions.php # Methods related to functions │ ├── Objects.php # Methods related to objects │ ├── Strings.php # Methods related to strings │ └── Utilities.php # Methods related to utilities ├── tests # Tests are placed in that folder. ├── composer.json # This file defines the project requirements ├── phpcs.xml.dist # Contains the configuration for PHPcs. ├── phpstan.neon.dist # Contains the configuration for PHPStan. ├── phpunit.xml.dist # Contains the configuration for PHPUnit. ├── LICENSE # License file for `lodash-php` └── README.md # Introduces our library to user.
注意: lodash-php 目前尚未与 Underscore/Lodash 具有相同的功能。有关更多信息,请参阅 贡献 部分。
安装
只需将 me-io/php-lodash
添加到您的项目 composer.json 文件中
{ "require": { "me-io/php-lodash": "^2" } }
然后运行 composer install。这将安装 me-io/php-lodash
及其所有依赖项。或者运行以下命令
composer require me-io/php-lodash
使用
数组
__::append(array $array = [], $value = null)
向数组添加项目
__::append([1, 2, 3], 4); # [1, 2, 3, 4]
__::compact(array $array = [])
返回一个移除所有假值后的数组副本。
__::compact([0, 1, false, 2, '', 3]); # [1, 2, 3]
__::flatten($array, $shallow = false)
扁平化多维数组。如果您传递 shallow,则数组将仅扁平化一个级别。
__::flatten([1, 2, [3, [4]]], [flatten]); # [1, 2, 3, 4]
__::patch($arr, $patches, $parent = '')
使用 xpath-value 对的列表修补数组。
__::patch(['addr' => ['country' => 'US', 'zip' => 12345]], ['/addr/country' => 'CA', '/addr/zip' => 54321]); # ['addr' => ['country' => 'CA', 'zip' => 54321]]
__::prepend(array $array = [], $value = null)
__::prepend([1, 2, 3], 4); # [4, 1, 2, 3]
__::range($start = null, $stop = null, $step = 1)
返回从 start 到 stop(不包括)的整数数组,步长为 step。
__::range(1, 10, 2); # [1, 3, 5, 7, 9]
__::repeat($object = '', $times = null)
返回一个长度为 $n
的数组,每个索引都包含提供的值。
__::repeat('foo', 3); # ['foo', 'foo', 'foo']
__::chunk(array $array, $size = 1, $preserveKeys = false)
将数组分割成块
__::chunk([1, 2, 3, 4, 5], 3); # [[1, 2, 3], [4, 5]]
__::drop(array $input, $number = 1)
创建一个包含从开始处删除 n 个元素的数组切片。
__::drop([0, 1, 3], 2); # [3]
__::randomize(array $array)
打乱数组,确保没有项目保持在同一位置。
__::randomize([1, 2, 3]); # [2, 3, 1]
__::search($array, $value)
在数组中搜索值的索引。
__::search(['a', 'b', 'c'], 'b'); # 1
__::average($array, $decimals)
返回数组的平均值。
__::average([1, 2, 3]); # 2
__::size($array)
获取数组的尺寸。
__::size([1, 2, 3]); # 3
__::contains($array, $value)
检查项目是否在数组中。
__::contains(['a', 'b', 'c'], 'b'); # true
__::clean(array $array)
从数组中清除所有假值。
__::clean([true, false, 0, 1, 'string', '']); # [true, 1, 'string']
__::random(array $array, $take = null)
从数组中获取一个随机字符串。
__::random([1, 2, 3]); # Returns 1, 2 or 3
__::intersection(array $a, array $b)
返回包含在两个输入数组中所有元素的数组。
__::intersection(["green", "red", "blue"], ["green", "yellow", "red"]); # ["green", "red"]
__::intersects(array $a, array $b)
返回一个布尔标志,表示两个输入数组是否有任何公共元素。
__::intersects(["green", "red", "blue"], ["green", "yellow", "red"]) # true
__::initial(array $array, int $to = 1)
从数组中排除最后 X 个元素
__::initial([1, 2, 3], 1); # [1, 2]
__::rest(array $array, int $from = 1)
从数组中排除第一个 X 个元素
__::rest([1, 2, 3], 2); # [3]
__::sortKeys(array $array, string $direction = 'ASC')
按键排序数组。
__::sortKeys(['z' => 0, 'b' => 1, 'r' => 2]); # ['b' => 1, 'r' => 2, 'z' => 0] __::sortKeys(['z' => 0, 'b' => 1, 'r' => 2], 'desc'); # ['z' => 0, 'r' => 2, 'b' => 1]
____::without(array $array, $remove, $preserveKeys = false)
从数组中移除不需要的值
返回不保留键的新数组。
__::without([1, 1 => 3, 2 => 4, 5], 4) # [0 => 1, 1 => 3, 2 => 5]
返回保留键的新数组。
__::without([1, 3 => 3, 2 => 4, 5], 4, true) # [0 => 1, 3 => 3, 4 => 5]
链式调用
__::chain($initialValue)
返回一个包装实例,允许值通过多个 php-lodash 函数传递
__::chain([0, 1, 2, 3, null]) ->compact() ->prepend(4) ->value(); # [4, 1, 2, 3]
集合
__::filter($array, callback($n))
返回通过真值测试的集合中的值。
$a = [ ['name' => 'fred', 'age' => 32], ['name' => 'maciej', 'age' => 16] ]; __::filter($a, function($n) { return $n['age'] > 24; }); # [['name' => 'fred', 'age' => 32]]
__::first($array, [$n])
获取数组的第一个元素。传递 n 返回前 n 个元素。
__::first([1, 2, 3, 4, 5], 2); # [1, 2]
__::get($array, JSON $string)
通过索引获取数组项,接受嵌套索引
__::get(['foo' => ['bar' => 'ter']], 'foo.bar'); # 'ter'
__::last($array, [$n])
获取数组的最后一个元素。传递 n 返回最后 n 个元素。
__::last([1, 2, 3, 4, 5], 2); # [4, 5]
__::map($array, callback($n))
返回通过映射每个在集合中通过迭代器迭代值的新数组。
__::map([1, 2, 3], function($n) { return $n * 3; }); # [3, 6, 9]
__::max($array)
从集合中返回最大值。如果传递迭代器,max 将返回迭代器返回的最大值。
__::max([1, 2, 3]); # 3
__::min($array)
从集合中返回最小值。如果传递迭代器,min 将返回迭代器返回的最小值。
__::min([1, 2, 3]); # 1
__::pluck($array, $property)
返回属于集合中每个项目给定属性的值数组。
$a = [ ['foo' => 'bar', 'bis' => 'ter' ], ['foo' => 'bar2', 'bis' => 'ter2'], ]; __::pluck($a, 'foo'); # ['bar', 'bar2']
__::where($array, $parameters[])
返回与给定参数数组匹配的对象集合。
$a = [ ['name' => 'fred', 'age' => 32], ['name' => 'maciej', 'age' => 16] ]; __::where($a, ['age' => 16]); # [['name' => 'maciej', 'age' => 16]]
__::assign($collection1, $collection2)
合并和合并提供的每个其他集合。
$a = [ 'color' => [ 'favorite' => 'red', 5 ], 3 ]; $b = [ 10, 'color' => [ 'favorite' => 'green', 'blue' ] ]; __::assign($a, $b); # ['color' => ['favorite' => 'green', 'blue'], 10]
__::reduceRight($collection, \Closure $iteratee, $accumulator = null)
将 $collection 减少到值,该值是 $accumulator 运行 $collection 中的每个元素(从右到左)通过 $iteratee 的结果,其中每个后续调用都提供前一个调用的返回值。
__::reduceRight(['a', 'b', 'c'], function ($word, $char) { return $word . $char; }, ''); # 'cba'
__::doForEachRight($collection, \Closure $iteratee)
从右到左遍历集合中的元素,并对每个元素调用迭代。
__::doForEachRight([1, 2, 3], function ($value) { print_r($value) }); # (Side effect: print 3, 2, 1)
__::doForEach($collection, \Closure $iteratee)
遍历集合中的元素,并对每个元素调用迭代。
__::doForEach([1, 2, 3], function ($value) { print_r($value) }); # (Side effect: print 1, 2, 3)
__::set($collection, $path, $value = null)
返回一个新集合,在索引处设置项目为给定值。索引可以是嵌套索引的路径。
__::set(['foo' => ['bar' => 'ter']], 'foo.baz.ber', 'fer'); # '['foo' => ['bar' => 'ter', 'baz' => ['ber' => 'fer']]]'
__::hasKeys($collection, $path, $value = null)
返回 $input 是否包含所有请求的 $keys。如果 $strict 为 true,它还检查 $input 是否仅包含给定的 $keys。
__::hasKeys(['foo' => 'bar', 'foz' => 'baz'], ['foo', 'foz']); # true
__::has($collection, $path)
如果 $collection 包含请求的 $key,则返回 true。
__::has(['foo' => ['bar' => 'num'], 'foz' => 'baz'], 'foo.bar'); # true __::hasKeys((object) ['foo' => 'bar', 'foz' => 'baz'], 'bar'); # false
__::concat($collection1, $collection2)
合并和连接提供的每个其他集合。
__::concat(['color' => ['favorite' => 'red', 5], 3], [10, 'color' => ['favorite' => 'green', 'blue']]); # ['color' => ['favorite' => ['green'], 5, 'blue'], 3, 10]
__::concatDeep($collection1, $collection2)
递归地合并和连接提供的每个其他集合。
__::concatDeep(['color' => ['favorite' => 'red', 5], 3], [10, 'color' => ['favorite' => 'green', 'blue']]); # ['color' => ['favorite' => ['red', 'green'], 5, 'blue'], 3, 10]
__::ease(array $collection, $glue = '.')
通过映射每个末尾叶值到由所有先前索引组成的关键字来简化复杂集合。
__::ease(['foo' => ['bar' => 'ter'], 'baz' => ['b', 'z']]); # '['foo.bar' => 'ter', 'baz.0' => 'b', , 'baz.1' => 'z']'
__::every($collection, \Closure $iteratee)
检查谓词是否为集合中所有元素返回真值。
__::every([1, 3, 4], function ($v) { return is_int($v); }); // → true
__::groupBy(array $array, $key)
返回一个关联数组,其键是 $key 的值。
__::groupBy([ ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'], ['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'], ['state' => 'CA', 'city' => 'Mountain View', 'object' => 'Space pen'], ], 'state' ); # [ # 'IN' => [ # ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'], # ['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'], # ], # 'CA' => [ # ['state' => 'CA', 'city' => 'Mountain View', 'object' => 'Space pen'] # ] # ] __::groupBy([ ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'], ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'Manhole'], ['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'], ], function ($value) { return $value->city; } ); # [ # 'Indianapolis' => [ # ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'], # ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'Manhole'], # ], # 'San Diego' => [ # ['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'], # ] # ]
__::isEmpty($value)
检查值是否为空数组或对象。
__::isEmpty([]); # true
__::merge($collection1, $collection2)
递归地合并和合并提供的每个其他集合。
__::merge(['color' => ['favorite' => 'red', 'model' => 3, 5], 3], [10, 'color' => ['favorite' => 'green', 'blue']]); # ['color' => ['favorite' => 'green', 'model' => 3, 'blue'], 10]
__::pick($collection = [], array $paths = [], $default = null)
返回一个仅包含给定路径列表中的键的数组。
__::pick(['a' => 1, 'b' => ['c' => 3, 'd' => 4]], ['a', 'b.d']); # ['a' => 1, 'b' => ['d' => 4]]
__::reduce($collection, \Closure $iteratee, $accumulator = null)
将集合 $collection 简化为一个值,该值是通过对 $collection 中的每个元素运行 $iteratee 得到的累加器结果,其中每个后续调用都提供前一个调用的返回值。
__::reduce([1, 2], function ($sum, $number) { return $sum + $number; }, 0); # 3 $a = [ ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'], ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'Manhole'], ['state' => 'IN', 'city' => 'Plainfield', 'object' => 'Basketball'], ['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'], ['state' => 'CA', 'city' => 'Mountain View', 'object' => 'Space pen'], ]; $iteratee = function ($accumulator, $value) { if (isset($accumulator[$value['city']])) $accumulator[$value['city']]++; else $accumulator[$value['city']] = 1; return $accumulator; }; __::reduce($c, $iteratee, []); # [ # 'Indianapolis' => 2, # 'Plainfield' => 1, # 'San Diego' => 1, # 'Mountain View' => 1, # ] $object = new \stdClass(); $object->a = 1; $object->b = 2; $object->c = 1; __::reduce($object, function ($result, $value, $key) { if (!isset($result[$value])) $result[$value] = []; $result[$value][] = $key; return $result; }, []) # [ # '1' => ['a', 'c'], # '2' => ['b'] # ]
__::unease(array $collection, $separator = '.')
使用键作为指示器从哈希表构建多维集合。
__::unease(['foo.bar' => 'ter', 'baz.0' => 'b', , 'baz.1' => 'z']); # '['foo' => ['bar' => 'ter'], 'baz' => ['b', 'z']]'
字符串
__::split($input, $delimiter, $limit = PHP_INT_MAX)
通过字符串分割。
__::split('a-b-c', '-', 2); # ['a', 'b-c']
__::camelCase($input)
将字符串转换为驼峰式。
__::camelCase('Foo Bar'); # 'fooBar'
__::capitalize($input)
将字符串的第一个字符转换为大写,其余字符转换为小写。
__::capitalize('FRED'); # 'Fred'
__::kebabCase($input)
将字符串转换为中划线命名。
__::kebabCase('Foo Bar'); # 'foo-bar'
__::lowerFirst($input)
将字符串的第一个字符转换为小写,类似于 lcfirst。
__::lowerFirst('Fred'); # 'fred'
__::snakeCase($input)
将字符串转换为蛇形命名。
__::snakeCase('Foo Bar'); # 'foo_bar'
__::startCase($input)
将字符串转换为首字母大写。
__::startCase('--foo-bar--'); # 'Foo Bar'
__::toLower($input)
将字符串整体转换为小写,类似于 strtolower。
__::toLower('fooBar'); # 'foobar'
__::toUpper($input)
将字符串整体转换为大写,类似于 strtoupper。
__::toUpper('fooBar'); # 'FOOBAR'
__::upperCase($input)
将字符串(空格分隔的单词)转换为大写。
__::upperCase('--foo-bar'); # 'FOO BAR'
__::upperFirst($input)
将字符串的第一个字符转换为大写,类似于 ucfirst。
__::upperFirst('fred'); # 'Fred'
__::words($input, $pattern = null)
将字符串分割成单词数组。
__::words('fred, barney, & pebbles'); # ['fred', 'barney', 'pebbles'] __::words('fred, barney, & pebbles', '/[^, ]+/'); # ['fred', 'barney', '&', 'pebbles']
__::lowerCase($input)
将字符串(空格分隔的单词)转换为小写。
__::lowerCase('--Foo-Bar--'); # 'foo bar'
函数
__::slug($string, [array $options])
__::slug('Jakieś zdanie z dużą ilością obcych znaków!'); # 'jakies-zdanie-z-duza-iloscia-obcych-znakow' $options = [ 'delimiter' => '-', 'limit' => 30, 'lowercase' => true, 'replacements' => array(), 'transliterate' => true ] __::slug('Something you don\'t know about know about Jackson', $options); # 'something-you-dont-know-about'
__::truncate($string, [$limit=40])
基于单词计数截断字符串。
$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque et mi orci.'; __::truncate($string); # 'Lorem ipsum dolor sit amet, consectetur...' __::truncate($string, 60); # 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pel...'
__::urlify($string)
在字符串中查找网址并将它们放入锚标签中。
__::urlify('I love https://google.com'); # 'I love <a href="https://google.com">google.com</a>'
对象
__::isArray($array)
检查给定的值是否为数组。
__::isArray([1, 2, 3]); # true __::isArray(123); # false
__::isFunction($string)
检查给定的值是否为函数。
__::isFunction(function ($a) { return $a + 2; }); # true
__::isNull($null)
检查给定的值是否为 null。
__::isNull(null); # true
__::isNumber($int|$float)
检查给定的值是否为数字。
__::isNumber(123); # true
__::isObject($object)
检查给定的值是否为对象。
__::isObject('fred'); # false
__::isString($string)
检查给定的值是否为字符串。
__::isString('fred'); # true
实用工具
__::isEmail($string)
检查值是否为有效的电子邮件。
__::isEmail('test@test.com'); # true __::isEmail('test_test.com'); # false
__::now()
包装了 time()
函数,返回自 Unix Epoch 以来当前偏移的秒数。
__::now(); # 1417546029
__::stringContains($needle, $haystack, [$offset])
包装了 time()
函数,返回自 Unix Epoch 以来当前偏移的秒数。
__::stringContains('waffle', 'wafflecone'); # true
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
测试
$ composer test
贡献
请随时为此项目做出贡献!欢迎拉取请求和功能请求! ✌️
请参阅 CONTRIBUTING 和 CODE_OF_CONDUCT 了解详细信息。
贡献者
向所有贡献者致以衷心的感谢
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。