tajawal/lodash-php

一个全面的PHP操作工具包,提供对常用功能的支持。

维护者

详细信息

github.com/tajawal/lodash-php

1.0.10 2018-01-24 12:03 UTC

README

lodash-php logo

lodash-php

Build Status PHP version

目录

需求

; php.ini
extension=php_mbstring.dll

简介

lodash-php 是一个PHP工具库,类似于 Underscore/Lodash,它利用 namespace 和动态自动加载来提高库性能。

项目结构

  • lodash.php 是 lodash-php 工具库的入口点
  • 所有 lodash-php 方法都存储在 /src/__ 中各自 namespace 文件夹内的单独文件中
  • 测试反映了库中定义的 namespace,并使用 phpunit 测试 处理
    • 要测试 lodash-php,请安装 phpunit,然后在终端中运行 phpunit
/lodash-php
├── /images
│   └── (place relevant graphics in this folder)
├── /src
│   └── /__
│       ├── /arrays
│       ├── /collections
│       ├── /functions
│       ├── /objects
│       ├── /utilities
│       └── load.php        # (autoloader script for all lodash-php methods)
├── /tests
│   ├── arrays.php
│   ├── chaining.php
│   ├── collections.php
│   ├── functions.php
│   ├── objects.php
│   └── utilities.php
├── .gitignore
├── .travis.yaml
├── lodash.php
├── composer.json
├── phpunit.xml
├── LICENSE
└── README.md

注意: lodash-php 目前与 Underscore/Lodash 不完全兼容。有关更多信息,请参阅贡献部分。

基准测试

screenshot-2.png

安装

按照以下方法描述安装 lodash-php

通过 Composer 和 Packagist

包仓库

将 require 语句放入您的 composer.json 文件中,然后运行 composer install

{
    "require": {
        ...
        "tajawal/lodash-php": "*"
        ...
    }
}

通过文件包含

将 require 语句放入您的代码中

require 'lodash-php/lodash.php';

用法

数组

__::append()
__::append([1, 2, 3], 4);
// >> [1, 2, 3, 4]
__::compact()

返回一个移除了假值的数组副本。

__::compact([0, 1, false, 2, '', 3]);
// >> [1, 2, 3]
__::flatten()

扁平化多维数组。如果您传递 shallow,则数组将只扁平化一层。

__::flatten([1, 2, [3, [4]]], [flatten]);
// >> [1, 2, 3, 4]
__::patch()

使用 xpath-value 对的列表修补数组。

__::patch(['addr' => ['country' => 'US', 'zip' => 12345]], ['/addr/country' => 'CA', '/addr/zip' => 54321]);
// >> ['addr' => ['country' => 'CA', 'zip' => 54321]]
__::prepend()
__::prepend([1, 2, 3], 4);
// >> [4, 1, 2, 3]
__::range()

返回从起始值到结束值(不包括结束值)的整数数组,步长为步长值。

__::range(1, 10, 2);
// >> [1, 3, 5, 7, 9]
__::repeat($val, $n)

返回一个长度为 $n 的数组,每个索引包含提供的值。

__::repeat('foo', 3);
// >> ['foo', 'foo', 'foo']

链式调用

即将推出...

集合

__::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]]

函数

__::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)
__::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 纪元以来的当前偏移量(以秒为单位)。

__::now();
// >> 1417546029
__::stringContains($needle, $haystack, [$offset])

包装了 time() 函数,返回自 Unix 纪元以来的当前偏移量(以秒为单位)。

__::stringContains('waffle', 'wafflecone');
// >> true

贡献

请随意为此项目做出贡献!欢迎提交拉取请求和功能请求!✌️

许可

MIT