PHP code example of apantle / fun-php

1. Go to this page and download the library: Download apantle/fun-php 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/ */

    

apantle / fun-php example snippets


$stringSquaredRoot = compose('strval', 'sqrt');

var_dump('4' === $stringSquaredRoot(16)); // bool(true) 


$getFoldersController = pipe(
    'validateHasUser',
    'validatePermissionOfUser',
    'getRootFolders'
);

function validateHasUser($request = null)
{
    if (!array_key_exists($request, 'user')) {
        throw new \LogicException('User parameter not received');
    }
    return $request;
}

function validatePermissionOfUser($request)
{
    if (intval($request['user']) !== 1) {
        throw new \DomainException('Only root user has permissions on this route');
    }
    return $request;
}

function getRootFolders($request) {
    return [ 'folder-' . $request['user'] ];
}

// $user key absent of request:
$getFoldersController(['path' => 'whatever']); // throws LogicException

// $user key present, non root user
$getFoldersController(['user' => 20]); // throws DomainException

// $user is root
$getFoldersController(['user' => 1]); // returns [ 'folder-1' ];



$simpleArrayReverse = curryToUnary('array_reverse');

var_dump([4,3,2,1] === $simpleArrayReverse([1,2,3,4]); // bool(true)

// custom position of argument to receive:
$map = curryToUnary('array_map', \Apantle\FunPHP\_, [1, 2, 3, 4]);

$result = $map(function ($num) { return $num * 10; });

var_dump([10, 20, 30, 40] === $result); // bool(true)


$request = [
    'store_id' => filter_input(
        INPUT_POST,
        'store',
        VALIDATE_FILTER_INT
    ) 
];

$scope = constant($request['store_id']);
$scope() // always return the value received in $_POST['store']


var_dump(1 === head[1,2,3,4]); // bool(true)


$input = 4;

var_dump(json_encode(unfold([
    'sqrt' => 'sqrt',
    'factorial' => 'gmp_fact'
])($input)));
// string(25) "{"sqrt":2,"factorial":24}"