Funct
A PHP library with commonly used code blocks
Install / Use
/learn @phpfunct/FunctREADME

A PHP library with commonly used code blocks for faster development
Funct\firstValueNotEmpty($a, $b, $c)
- Requirements
- Installation
- Usage
- Library
- General
- Collection
- String
- between
- camelize
- chompLeft
- chompRight
- classify
- collapseWhitespace
- contains
- countOccurrences
- dasherize
- endsWith
- includes
- isAlpha
- isAlphaNumeric
- isLower
- isNumeric
- isUpper
- latinize
- left
- len
- length
- lines
- lowerCaseFirst
- pad
- padLeft
- padRight
- repeat
- reverse
- right
- slugify
- startsWith
- strip
- stripPunctuation
- swapCase
- times
- titleize
- toSentence
- toSentenceSerial
- toLower
- toUpper
- truncate
- underscore
- upperCaseFirst
- Invoke
- Object
- Testing
- Contributing
- License
Requirements
- PHP >= 5.5
Installation
Via Composer
$ composer require funct/funct
Usage
The 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');
General
arrayKeyNotExists($key, array $array)
Checks if the given key or index exists in the array
Funct\arrayKeyNotExists(2, [1, 2]); // => true
Funct\arrayKeyNotExists(1, [1, 2]); // => false
false($value)
Returns true if value is false
Funct\false(false); // => true
Funct\false(true); // => false
firstValue($valueA)
Returns 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'
firstValueNotEmpty($valueA, $valueB)
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'
ifSetOr($value, $default)
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)
notEmpty($value)
Returns true if value is not empty
Funct\notEmpty('fooBar'); // => true
Funct\notEmpty(''); // => false
notInArray($needle, $haystack, $strict = null)
Checks if needle is not in array
Funct\notInArray(3, [0, 1, 2]); // => true
Funct\notInArray(2, [0, 1, 2]); // => false
notNull($value)
Returns true if value is not null
Funct\notNull('fooBar'); // => true
Funct\notNull(null); // => false
null($value)
Returns true if value is null
Funct\null(null); // => true
Funct\null('fooBar'); // => false
tempFile($prefix = 'php')
Generates temp file on systems temp folder with prefix
Funct\tempFile('php'); // => /tmp/someFile.php
true($value)
Returns true if value is true
Funct\true(true); // => true
Funct\true(false); // => false
Collection
compact($collection)
Returns a copy of the array with all falsy values removed
Collection\compact([0, 1, false, 2, '', 3]); // => [1, 2, 3]
countBy($collection, $callback)
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]
every($collection, callable $callback = null)
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;
}
