Location: PHPKode > projects > Artise > artise/environment/uri/pattern.class.php
<?php
namespace Env\Uri { class PatternParser { private $pattern = ''; public function __construct($pattern) { $this->pattern = $pattern; } private function nonSlashedCharPos($str, $char, $offset = 0) { if(preg_match('#(?<!\\\\)' . quotemeta($char) . '#', substr($str, $offset), $matches, PREG_OFFSET_CAPTURE)) { return $matches[0][1]; } return false; } public function parse() { $offset = 0; $parts = array(); $patternLen = strlen($this->pattern); while(true) { $openedParenthesis = $this->nonSlashedCharPos($this->pattern, '(', $offset); $closedParenthesis = $this->nonSlashedCharPos($this->pattern, ')', $offset); $readp = true; if($openedParenthesis === false && $closedParenthesis === false) { $openedParenthesis = $patternLen; $closedParenthesis = $patternLen; $readp = false; } elseif($openedParenthesis !== false && $closedParenthesis === false || $openedParenthesis === false && $closedParenthesis !== false) { return false; } $text = substr($this->pattern, $offset, $openedParenthesis - $offset); if(!empty($text)) { $parts[] = $this->meta2regexp($text); } if($readp) { ++$openedParenthesis; $contentp = substr($this->pattern, $openedParenthesis, $closedParenthesis - $openedParenthesis); $xor = false; if(substr($this->pattern, ++$closedParenthesis, 1) === '+') $xor = true; else --$closedParenthesis; $divided = preg_split('#(?<!\\\\)\\|#', $contentp); foreach($divided as &$value) $value = $this->meta2regexp($value); if(!$xor) $divided[] = ''; $parts[] = $divided; } $offset = $closedParenthesis + 1; if($offset >= $patternLen) { return $parts; } } } private function meta2regexp($str) { $str = preg_replace('#(?<!\\\\)%([a-zA-Z_]+)#', '(?<$1>.+?)', $str); return $str; } } } ?>
Return current item: Artise