scratch – Blame information for rev 126
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
126 | office | 1 | <?php |
2 | /** |
||
3 | * |
||
4 | * This file is part of the Aura project for PHP. |
||
5 | * |
||
6 | * @package Aura.Uri |
||
7 | * |
||
8 | * @license http://opensource.org/licenses/bsd-license.php BSD |
||
9 | * |
||
10 | */ |
||
11 | namespace Aura\Uri; |
||
12 | |||
13 | /** |
||
14 | * |
||
15 | * Manage the Path |
||
16 | * |
||
17 | * @package Aura.Uri |
||
18 | * |
||
19 | */ |
||
20 | class Path extends \ArrayObject |
||
21 | { |
||
22 | /** |
||
23 | * |
||
24 | * The dot-format extension of the last path element, including the dot |
||
25 | * (for example, the ".rss" in "feed.rss"). |
||
26 | * |
||
27 | * @var string |
||
28 | * |
||
29 | */ |
||
30 | protected $format; |
||
31 | |||
32 | /** |
||
33 | * |
||
34 | * Returns the path array as a string, including the format. |
||
35 | * |
||
36 | * @return string The path string. |
||
37 | * |
||
38 | */ |
||
39 | public function __toString() |
||
40 | { |
||
41 | $spec = $this->getArrayCopy(); |
||
42 | $path = []; |
||
43 | |||
44 | // encode each path element |
||
45 | foreach ($spec as $elem) { |
||
46 | $path[] = rawurlencode($elem); |
||
47 | } |
||
48 | |||
49 | // create a string from the encoded elements |
||
50 | $url = implode('/', $path) . $this->format; |
||
51 | |||
52 | return !empty( $url ) ? '/' . $url : $url; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * |
||
57 | * Sets the $path array and $format value from a string. |
||
58 | * |
||
59 | * This will overwrite any previous values. |
||
60 | * |
||
61 | * @param string $path The path string to use; for example, |
||
62 | * "/foo/bar/baz/dib.gir". A leading slash will *not* create an empty |
||
63 | * first element; if the string has a leading slash, it is ignored. |
||
64 | * |
||
65 | * @return void |
||
66 | * |
||
67 | */ |
||
68 | public function setFromString($path) |
||
69 | { |
||
70 | $this->exchangeArray([]); |
||
71 | $path = explode('/', $path); |
||
72 | |||
73 | if ($path[0] == '') { |
||
74 | array_shift($path); |
||
75 | } |
||
76 | |||
77 | foreach ($path as $key => $val) { |
||
78 | $this[$key] = urldecode($val); |
||
79 | } |
||
80 | |||
81 | // $key and $val are already at the end |
||
82 | $this->setFormat(null); |
||
83 | if ( isset($val) ) { |
||
84 | // find the last dot in the value |
||
85 | $pos = strrpos($val, '.'); |
||
86 | if ($pos !== false) { |
||
87 | // remove from the path and retain as the format |
||
88 | $this[$key] = substr($val, 0, $pos); |
||
89 | $this->setFormat(substr($val, $pos)); |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * |
||
96 | * Set the dot-format; remember to include the leading dot. |
||
97 | * |
||
98 | * @param string $format |
||
99 | * |
||
100 | * @return void |
||
101 | * |
||
102 | */ |
||
103 | public function setFormat($format) |
||
104 | { |
||
105 | $this->format = $format; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * |
||
110 | * Get the dot-format extension. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public function getFormat() |
||
115 | { |
||
116 | return $this->format; |
||
117 | } |
||
118 | } |