protected function removingRegisteredPattern($strict = false) { $name = sha1(time()); $this->pattern->register($name, '.*', $strict); $this->assertEquals('.*', $this->pattern->get($name, $strict)); $this->pattern->remove($name, $strict); $this->assertFalse($this->pattern->exist($name, $strict)); }
/** * Parsing url and returning array containing regex parts * * @param string $url * @return array * @throws \LogicException * @throws \InvalidArgumentException */ public function parse($url) { // cleanse url $urlParts = explode('/', trim($url, '/')); $returnParts = []; foreach ($urlParts as $urlPart) { $optionalFlag = false; $length = strlen($urlPart); $name = null; if (empty($urlPart)) { continue; } // check if current part has regex if ($urlPart[0] != '[') { $returnParts[] = $urlPart; continue; } if ($urlPart[$length - 1] == '?') { $optionalFlag = true; --$length; $urlPart = substr($urlPart, 0, -1); } $quantifier = $this->generateQuantifier($urlPart, $length); $parts = explode(':', trim($urlPart, '[]')); $name = $parts[0]; if (empty($name)) { throw new \InvalidArgumentException('Name not provided for ' . $urlPart . ' in ' . $url); } if (ctype_digit($name)) { throw new \InvalidArgumentException('Name should be alphanumeric!'); } if (isset($returnParts[$name])) { throw new \InvalidArgumentException('Duplicate pattern name in ' . $url); } unset($parts[0]); // checking if we have only parameter name in route path if (empty($parts)) { // we want all characters until first / $returnParts[$name] = $this->generatePattern('[^/.]', $quantifier, $optionalFlag); continue; } $string = implode(':', $parts); // lets parse content inside () // ie. if we have route like /test/[name:(foo|bar)] // then only possible route can be /test/foo and /test/bar if ($string != ($trimmed = trim($string, '()'))) { $returnParts[$name] = $this->generatePattern($trimmed, '', $optionalFlag); continue; } // lets parse content inside <> // so everything that is inside <> will be only copy/paste to regex without // modifications if ($string != ($trimmed = trim($string, '<>'))) { $returnParts[$name] = $this->generatePattern($trimmed, '', $optionalFlag); continue; } // checking if we need to build regex from patterns // also deciding is it strict or combined parameter if (!(strpos($string, '-') !== false || strpos($string, '|') !== false)) { if (!$this->pattern->exist($string) && !$this->pattern->exist($string, true)) { throw new \LogicException('Registered pattern with name ' . $string . ' does not exist'); } if ($this->pattern->exist($string, true)) { $returnParts[$name] = $this->generatePattern($this->pattern->get($string, true), '', $optionalFlag); } else { $returnParts[$name] = $this->generatePattern('[' . $this->pattern->get($string) . ']', $quantifier, $optionalFlag); } continue; } else { $returnParts[$name] = $this->generatePattern($this->generateCombinedPatterns($string, $quantifier), '', $optionalFlag); } } return $returnParts; }