## Apache Zeta Components Manual :: File Source for url_creator.php

## Source for file url_creator.php

Documentation is available at url_creator.php

1. <?php
2. /**
3. * File containing the ezcUrlCreator class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @version //autogen//
24. * @filesource
25. * @package Url
26. */
28. /**
29. * ezcUrlCreator makes it easy to create urls from scratch.
30. *
31. * Holds a list of urls mapped to aliases. The aliases are used to refer to the
32. * urls stored, so the urls will not be hardcoded all over the application code.
33. *
34. * Example of use:
35. * <code>
36. * // register an URL under the alias 'map'
37. * ezcUrlCreator::registerUrl( 'map', '/images/geo/%s?xsize=%d&ysize=%d&zoom=%d' );
38. *
39. * // retrieve the stored URL under the alias 'map' formatted with parameters
40. * $url = ezcUrlCreator::getUrl( 'map', 'map_norway.gif', 450, 450, 4 );
41. * // will be: "/images/geo/map_norway.gif?xsize=450&ysize=450&zoom=4"
42. *
43. * // retrieve the stored URL under the alias 'map' formatted with other parameters
44. * $url = ezcUrlCreator::getUrl( 'map', 'map_sweden.gif', 450, 450, 4 );
45. * // will be: "/images/geo/map_sweden.gif?xsize=450&ysize=450&zoom=4"
46. * </code>
47. *
48. * @package Url
49. * @version //autogen//
50. */
51. class ezcUrlCreator
52. {
53. /**
54. * Holds the registered urls.
55. *
56. * @var array(string=>string)
57. */
58. private static $urls = array();
60. /**
61. * Registers $url as $name in the URLs list.
62. *
63. * If $name is already registered, it will be overwritten.
64. *
65. * @param string $name The name associated with the URL
66. * @param string $url The URL to register
67. */
68. public static function registerUrl ( $name, $url )
69. {
70. self::$urls[$name] = $url;
71. }
73. /**
74. * Returns the URL registerd as $name prepended to $suffix.
75. *
76. * Example:
77. * <code>
78. * ezcUrlCreator::registerUrl( 'map', '/images/geo?xsize=450&ysize=450&zoom=4' );
79. * echo ezcUrlCreator::prependUrl( 'map', 'map_sweden.gif' );
80. * </code>
81. * will output:
82. * /images/geo/map_sweden.gif?xsize=450&ysize=450&zoom=4
83. *
84. * @throws ezcUrlNotRegisteredException
85. * if $name is not registered
86. * @param string $name The name associated with the URL that will be appended with $suffix
87. * @param string $suffix The string which will be appended to the URL
88. * @return string
89. */
90. public static function prependUrl ( $name, $suffix )
91. {
92. if ( !isset( self::$urls[$name] ) )
93. {
94. throw new ezcUrlNotRegisteredException( $name );
95. }
97. $url = new ezcUrl ( self::$urls[$name] );
98. $url->path = array_merge( $url->path, explode ( '/', $suffix ) );
99. return $url->buildUrl();
100. }
102. /**
103. * Returns the URL registered as $name.
104. *
105. * This function accepts a variable number of arguments like the sprintf()
106. * function. If you specify more than 1 arguments when calling this
107. * function, the registered URL will be formatted using those arguments
108. * similar with the sprintf() function.
109. * Example:
110. * <code>
111. * ezcUrlCreator::registerUrl( 'map', '/images/geo/%s?xsize=%d&ysize=%d&zoom=%d' );
112. * echo ezcUrlCreator::getUrl( 'map', 'map_sweden.gif', 450, 450, 4 );
113. * </code>
114. * will output:
115. * /images/geo/map_sweden.gif?xsize=450&ysize=450&zoom=4
116. *
117. * @throws ezcUrlNotRegisteredException
118. * if $name is not registered
119. * @param string $name The name associated with the URL
120. * @param mixed $args,... Optional values which will be vsprintf-ed in the URL
121. * @return string
122. */
123. public static function getUrl ( $name )
124. {
125. if ( !isset( self::$urls[$name] ) )
126. {
127. throw new ezcUrlNotRegisteredException( $name );
128. }
130. if ( func_num_args () > 1 )
131. {
132. $args = func_get_args ();
133. // get rid of the first argument ($name)
134. unset( $args[0] );
135. return vsprintf ( self::$urls[$name], $args );
136. }
137. return self::$urls[$name];
138. }
139. }
140. ?>

Documentation generated by phpDocumentor 1.4.3