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.  */
  27.  
  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. {
  52.     /**
  53.      * Holds the registered urls.
  54.      *
  55.      * @var array(string=>string) 
  56.      */
  57.     private static $urls array();
  58.  
  59.     /**
  60.      * Registers $url as $name in the URLs list.
  61.      *
  62.      * If $name is already registered, it will be overwritten.
  63.      *
  64.      * @param string $name The name associated with the URL
  65.      * @param string $url The URL to register
  66.      */
  67.     public static function registerUrl$name$url )
  68.     {
  69.         self::$urls[$name$url;
  70.     }
  71.  
  72.     /**
  73.      * Returns the URL registerd as $name prepended to $suffix.
  74.      *
  75.      * Example:
  76.      * <code>
  77.      * ezcUrlCreator::registerUrl( 'map', '/images/geo?xsize=450&ysize=450&zoom=4' );
  78.      * echo ezcUrlCreator::prependUrl( 'map', 'map_sweden.gif' );
  79.      * </code>
  80.      * will output:
  81.      * /images/geo/map_sweden.gif?xsize=450&ysize=450&zoom=4
  82.      *
  83.      * @throws ezcUrlNotRegisteredException
  84.      *          if $name is not registered
  85.      * @param string $name The name associated with the URL that will be appended with $suffix
  86.      * @param string $suffix The string which will be appended to the URL
  87.      * @return string 
  88.      */
  89.     public static function prependUrl$name$suffix )
  90.     {
  91.         if !issetself::$urls[$name) )
  92.         {
  93.             throw new ezcUrlNotRegisteredException$name );
  94.         }
  95.  
  96.         $url new ezcUrlself::$urls[$name);
  97.         $url->path array_merge$url->pathexplode'/'$suffix ) );
  98.         return $url->buildUrl();
  99.     }
  100.  
  101.     /**
  102.      * Returns the URL registered as $name.
  103.      *
  104.      * This function accepts a variable number of arguments like the sprintf()
  105.      * function. If you specify more than 1 arguments when calling this
  106.      * function, the registered URL will be formatted using those arguments
  107.      * similar with the sprintf() function.
  108.      * Example:
  109.      * <code>
  110.      * ezcUrlCreator::registerUrl( 'map', '/images/geo/%s?xsize=%d&ysize=%d&zoom=%d' );
  111.      * echo ezcUrlCreator::getUrl( 'map', 'map_sweden.gif', 450, 450, 4 );
  112.      * </code>
  113.      * will output:
  114.      * /images/geo/map_sweden.gif?xsize=450&ysize=450&zoom=4
  115.      *
  116.      * @throws ezcUrlNotRegisteredException
  117.      *          if $name is not registered
  118.      * @param string $name The name associated with the URL
  119.      * @param mixed $args,... Optional values which will be vsprintf-ed in the URL
  120.      * @return string 
  121.      */
  122.     public static function getUrl$name )
  123.     {
  124.         if !issetself::$urls[$name) )
  125.         {
  126.             throw new ezcUrlNotRegisteredException$name );
  127.         }
  128.  
  129.         if func_num_args()
  130.         {
  131.             $args func_get_args();
  132.             // get rid of the first argument ($name)
  133.             unset$args[0);
  134.             return vsprintfself::$urls[$name]$args );
  135.         }
  136.         return self::$urls[$name];
  137.     }
  138. }
  139. ?>
Documentation generated by phpDocumentor 1.4.3