/Url/src/url_creator.php

https://github.com/F5/zetacomponents · PHP · 140 lines · 34 code · 6 blank · 100 comment · 3 complexity · 9af1eb196bbc3dff2b5503a2ad706171 MD5 · raw file

  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. */
  27. /**
  28. * ezcUrlCreator makes it easy to create urls from scratch.
  29. *
  30. * Holds a list of urls mapped to aliases. The aliases are used to refer to the
  31. * urls stored, so the urls will not be hardcoded all over the application code.
  32. *
  33. * Example of use:
  34. * <code>
  35. * // register an URL under the alias 'map'
  36. * ezcUrlCreator::registerUrl( 'map', '/images/geo/%s?xsize=%d&ysize=%d&zoom=%d' );
  37. *
  38. * // retrieve the stored URL under the alias 'map' formatted with parameters
  39. * $url = ezcUrlCreator::getUrl( 'map', 'map_norway.gif', 450, 450, 4 );
  40. * // will be: "/images/geo/map_norway.gif?xsize=450&ysize=450&zoom=4"
  41. *
  42. * // retrieve the stored URL under the alias 'map' formatted with other parameters
  43. * $url = ezcUrlCreator::getUrl( 'map', 'map_sweden.gif', 450, 450, 4 );
  44. * // will be: "/images/geo/map_sweden.gif?xsize=450&ysize=450&zoom=4"
  45. * </code>
  46. *
  47. * @package Url
  48. * @version //autogen//
  49. */
  50. class ezcUrlCreator
  51. {
  52. /**
  53. * Holds the registered urls.
  54. *
  55. * @var array(string=>string)
  56. */
  57. private static $urls = array();
  58. /**
  59. * Registers $url as $name in the URLs list.
  60. *
  61. * If $name is already registered, it will be overwritten.
  62. *
  63. * @param string $name The name associated with the URL
  64. * @param string $url The URL to register
  65. */
  66. public static function registerUrl( $name, $url )
  67. {
  68. self::$urls[$name] = $url;
  69. }
  70. /**
  71. * Returns the URL registerd as $name prepended to $suffix.
  72. *
  73. * Example:
  74. * <code>
  75. * ezcUrlCreator::registerUrl( 'map', '/images/geo?xsize=450&ysize=450&zoom=4' );
  76. * echo ezcUrlCreator::prependUrl( 'map', 'map_sweden.gif' );
  77. * </code>
  78. * will output:
  79. * /images/geo/map_sweden.gif?xsize=450&ysize=450&zoom=4
  80. *
  81. * @throws ezcUrlNotRegisteredException
  82. * if $name is not registered
  83. * @param string $name The name associated with the URL that will be appended with $suffix
  84. * @param string $suffix The string which will be appended to the URL
  85. * @return string
  86. */
  87. public static function prependUrl( $name, $suffix )
  88. {
  89. if ( !isset( self::$urls[$name] ) )
  90. {
  91. throw new ezcUrlNotRegisteredException( $name );
  92. }
  93. $url = new ezcUrl( self::$urls[$name] );
  94. $url->path = array_merge( $url->path, explode( '/', $suffix ) );
  95. return $url->buildUrl();
  96. }
  97. /**
  98. * Returns the URL registered as $name.
  99. *
  100. * This function accepts a variable number of arguments like the sprintf()
  101. * function. If you specify more than 1 arguments when calling this
  102. * function, the registered URL will be formatted using those arguments
  103. * similar with the sprintf() function.
  104. * Example:
  105. * <code>
  106. * ezcUrlCreator::registerUrl( 'map', '/images/geo/%s?xsize=%d&ysize=%d&zoom=%d' );
  107. * echo ezcUrlCreator::getUrl( 'map', 'map_sweden.gif', 450, 450, 4 );
  108. * </code>
  109. * will output:
  110. * /images/geo/map_sweden.gif?xsize=450&ysize=450&zoom=4
  111. *
  112. * @throws ezcUrlNotRegisteredException
  113. * if $name is not registered
  114. * @param string $name The name associated with the URL
  115. * @param mixed $args,... Optional values which will be vsprintf-ed in the URL
  116. * @return string
  117. */
  118. public static function getUrl( $name )
  119. {
  120. if ( !isset( self::$urls[$name] ) )
  121. {
  122. throw new ezcUrlNotRegisteredException( $name );
  123. }
  124. if ( func_num_args() > 1 )
  125. {
  126. $args = func_get_args();
  127. // get rid of the first argument ($name)
  128. unset( $args[0] );
  129. return vsprintf( self::$urls[$name], $args );
  130. }
  131. return self::$urls[$name];
  132. }
  133. }
  134. ?>