]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/Net/URL/Mapper/Part.php
Net_URL_Mapper 0.9.1
[quix0rs-gnu-social.git] / extlib / Net / URL / Mapper / Part.php
1 <?php
2 /**
3  * URL parser and mapper
4  *
5  * PHP version 5
6  *
7  * LICENSE:
8  * 
9  * Copyright (c) 2006, Bertrand Mansion <golgote@mamasam.com>
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  *
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.
23  *
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.
35  *
36  * @category   Net
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: Part.php 232857 2007-03-28 10:23:04Z mansion $
41  * @link       http://pear.php.net/package/Net_URL_Mapper
42  */
43
44 abstract class Net_URL_Mapper_Part
45 {
46     protected $defaults;
47     protected $rule;
48     protected $public;
49     protected $type;
50     protected $required = false;
51     
52     /**
53     * Part name if dynamic or content, generated from path
54     * @var string
55     */
56     public $content;
57     
58     const DYNAMIC = 1;
59     const WILDCARD = 2;
60     const FIXED = 3;
61
62     public function __construct($content, $path)
63     {
64         $this->content = $content;
65         $this->path = $path;
66     }
67
68     public function setRule($rule)
69     {
70         $this->rule = $rule;
71     }
72
73     abstract public function getFormat();
74     
75     abstract public function getRule();
76
77     public function addSlash($str)
78     {
79         $str = trim($str, '/');
80         if (($pos = strpos($this->path, '/')) !== false) {
81             if ($pos == 0) {
82                 $str = '/'.$str;
83             } else {
84                 $str .= '/';
85             }
86         }
87         return $str;
88     }
89
90     public function addSlashRegex($str)
91     {
92         $str = trim($str, '/');
93         if (($pos = strpos($this->path, '/')) !== false) {
94             if ($pos == 0) {
95                 $str = '\/'.$str;
96             } else {
97                 $str .= '\/';
98             }
99         }
100         if (!$this->isRequired()) {
101             $str = '('.$str.'|)';
102         }
103         return $str;
104     }
105
106     public function setDefaults($defaults)
107     {
108         $this->defaults = (string)$defaults;
109     }
110
111     public function getType()
112     {
113         return $this->type;
114     }
115
116     public function accept($visitor, $method = null)
117     {
118         $args = func_get_args();
119         $visitor->$method($this, $args);
120     }
121
122     public function setRequired($required)
123     {
124         $this->required = $required;
125     }
126
127     public function isRequired()
128     {
129         return $this->required;
130     }
131
132     abstract public function generate($value = null);
133     
134     public function match($value)
135     {
136         $rule = $this->getRule();
137         return preg_match('/^'.$rule.'$/', $this->addSlash($value));
138     }
139
140 }
141
142 ?>