一个包含常用代码块的 PHP 库
A PHP library with commonly used code blocks for faster development
Funct\firstValueNotEmpty($a, $b, $c)Via Composer
$ composer require funct/functThe library consist of five groups: Collection, Invoke, Object, Strings and General. Each group has it's own namespace suffix (Only General uses root namespace).
To include all group functions just include root namespace at the top of the file:
use Funct;For single group functions you have two options. One is to include root namespace and call directly with full namespace for e.g.:
use Funct;
Funct\Strings\classify('hello world');or to include only single group for e.g.:
use Funct\Strings;
Strings\classify('hello world');If you are using PHP >=5.6 you can include only single function. For e.g.:
use function Funct\Strings\classify;
classify('hello world');Checks if the given key or index exists in the array
Funct\arrayKeyNotExists(2, [1, 2]); // => true
Funct\arrayKeyNotExists(1, [1, 2]); // => falseReturns true if value is false
Funct\false(false); // => true
Funct\false(true); // => falseReturns a first non null value from function arguments
Funct\firstValue('foo_bar'); // => 'foo_bar'
Funct\firstValue(null, 'foo_bar'); // => 'foo_bar'
Funct\firstValue(null, null, 'foo_bar'); // => 'foo_bar'Returns a first not empty value from function arguments
Funct\firstValueNotEmpty('foo_bar'); // => 'foo_bar'
Funct\firstValueNotEmpty('', 'foo_bar'); // => 'foo_bar'
Funct\firstValueNotEmpty('', null, 'foo_bar'); // => 'foo_bar'Return the first param if isset or the second one or null if it doesn't
$bar = 'bar';
Funct\ifSetOr($foo); // => 'NULL'
Funct\ifSetOr($foo, 'foo_bar'); // => 'foo_bar'
Funct\ifSetOr($bar, 'foo_bar'); // => 'bar' ($bar value)Returns true if value is not empty
Funct\notEmpty('fooBar'); // => true
Funct\notEmpty(''); // => falseChecks if needle is not in array
Funct\notInArray(3, [0, 1, 2]); // => true
Funct\notInArray(2, [0, 1, 2]); // => falseReturns true if value is not null
Funct\notNull('fooBar'); // => true
Funct\notNull(null); // => falseReturns true if value is null
Funct\null(null); // => true
Funct\null('fooBar'); // => falseGenerates temp file on systems temp folder with prefix
Funct\tempFile('php'); // => /tmp/someFile.phpReturns true if value is true
Funct\true(true); // => true
Funct\true(false); // => falseReturns a copy of the array with all falsy values removed
Collection\compact([0, 1, false, 2, '', 3]); // => [1, 2, 3]Sorts a array into groups and returns a count for the number of objects in each group. Similar to groupBy, but instead of returning a array of values, returns a count for the number of values in that group
Collection\countBy(
[1, 2, 3, 4, 5],
function ($value) {
return $value % 2 == 0 ? 'even': 'odd';
}
); // => ['odd' => 3, 'even' => 2]
Collection\countBy(
[
['color' => 'red', 'title' => 'Foo'],
['color' => 'red', 'title' => 'Foo'],
['color' => 'red', 'title' => 'Foo'],
['color' => 'blue', 'title' => 'Bar'],
['color' => 'blue', 'title' => 'Bar']
],
'color'
); // => ['red' => 3, 'blue => 2]Returns true if all of the values in the array pass the callback truth test.
Collection\every([true, 1, null, 'yes']); // => false
Collection\every([true, 1, 'yes']); // => true
Collection\every(
[2, 4, 6],
function ($value) {
return ($value % 2) === 0;
}
); // => trueLooks through the array and returns the first value that matches all of the key-value pairs listed in properties.
…First value of collection
Collection\first([1, 2, 3]); // => 1Collection\firstN([1, 2, 3]); // => [1]
Collection\firstN([1, 2, 3], 2); // => [1, 2]Flattens a nested array by depth.
Collection\flatten(['a', ['b', ['c', ['d']]]]); // => ['a', 'b', ['c', ['d']]]
Collection\flatten(['a', ['b', ['c', ['d']]]], 2); // => ['a', 'b', 'c', ['d']]
Collection\flatten(['a', ['b', ['c', ['d']]]], 3); // => ['a', 'b', 'c', 'd']Flattens all arrays to single level
Collection\flattenAll(['a', ['b', ['c', ['d']]]]); // => ['a', 'b', 'c', 'd']Alias of invoke($collection, $callable)
Returns item from collection if exists otherwise null or default value
$collection = ['red' => []];
$collection['blue'] = Collection\get($collection, 'blue', []);
$collection['blue'][] = 'Hello World';
Collection\get($collection, 'red', ['empty']);Splits a collection into sets, grouped by the result of running each value through callback. If callback is a string
Collection\groupBy([1.3, 2.1, 2.4], function($num) { return floor($num); }); // => [1 => [1.3], 2 => [2.1, 2.4]]
Collection\groupBy(['one', 'two', 'three'], 'strlen'); // => [3 => ["one", "two"], 5 => ["three"]]Returns everything but the last entry of the array. Especially useful on the arguments object. Pass n to exclude the
Collection\initial([5, 4, 3, 2, 1]); // => [5, 4, 3, 2]
Collection\initial([5, 4, 3, 2, 1], 2); // => [5, 4, 3]Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each
Collection\intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]); // => [1, 2]Invokes callback on each value in the list. Any extra arguments passed will be forwarded on to the method invocation.
Collection\invoke(['a', 'b', 'c'], 'strtoupper'); // => ['A', 'B', 'C']Returns last element of array
Collection\last([1, 2, 3]); // => 3Returns the index of the last occurrence of value in the array, or false if value is not present
Collecton\lastIndexOf([1, 2, 3, 1, 2, 3], 2); // => 4Returns the last element of an array. Passing n will return the last n elements of the array.
Collection\lastN([1, 2, 3]); // => [3]
Collection\lastN([1, 2, 3], 2); // => [2, 3]暂无开放 Issues,或尚未同步最近议题。