/** * @param AbstractController $controller * @param string $method * @param array $args * * @throws \InvalidArgumentException * * @return array */ private function mapArguments(AbstractController $controller, string $method, array $args) : array { $return = []; $reflection = new \ReflectionClass($controller); if (!$reflection->hasMethod($method)) { throw new \InvalidArgumentException(sprintf("Method '%s' does not exist on '%s' for route '%s'", $method, get_class($controller), $this->currentRoute->getName())); } $method = $reflection->getMethod($method); foreach ($method->getParameters() as $parameter) { if (!isset($args[$parameter->getName()]) && $parameter->isOptional() === false) { throw new \InvalidArgumentException(sprintf("Missing mandatory arguments on controller definition '%s' on '%s'", $parameter->getName(), get_class($controller))); } $return[$parameter->getName()] = null; if (array_key_exists($parameter->getName(), $args) && $args[$parameter->getName()] !== '') { $value = $args[$parameter->getName()]; if ($parameter->getClass() && $parameter->getClass()->getName() == 'Cawa\\Date\\DateTime') { $value = new DateTime($value); } $return[$parameter->getName()] = $value; } elseif (!array_key_exists($parameter->getName(), $args)) { $return[$parameter->getName()] = $parameter->getDefaultValue(); } } return $return; }
/** * Test Empty Option */ public function testGetEmptyOption() { $route = new Route(); $this->assertNull($route->getOption(Route::OPTIONS_CACHE)); }
<?php /* @var $this \Cawa\Router\Router */ use Cawa\Core\App; use Cawa\Router\Route; return [Route::create()->setName("directResponse")->setMatch("/fr/direct")->setController(function (array $args = array()) { App::response()->setStatus(422); App::response()->addHeader("maman-sd78fsd5f4", "sdfsd"); return "Super"; }), Route::create()->setName("nolanguage")->setMatch("/")->setController("SampleApp\\Controller\\Index::redirect"), Route::create()->setName("index")->setMatch("/{{L}}")->setController("SampleApp\\Controller\\Index::method"), Route::create()->setName("indexTwig")->setMatch("/{{L}}/twig")->setController("SampleApp\\Controller\\Index::twig"), Route::create()->setResponseCode(404)->setController("SampleApp\\Controller\\Index::notFound")->setOption(Route::OPTIONS_CACHE, 60)];