3 * URL parser and mapper
9 * Copyright (c) 2006, Bertrand Mansion <golgote@mamasam.com>
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * * Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * * Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * * The names of the authors may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
32 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * @package Net_URL_Mapper
38 * @author Bertrand Mansion <golgote@mamasam.com>
39 * @license http://opensource.org/licenses/bsd-license.php New BSD License
40 * @version CVS: $Id: Path.php,v 1.1 2007/03/28 10:23:04 mansion Exp $
41 * @link http://pear.php.net/package/Net_URL_Mapper
44 require_once 'Net/URL.php';
45 require_once 'Net/URL/Mapper/Part/Dynamic.php';
46 require_once 'Net/URL/Mapper/Part/Wildcard.php';
47 require_once 'Net/URL/Mapper/Part/Fixed.php';
49 class Net_URL_Mapper_Path
60 protected $rules = array();
61 protected $defaults = array();
62 protected $parts = array();
67 protected $fixed = true;
70 public function __construct($path = '', $defaults = array(), $rules = array())
72 $this->path = '/'.trim(Net_URL::resolvePath($path), '/');
73 $this->setDefaults($defaults);
74 $this->setRules($rules);
78 } catch (Exception $e) {
79 // The path could not be parsed correctly, treat it as fixed
81 $part = self::createPart(Net_URL_Mapper_Part::FIXED, $this->path, $this->path);
82 $this->parts = array($part);
87 public function getPath()
92 protected function parsePath()
94 while ($this->yylex()) { }
99 * Path aliases can be used instead of full path
100 * @return null|string
102 public function getAlias()
109 * @param string Set the path name
112 public function setAlias($alias)
114 $this->alias = $alias;
119 * Get the path parts default values
122 public function getDefaults()
124 return $this->defaults;
128 * Set the path parts default values
129 * @param array Associative array with format partname => value
131 public function setDefaults($defaults)
133 if (is_array($defaults)) {
134 $this->defaults = $defaults;
136 $this->defaults = array();
141 * Set the path parts default values
142 * @param array Associative array with format partname => value
144 public function setRules($rules)
146 if (is_array($rules)) {
147 $this->rules = $rules;
149 $this->rules = array();
154 * Returns the regular expression used to match this path
155 * @return string PERL Regular expression
157 public function getRule()
159 if (is_null($this->rule)) {
161 foreach ($this->parts as $path => $part) {
162 $this->rule .= $part->getRule();
169 public function getFormat()
171 if (is_null($this->format)) {
172 $this->format = '/^';
173 foreach ($this->parts as $path => $part) {
174 $this->format .= $part->getFormat();
176 $this->format .= '$/';
178 return $this->format;
181 protected function addPart($part)
183 if (array_key_exists($part->content, $this->defaults)) {
184 $part->setRequired(false);
185 $part->setDefaults($this->defaults[$part->content]);
187 if (isset($this->rules[$part->content])) {
188 $part->setRule($this->rules[$part->content]);
191 if ($part->getType() != Net_URL_Mapper_Part::FIXED) {
192 $this->fixed = false;
193 $this->parts[$part->content] = $part;
195 $this->parts[] = $part;
200 public static function createPart($type, $content, $path)
203 case Net_URL_Mapper_Part::DYNAMIC:
204 return new Net_URL_Mapper_Part_Dynamic($content, $path);
206 case Net_URL_Mapper_Part::WILDCARD:
207 return new Net_URL_Mapper_Part_Wildcard($content, $path);
210 return new Net_URL_Mapper_Part_Fixed($content, $path);
215 * Checks whether the path contains the given part by name
216 * If value parameter is given, the part also checks if the
217 * given value conforms to the part rule.
218 * @param string Part name
219 * @param mixed The value to check against
221 public function hasKey($partName, $value = null)
223 if (array_key_exists($partName, $this->parts)) {
224 if (!is_null($value) && $value !== false) {
225 return $this->parts[$partName]->match($value);
229 } elseif (array_key_exists($partName, $this->defaults) &&
230 $value == $this->defaults[$partName]) {
236 public function generate($values = array(), $qstring = array(), $anchor = '')
239 foreach ($this->parts as $part) {
240 $path .= $part->generate($values);
242 $path = '/'.trim(Net_URL::resolvePath($path), '/');
243 if (!empty($qstring)) {
244 $path .= '?'.http_build_query($qstring);
246 if (!empty($anchor)) {
247 $path .= '#'.ltrim($anchor, '#');
252 public function getRequired()
254 if (!isset($this->required)) {
256 foreach ($this->parts as $part) {
257 if ($part->isRequired()) {
258 $req[] = $part->content;
261 $this->required = $req;
263 return $this->required;
266 public function getMaxKeys()
268 if (is_null($this->maxKeys)) {
269 $this->maxKeys = count($this->required);
270 $this->maxKeys += count($this->defaults);
272 return $this->maxKeys;
278 private $_yy_state = 1;
279 private $_yy_stack = array();
283 return $this->{'yylex' . $this->_yy_state}();
286 function yypushstate($state)
288 array_push($this->_yy_stack, $this->_yy_state);
289 $this->_yy_state = $state;
292 function yypopstate()
294 $this->_yy_state = array_pop($this->_yy_stack);
297 function yybegin($state)
299 $this->_yy_state = $state;
313 if ($this->N >= strlen($this->path)) {
314 return false; // end of input
316 $yy_global_pattern = "/^(\/?:\/?\\(([a-zA-Z0-9_]+)\\))|^(\/?\\*\/?\\(([a-zA-Z0-9_]+)\\))|^(\/?:([a-zA-Z0-9_]+))|^(\/?\\*([a-zA-Z0-9_]+))|^(\/?([^\/:*]+))/";
319 if (preg_match($yy_global_pattern, substr($this->path, $this->N), $yymatches)) {
320 $yysubmatches = $yymatches;
321 $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
322 if (!count($yymatches)) {
323 throw new Exception('Error: lexing failed because a rule matched' .
324 'an empty string. Input "' . substr($this->path,
325 $this->N, 5) . '... state START');
327 next($yymatches); // skip global match
328 $this->token = key($yymatches); // token number
329 if ($tokenMap[$this->token]) {
330 // extract sub-patterns for passing to lex function
331 $yysubmatches = array_slice($yysubmatches, $this->token + 1,
332 $tokenMap[$this->token]);
334 $yysubmatches = array();
336 $this->value = current($yymatches); // token value
337 $r = $this->{'yy_r1_' . $this->token}($yysubmatches);
339 $this->N += strlen($this->value);
340 $this->line += substr_count("\n", $this->value);
343 } elseif ($r === true) {
344 // we have changed state
345 // process this token in the new state
346 return $this->yylex();
347 } elseif ($r === false) {
348 $this->N += strlen($this->value);
349 $this->line += substr_count("\n", $this->value);
350 if ($this->N >= strlen($this->path)) {
351 return false; // end of input
355 } else { $yy_yymore_patterns = array(
356 1 => "^(\/?\\*\/?\\(([a-zA-Z0-9_]+)\\))|^(\/?:([a-zA-Z0-9_]+))|^(\/?\\*([a-zA-Z0-9_]+))|^(\/?([^\/:*]+))",
357 3 => "^(\/?:([a-zA-Z0-9_]+))|^(\/?\\*([a-zA-Z0-9_]+))|^(\/?([^\/:*]+))",
358 5 => "^(\/?\\*([a-zA-Z0-9_]+))|^(\/?([^\/:*]+))",
359 7 => "^(\/?([^\/:*]+))",
365 if (!strlen($yy_yymore_patterns[$this->token])) {
366 throw new Exception('cannot do yymore for the last token');
368 if (preg_match($yy_yymore_patterns[$this->token],
369 substr($this->path, $this->N), $yymatches)) {
370 $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
371 next($yymatches); // skip global match
372 $this->token = key($yymatches); // token number
373 $this->value = current($yymatches); // token value
374 $this->line = substr_count("\n", $this->value);
376 } while ($this->{'yy_r1_' . $this->token}() !== null);
378 $this->N += strlen($this->value);
379 $this->line += substr_count("\n", $this->value);
383 throw new Exception('Unexpected input at line' . $this->line .
384 ': ' . $this->path[$this->N]);
392 function yy_r1_1($yy_subpatterns)
395 $c = $yy_subpatterns[0];
396 $part = self::createPart(Net_URL_Mapper_Part::DYNAMIC, $c, $this->value);
397 $this->addPart($part);
399 function yy_r1_3($yy_subpatterns)
402 $c = $yy_subpatterns[0];
403 $part = self::createPart(Net_URL_Mapper_Part::WILDCARD, $c, $this->value);
404 $this->addPart($part);
406 function yy_r1_5($yy_subpatterns)
409 $c = $yy_subpatterns[0];
410 $part = self::createPart(Net_URL_Mapper_Part::DYNAMIC, $c, $this->value);
411 $this->addPart($part);
413 function yy_r1_7($yy_subpatterns)
416 $c = $yy_subpatterns[0];
417 $part = self::createPart(Net_URL_Mapper_Part::WILDCARD, $c, $this->value);
418 $this->addPart($part);
420 function yy_r1_9($yy_subpatterns)
423 $c = $yy_subpatterns[0];
424 $part = self::createPart(Net_URL_Mapper_Part::FIXED, $c, $this->value);
425 $this->addPart($part);