PHP code example of pinkcrab / function-constructors
1. Go to this page and download the library: Download pinkcrab/function-constructors library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pinkcrab / function-constructors example snippets
FunctionsLoader::
use PinkCrab\FunctionConstructors\Arrays as Arr;
use PinkCrab\FunctionConstructors\Numbers as Num;
use PinkCrab\FunctionConstructors\Strings as Str;
use PinkCrab\FunctionConstructors\Comparisons as C;
use PinkCrab\FunctionConstructors\GeneralFunctions as F;
use PinkCrab\FunctionConstructors\Objects as Obj;
// Allowing for
Arr\Map('esc_html') or Str\append('foo') or F\pipe($var, 'strtoupper', Str\append('foo'))
$data = [0,3,4,5,6,8,4,6,8,1,3,4];
// Remove all odd numbers, sort in an acceding order and double the value.
$newData = F\pipe(
$data,
Arr\filter(Num\isMultipleOf(2)), // Remove odd numbers
Arr\natsort(), // Sort the remaining values
Arr\map(Num\multiply(2)) // Double the values.
);
// Result
$newData = [
2 => 8,
6 => 8,
11 => 8,
4 => 12,
7 => 12,
5 => 16,
8 => 16,
];
$data = [
['details'=>['description' => ' This is some description ']],
['details'=>['description' => ' This is some other description ']],
];
$callback = F\compose(
F\pluckProperty('details','description'), // Plucks the description
'trim', // Remove all whitespace
Str\slice(0, 20), // Remove all but first 20 chars
'ucfirst', // Uppercase each word
Str\prepend('...') // End the string with ...
);
$results = array_map($callback, $data);
$results = [
'This Is Some Descrip...',
'This Is Some Other D...'
]
$data = [
['id' => 1, 'name' => 'James', 'timezone' => '+1', 'colour' => 'red'],
['id' => 2, 'name' => 'Sam', 'timezone' => '+1', 'colour' => 'red', 'special' => true],
['id' => 3, 'name' => 'Sarah', 'timezone' => '+2', 'colour' => 'green'],
['id' => 4, 'name' => 'Donna', 'timezone' => '+2', 'colour' => 'blue', 'special' => true],
];
// Filter all users with +2 timezone.
$zonePlus2 = array_filter($data, F\propertyEquals('timezone','+2'));
$results = [['id' => 3, ....],['id' => 4, ...]];
// Filter all user who have the special index.
$special = array_filter($data, F\hasProperty('special'));
$results = [['id' => 2, ....],['id' => 4, ...]];
// Get a list of all colours.
$colours = array_map(F\getProperty('colour'), $data);
$results = ['red', 'red', 'green', 'blue'];
// Set object property.
$object = new class(){ public $key = 'default'};
// Create a custom setter function.
$setKeyOfObject = F\setProperty($object, 'key');
$object = $setKeyOfObject('new value');
// {"key":"new value"}
// Can be used with arrays too
$array = ['key' => 'default'];
// Create a custom setter function.
$setKeyOfSArray = F\setProperty($array, 'key');
$array = $setKeyOfSArray('new value');
// [key => "new value"]
$appendFoo = Str\append('foo');
$result = $appendFoo('BAR');
$prependFoo = Str\prepend('foo');
$result = $prependFoo('BAR');
$replaceFooWithBar = Str\replaceWith('foo', 'bar');
$result = $replaceFooWithBar("its all a bit foo foo");
// "its all a bit bar bar"
$wrapStringWithBar = Str\wrap('bar-start-', '-bar-end');
$result = $wrapStringWithBar('foo');
// bar-start-foo-bar-end
// Check if a string contains
$containsFoo = Str\contains('foo');
$containsFoo('foo'); // true
$containsFoo('fobar'); // false
// Check if string start with (ends with also false
// Unlike using empty(), this checks if the value is a string also.
Str\isBlank(0); // false
Str\isBlank(null); // false
// Contains a regex pattern
$containsNumber = Str\containsPattern('~[0-9]+~');
$containsNumber('apple'); // false
$containsNumber('A12DFR3'); // true
// Create a mapper which doubles the value.
$doubleIt = Arr\map( Num\multiply(2) );
$doubleIt([1,2,3,4]); // [2,4,6,8]
// Create mapper to normalise array keys
$normaliseKeys = Arr\mapKey(F\compose(
'strval',
'trim',
Str\replace(' ', '-')
Str\prepend('__')
));
$normaliseKeys(1 => 'a', ' 2 ' => 'b', 'some key' => 'c');
// ['__1'=> 'a', '__2' => 'b', '__some-key' => 'c']
// Map and array with the value and key.
$mapWithKey = Arr\mapWithKey( function($key, $value) {
return $key . $value;
});
$mapWithKey('a' => 'pple', 'b' => 'anana');
// ['apple', 'banana']
// Filter out ony factors of 3
$factorsOf3s = Arr\filter( Num\factorOf(3) );
$factorsOf3s([1,3,5,6,8,7,9,11]); // [3,6,9]
// Filer first and last of an array/
$games = [
['id'=>1, 'result'=>'loss'],
['id'=>2, 'result'=>'loss'],
['id'=>3, 'result'=>'win'],
['id'=>4, 'result'=>'win'],
['id'=>5, 'result'=>'loss'],
];
$firstWin = Arr\filterFirst( F\propertyEquals('result','win') );
$result = $firstWin($games); // ['id'=>3, 'result'=>'win']
$lastLoss = Arr\filterLast( F\propertyEquals('result','loss') );
$result = $lastLoss($games); // ['id'=>5, 'result'=>'loss']
// Count result of filter.
$totalWins = Arr\filterCount( F\propertyEquals('result','win') );
$result = $totalWins($games); // 2
// Take the first or last items from an array
$first5 = Arr\take(5);
$last3 = Arr\takeLast(5);
$nums = [1,3,5,6,8,4,1,3,5,7,9,3,4];
$first5($nums); // [1,3,5,6,8]
$last3($nums); // [9,3,4]
// Using takeWhile and takeUntil to get the same result.
$games = [
['id'=>1, 'result'=>'loss'],
['id'=>2, 'result'=>'loss'],
['id'=>3, 'result'=>'win'],
['id'=>4, 'result'=>'win'],
['id'=>5, 'result'=>'loss'],
];
// All games while the result is a loss, then stop
$initialLoosingStreak = Arr\takeWhile(F\propertyEquals('result','loss'));
// All games until the first win, then stop
$untilFirstWin = Arr\takeUntil(F\propertyEquals('result', 'win'));
$result = $initialLoosingStreak($game);
$result = $untilFirstWin($game);
// [['id' => 1, 'result' => 'loss'], ['id' => 2, 'result' => 'loss']]