Pramda\Exception::assertCallable PHP Method

assertCallable() public static method

public static assertCallable ( callable $callable )
$callable callable
    public static function assertCallable($callable)
    {
        // Is it a callable?
        if (is_callable($callable)) {
            return;
        }
        // Is it an array that represents a callable?
        if (is_array($callable)) {
            if (method_exists($callable[0], $callable[1])) {
                return;
            }
        }
        // Is it a string that represents a callable
        if (is_string($callable)) {
            // of an object?
            if (strpos($callable, '::') !== FALSE) {
                $tmp = explode('::', $callable);
                self::assertCallable($tmp);
                return;
            } else {
                // or of a plain function?
                if (function_exists($callable)) {
                    return;
                }
            }
        }
        throw new \InvalidArgumentException("Argument is not a callable");
    }

Usage Example

Example #1
0
 /**
  * @return mixed|callable
  */
 public static function zipWith()
 {
     $args = func_get_args();
     /**
      * Like zip but applies the callable to each value before yielding
      *
      * @param callable        $callable
      * @param Generator|array $a
      * @param Generator|array $b
      *
      * @return \Generator
      * @throws Exception
      */
     $_zipWith = function ($callable, $a, $b) {
         Exception::assertCallable($callable);
         Exception::assertList($a);
         Exception::assertList($b);
         if (is_array($a)) {
             $a = new \ArrayIterator($a);
         }
         if (is_array($b)) {
             $b = new \ArrayIterator($b);
         }
         for ($a->rewind(), $b->rewind(); $a->valid() && $b->valid(); $a->next(), $b->next()) {
             (yield self::apply($callable, [$a->current(), $b->current()]));
         }
     };
     return call_user_func_array(self::curry3($_zipWith), $args);
 }