]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/Net/URL/Mapper/Path.plex
two useful functions for profiling
[quix0rs-gnu-social.git] / extlib / Net / URL / Mapper / Path.plex
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: Path.plex 283937 2009-07-12 11:37:21Z mansion $
41  * @link       http://pear.php.net/package/Net_URL_Mapper
42  */
43
44 require_once 'Net/URL/Mapper/Part/Dynamic.php';
45 require_once 'Net/URL/Mapper/Part/Wildcard.php';
46 require_once 'Net/URL/Mapper/Part/Fixed.php';
47
48 class Net_URL_Mapper_Path
49 {
50     private $path = '';
51     private $N = 0;
52     public $token;
53     public $value;
54     private $line = 1;
55     private $state = 1;
56
57
58     protected $alias;
59     protected $rules = array();
60     protected $defaults = array();
61     protected $parts = array();
62     protected $rule;
63     protected $format;
64     protected $minKeys;
65     protected $maxKeys;
66     protected $fixed = true;
67     protected $required;
68
69     public function __construct($path = '', $defaults = array(), $rules = array())
70     {
71         $this->path = '/'.trim(Net_URL::resolvePath($path), '/');
72         $this->setDefaults($defaults);
73         $this->setRules($rules);
74
75         try {
76             $this->parsePath();
77         } catch (Exception $e) {
78             // The path could not be parsed correctly, treat it as fixed
79             $this->fixed = true;
80             $part = self::createPart(Net_URL_Mapper_Part::FIXED, $this->path, $this->path);
81             $this->parts = array($part);
82         }
83         $this->getRequired();
84     }
85
86     /**
87     * Called when the object is serialized
88     * Make sure we do not store too much info when the object is serialized
89     * and call the regular expressions generator functions so that they will
90     * not need to be generated again on wakeup.
91     *
92     * @return   array   Name of properties to store when serialized
93     */
94     public function __sleep()
95     {
96         $this->getFormat();
97         $this->getRule();
98         return array('alias', 'path', 'defaults', 'rule', 'format',
99             'parts', 'minKeys', 'maxKeys', 'fixed', 'required');
100     }
101
102     public function getPath()
103     {
104         return $this->path;
105     }
106
107     protected function parsePath()
108     {
109         while ($this->yylex()) { }
110     }
111
112     /**
113     * Get the path alias
114     * Path aliases can be used instead of full path
115     * @return null|string
116     */
117     public function getAlias()
118     {
119         return $this->alias;
120     }
121
122     /**
123     * Set the path name
124     * @param string Set the path name
125     * @see getAlias()
126     */
127     public function setAlias($alias)
128     {
129         $this->alias = $alias;
130         return $this;
131     }
132
133     /**
134     * Get the path parts default values
135     * @return null|array
136     */
137     public function getDefaults()
138     {
139         return $this->defaults;
140     }
141
142     /**
143     * Set the path parts default values
144     * @param array  Associative array with format partname => value
145     */    
146     public function setDefaults($defaults)
147     {
148         if (is_array($defaults)) {
149             $this->defaults = $defaults;
150         } else {
151             $this->defaults = array();
152         }
153     }
154
155     /**
156     * Set the path parts default values
157     * @param array  Associative array with format partname => value
158     */    
159     public function setRules($rules)
160     {
161         if (is_array($rules)) {
162             $this->rules = $rules;   
163         } else {
164             $this->rules = array();
165         }
166     }
167
168     /**
169     * Returns the regular expression used to match this path
170     * @return string  PERL Regular expression
171     */ 
172     public function getRule()
173     {
174         if (is_null($this->rule)) {
175             $this->rule = '/^';
176             foreach ($this->parts as $path => $part) {
177                 $this->rule .= $part->getRule();
178             }
179             $this->rule .= '$/';
180         }
181         return $this->rule;
182     }
183
184     public function getFormat()
185     {
186         if (is_null($this->format)) {
187             $this->format = '/^';
188             foreach ($this->parts as $path => $part) {
189                 $this->format .= $part->getFormat();
190             }
191             $this->format .= '$/';
192         }
193         return $this->format;
194     }
195
196     protected function addPart($part)
197     {
198         if (array_key_exists($part->content, $this->defaults)) {
199             $part->setRequired(false);
200             $part->setDefaults($this->defaults[$part->content]);
201         }
202         if (isset($this->rules[$part->content])) {
203             $part->setRule($this->rules[$part->content]);
204         }
205         $this->rule = null;
206         if ($part->getType() != Net_URL_Mapper_Part::FIXED) {
207             $this->fixed = false;
208             $this->parts[$part->content] = $part;
209         } else {
210             $this->parts[] = $part;
211         }
212         return $part;
213     }
214
215     public static function createPart($type, $content, $path)
216     {
217         switch ($type) {
218             case Net_URL_Mapper_Part::DYNAMIC:
219                 return new Net_URL_Mapper_Part_Dynamic($content, $path);
220                 break;
221             case Net_URL_Mapper_Part::WILDCARD:
222                 return new Net_URL_Mapper_Part_Wildcard($content, $path);
223                 break;
224             default:
225                 return new Net_URL_Mapper_Part_Fixed($content, $path);
226         }
227     }
228
229     /**
230     * Checks whether the path contains the given part by name
231     * If value parameter is given, the part also checks if the 
232     * given value conforms to the part rule.
233     * @param string Part name
234     * @param mixed  The value to check against 
235     */
236     public function hasKey($partName, $value = null)
237     {
238         if (array_key_exists($partName, $this->parts)) {
239             if (!is_null($value) && $value !== false) {
240                 return $this->parts[$partName]->match($value);
241             } else {
242                 return true;
243             }
244         } elseif (array_key_exists($partName, $this->defaults) &&
245             $value == $this->defaults[$partName]) {
246             return true;
247         }
248         return false;
249     }
250
251     public function generate($values = array(), $qstring = array(), $anchor = '')
252     {
253         $path = '';
254         foreach ($this->parts as $part) {
255             $path .= $part->generate($values);
256         }
257         $path = '/'.trim(Net_URL::resolvePath($path), '/');
258         if (!empty($qstring)) {
259             $path .= '?'.http_build_query($qstring);
260         }
261         if (!empty($anchor)) {
262             $path .= '#'.ltrim($anchor, '#');
263         }
264         return $path;
265     }
266
267     public function getRequired()
268     {
269         if (!isset($this->required)) {
270             $req = array();
271             foreach ($this->parts as $part) {
272                 if ($part->isRequired()) {
273                     $req[] = $part->content;
274                 }
275             }
276             $this->required = $req;
277         }
278         return $this->required;
279     }
280
281     public function getMaxKeys()
282     {
283         if (is_null($this->maxKeys)) {
284             $this->maxKeys = count($this->required);
285             $this->maxKeys += count($this->defaults);
286         }
287         return $this->maxKeys;
288     }
289
290
291
292 /*!lex2php
293 %input $this->path
294 %counter $this->N
295 %token $this->token
296 %value $this->value
297 %line $this->line
298 static = /\/?([^\/:\*]+)/
299 variable = /([a-zA-Z0-9_]+)/
300 dynamic = /\/?:/
301 wildcard = @/?\*@
302 grouping = /\/?\(([a-zA-Z0-9_]+)\)/
303 */
304 /*!lex2php
305 %statename START
306 dynamic grouping {
307     $c = $yy_subpatterns[0];
308     $part = self::createPart(Net_URL_Mapper_Part::DYNAMIC, $c, $this->value);
309     $this->addPart($part);
310 }
311 wildcard grouping {
312     $c = $yy_subpatterns[0];
313     $part = self::createPart(Net_URL_Mapper_Part::WILDCARD, $c, $this->value);
314     $this->addPart($part);
315 }
316 dynamic variable {
317     $c = $yy_subpatterns[0];
318     $part = self::createPart(Net_URL_Mapper_Part::DYNAMIC, $c, $this->value);
319     $this->addPart($part);
320 }
321 wildcard variable {
322     $c = $yy_subpatterns[0];
323     $part = self::createPart(Net_URL_Mapper_Part::WILDCARD, $c, $this->value);
324     $this->addPart($part);
325 }
326 static {
327     $c = $yy_subpatterns[0];
328     $part = self::createPart(Net_URL_Mapper_Part::FIXED, $c, $this->value);
329     $this->addPart($part);
330 }
331 */
332 }
333
334 ?>