path = $path; $this->middleware = $middleware; $this->methods = is_array($methods) ? $this->validateHttpMethods($methods) : $methods; if (empty($name)) { $name = ($this->methods === self::HTTP_METHOD_ANY) ? $path : $path . '^' . implode(self::HTTP_METHOD_SEPARATOR, $this->methods); } $this->name = $name; $this->implicitHead = is_array($this->methods) && ! in_array(RequestMethod::METHOD_HEAD, $this->methods, true); $this->implicitOptions = is_array($this->methods) && ! in_array(RequestMethod::METHOD_OPTIONS, $this->methods, true); } /** * @return string */ public function getPath() { return $this->path; } /** * Set the route name. * * @param string $name */ public function setName($name) { $this->name = (string) $name; } /** * @return string */ public function getName() { return $this->name; } /** * @return string|callable|array */ public function getMiddleware() { return $this->middleware; } /** * @return int|string[] Returns HTTP_METHOD_ANY or array of allowed methods. */ public function getAllowedMethods() { return $this->methods; } /** * Indicate whether the specified method is allowed by the route. * * @param string $method HTTP method to test. * @return bool */ public function allowsMethod($method) { $method = strtoupper($method); if (RequestMethod::METHOD_HEAD === $method || RequestMethod::METHOD_OPTIONS === $method || $this->methods === self::HTTP_METHOD_ANY || in_array($method, $this->methods, true) ) { return true; } return false; } /** * @param array $options */ public function setOptions(array $options) { $this->options = $options; } /** * @return array */ public function getOptions() { return $this->options; } /** * Whether or not HEAD support is implicit (i.e., not explicitly specified) * * @return bool */ public function implicitHead() { return $this->implicitHead; } /** * Whether or not OPTIONS support is implicit (i.e., not explicitly specified) * * @return bool */ public function implicitOptions() { return $this->implicitOptions; } /** * Validate the provided HTTP method names. * * Validates, and then normalizes to upper case. * * @param string[] An array of HTTP method names. * @return string[] * @throws Exception\InvalidArgumentException for any invalid method names. */ private function validateHttpMethods(array $methods) { if (false === array_reduce($methods, function ($valid, $method) { if (false === $valid) { return false; } if (! is_string($method)) { return false; } if (! preg_match('/^[!#$%&\'*+.^_`\|~0-9a-z-]+$/i', $method)) { return false; } return $valid; }, true)) { throw new Exception\InvalidArgumentException('One or more HTTP methods were invalid'); } return array_map('strtoupper', $methods); } }