/** * @param array|Route $route * @param bool $compile * @param bool $recursive * @return array|Route */ protected function definition($route, $compile = true, $recursive = false) { $recursive && isset($route[Arg::CHILDREN]) && ($route[Arg::CHILDREN] = $this->children($route[Arg::CHILDREN], $compile, $recursive)); if (!isset($route[Arg::ROUTE])) { return isset($route[Arg::REGEX]) ? $route : Exception::invalidArgument('Route path not specified'); } !isset($route[Arg::TOKENS]) && ($route[Arg::TOKENS] = $this->tokens($route[Arg::ROUTE], isset($route[Arg::CONSTRAINTS]) ? $route[Arg::CONSTRAINTS] : [])); $compile && !isset($route[Arg::REGEX]) && ($route[Arg::REGEX] = $this->regex($route[Arg::TOKENS])); return $route; }
/** * */ function test_invalid_argument_exception() { try { Exception::invalidArgument('foo'); } catch (\Exception $exception) { } $this->assertEquals('foo', $exception->getMessage()); $this->assertEquals(__FILE__, $exception->getFile()); $this->assertEquals(22, $exception->getLine()); $this->assertInstanceOf(InvalidArgument::class, $exception); }
/** * @param array $tokens * @param array $params * @param array $defaults * @param callable $wildcard * @return string * @throws \InvalidArgumentException */ protected function compile(array $tokens, array $params, array $defaults = null, callable $wildcard = null) { $current = ['is_optional' => false, 'skip' => true, 'skippable' => false, 'path' => '']; $stack = []; foreach ($tokens as $part) { if ('literal' === $part[Dash::TYPE]) { $current['path'] .= $part[Dash::LITERAL]; continue; } if ('param' === $part[Dash::TYPE]) { $current['skippable'] = true; if (!$part[Dash::NAME]) { continue; } $default = isset($defaults[$part[Dash::NAME]]) ? $defaults[$part[Dash::NAME]] : null; $path = isset($params[$part[Dash::NAME]]) ? $params[$part[Dash::NAME]] : null; if (!$path) { if ($current['is_optional']) { continue; } !$default && Exception::invalidArgument(sprintf('Missing parameter "%s"', $part[Dash::NAME])); $path = $default; } (!$current['is_optional'] || !$default || $default !== $path) && ($current['skip'] = false); $current['path'] .= $path; unset($params[$part[Dash::NAME]]); continue; } if ('optional-start' === $part[Dash::TYPE]) { $stack[] = $current; $current = ['is_optional' => true, 'skip' => true, 'skippable' => false, 'path' => '']; continue; } if ('optional-end' === $part[Dash::TYPE]) { $parent = array_pop($stack); if ($current['path'] === '' || !$current['is_optional'] || !$current['skippable'] || !$current['skip']) { $parent['path'] .= $current['path']; $parent['skip'] = false; } $current = $parent; continue; } } return $wildcard && $params ? $wildcard(rtrim($current['path'], Arg::SEPARATOR), $params) : $current['path']; }
/** * */ function test_invalid_argument() { $this->setExpectedException(InvalidArgument::class, 'foo'); Exception::invalidArgument('foo'); }