PHP Classes

File: apply.php

Recommend this page to a friend!
  Classes of Nathaniel Sherwood   Encase PHP Functional Programming   apply.php   Download  
File: apply.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Encase PHP Functional Programming
Make functions work as if they are class functions
Author: By
Last change:
Date: 5 years ago
Size: 1,314 bytes
 

Contents

Class file image Download
<?php
namespace Encase\Functional;

/**
 * Invoke the `$func` function on `$subject` with the given arguments.
 *
 * Calls `$func($subject, ...$args)`, but will also clone $subject if it is an
 * object, so as to prevent $func from being able to mutate it. Returns the
 * result of the function call.
 *
 * Note that the argument list passed is limited to the number of REQUIRED
 * function parameters, so as to allow PHP internal functions to be used with
 * greater flexibility and ease. This can be overridden by wrapping the `$func`
 * argument in an `\Encase\Functional\Func`, in which case all arguments will
 * be passed.
 *
 * @param mixed $subject The subject of the function invokation.
 * @param callable $func The function to apply.
 * @param mixed ...$args One or more arguments to pass to the function.
 * @return mixed The result of the function call.
 */
function apply($subject, $func, ...$args)
{
   
assertType($func, 'callable', 'func');

    \
array_unshift($args, \is_object($subject) ? clone $subject : $subject);

    if (!
$func instanceof Func) {
       
$func = Func::make($func);

        if (
$func->isInternal() && !$func->isVariadic()) {
            if (
$nargs = $func->getNumberOfRequiredParameters()) {
               
$args = \array_slice($args, 0, $nargs);
            }
        }
    }

    return \
call_user_func_array($func, $args);
}