public function rethrow_exceptions() { $fixture = function ($val) { throw new MethodNotImplementedException('Test', __FUNCTION__); }; $as = function ($e) { return new IllegalStateException('Should not occur', $e); }; $closure = Closure::of($fixture)->wrapIn(Errors::rethrow($as)); $closure(null); }
/** * Returns a closure for map lookup * * @param php.ArrayAccess|[:var] $map * @return lang.functions.Closure * @throws lang.IllegalArgumentException */ public static function forMap($map) { if (func_num_args() > 1) { $default = func_get_arg(1); if ($map instanceof \ArrayAccess) { return Closure::of(function ($key) use($map, $default) { return isset($map[$key]) ? $map[$key] : $default; }); } else { if (is('[:var]', $map)) { return Closure::of(function ($key) use($map, $default) { return array_key_exists($key, $map) ? $map[$key] : $default; }); } } } else { if ($map instanceof \ArrayAccess) { return Closure::of(function ($key) use($map) { if (isset($map[$key])) { return $map[$key]; } throw new ElementNotFoundException('No element by key "' . $key . '"'); }); } else { if (is('[:var]', $map)) { return Closure::of(function ($key) use($map) { if (array_key_exists($key, $map)) { return $map[$key]; } throw new ElementNotFoundException('No element by key "' . $key . '"'); }); } } } throw new IllegalArgumentException('Expected either an array or an object overloading array access'); }
public function apply_instance() { $inCurly = new EnclosedIn('{', '}'); $inSquare = new EnclosedIn('[', ']'); $this->assertEquals('[{Test}]', Closure::of([$inCurly, 'layout'])->andThen([$inSquare, 'layout'])->apply('Test')); }