Microsoft_Uri
[ class tree: Microsoft_Uri ] [ index: Microsoft_Uri ] [ all elements ]

Source for file Uri.php

Documentation is available at Uri.php

  1. <?php
  2. /**
  3.  * Zend Framework
  4.  *
  5.  * LICENSE
  6.  *
  7.  * This source file is subject to the new BSD license that is bundled
  8.  * with this package in the file LICENSE.txt.
  9.  * It is also available through the world-wide-web at this URL:
  10.  * http://framework.zend.com/license/new-bsd
  11.  * If you did not receive a copy of the license and are unable to
  12.  * obtain it through the world-wide-web, please send an email
  13.  * to license@zend.com so we can send you a copy immediately.
  14.  *
  15.  * @category  Microsoft
  16.  * @package   Microsoft_Uri
  17.  * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18.  * @license   http://framework.zend.com/license/new-bsd     New BSD License
  19.  * @version   $Id: Uri.php 18950 2009-11-12 15:37:56Z alexander $
  20.  */
  21.  
  22. /**
  23.  * @see Microsoft_AutoLoader
  24.  */
  25. require_once dirname(__FILE__'/AutoLoader.php';
  26.  
  27. /**
  28.  * Abstract class for all Microsoft_Uri handlers
  29.  *
  30.  * @category  Microsoft
  31.  * @package   Microsoft_Uri
  32.  * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33.  * @license   http://framework.zend.com/license/new-bsd     New BSD License
  34.  */
  35. abstract class Microsoft_Uri
  36. {
  37.     /**
  38.      * Scheme of this URI (http, ftp, etc.)
  39.      *
  40.      * @var string 
  41.      */
  42.     protected $_scheme = '';
  43.  
  44.     /**
  45.      * Global configuration array
  46.      *
  47.      * @var array 
  48.      */
  49.     static protected $_config = array(
  50.         'allow_unwise' => false
  51.     );
  52.  
  53.     /**
  54.      * Return a string representation of this URI.
  55.      *
  56.      * @see    getUri()
  57.      * @return string 
  58.      */
  59.     public function __toString()
  60.     {
  61.         return $this->getUri();
  62.     }
  63.  
  64.     /**
  65.      * Convenience function, checks that a $uri string is well-formed
  66.      * by validating it but not returning an object.  Returns TRUE if
  67.      * $uri is a well-formed URI, or FALSE otherwise.
  68.      *
  69.      * @param  string $uri The URI to check
  70.      * @return boolean 
  71.      */
  72.     public static function check($uri)
  73.     {
  74.         try {
  75.             $uri self::factory($uri);
  76.         catch (Exception $e{
  77.             return false;
  78.         }
  79.  
  80.         return $uri->valid();
  81.     }
  82.  
  83.     /**
  84.      * Create a new Microsoft_Uri object for a URI.  If building a new URI, then $uri should contain
  85.      * only the scheme (http, ftp, etc).  Otherwise, supply $uri with the complete URI.
  86.      *
  87.      * @param  string $uri The URI form which a Microsoft_Uri instance is created
  88.      * @throws Microsoft_Uri_Exception When an empty string was supplied for the scheme
  89.      * @throws Microsoft_Uri_Exception When an illegal scheme is supplied
  90.      * @throws Microsoft_Uri_Exception When the scheme is not supported
  91.      * @return Microsoft_Uri 
  92.      * @link   http://www.faqs.org/rfcs/rfc2396.html
  93.      */
  94.     public static function factory($uri 'http')
  95.     {
  96.         // Separate the scheme from the scheme-specific parts
  97.         $uri            explode(':'$uri2);
  98.         $scheme         strtolower($uri[0]);
  99.         $schemeSpecific = isset($uri[1]=== true $uri[1'';
  100.  
  101.         if (strlen($scheme=== 0{
  102.             require_once 'Microsoft/Uri/Exception.php';
  103.             throw new Microsoft_Uri_Exception('An empty string was supplied for the scheme');
  104.         }
  105.  
  106.         // Security check: $scheme is used to load a class file, so only alphanumerics are allowed.
  107.         if (ctype_alnum($scheme=== false{
  108.             require_once 'Microsoft/Uri/Exception.php';
  109.             throw new Microsoft_Uri_Exception('Illegal scheme supplied, only alphanumeric characters are permitted');
  110.         }
  111.  
  112.         /**
  113.          * Create a new Microsoft_Uri object for the $uri. If a subclass of Microsoft_Uri exists for the
  114.          * scheme, return an instance of that class. Otherwise, a Microsoft_Uri_Exception is thrown.
  115.          */
  116.         switch ($scheme{
  117.             case 'http':
  118.                 // Break intentionally omitted
  119.             case 'https':
  120.                 $className 'Microsoft_Uri_Http';
  121.                 break;
  122.  
  123.             case 'mailto':
  124.                 // TODO
  125.             default:
  126.                 require_once 'Microsoft/Uri/Exception.php';
  127.                 throw new Microsoft_Uri_Exception("Scheme \"$scheme\" is not supported");
  128.                 break;
  129.         }
  130.  
  131.         if (!class_exists($className)) {
  132.             require_once str_replace('_''/'$className'.php';
  133.         }
  134.         $schemeHandler new $className($scheme$schemeSpecific);
  135.  
  136.         return $schemeHandler;
  137.     }
  138.  
  139.     /**
  140.      * Get the URI's scheme
  141.      *
  142.      * @return string|falseScheme or false if no scheme is set.
  143.      */
  144.     public function getScheme()
  145.     {
  146.         if (empty($this->_scheme=== false{
  147.             return $this->_scheme;
  148.         else {
  149.             return false;
  150.         }
  151.     }
  152.  
  153.     /**
  154.      * Set global configuration options
  155.      *
  156.      * @param Microsoft_Config|array$config 
  157.      */
  158.     static public function setConfig($config)
  159.     {
  160.         if ($config instanceof Microsoft_Config{
  161.             $config $config->toArray();
  162.         elseif (!is_array($config)) {
  163.             throw new Microsoft_Uri_Exception("Config must be an array or an instance of Microsoft_Config.");
  164.         }
  165.  
  166.         foreach ($config as $k => $v{
  167.             self::$_config[$k$v;
  168.         }
  169.     }
  170.  
  171.     /**
  172.      * Microsoft_Uri and its subclasses cannot be instantiated directly.
  173.      * Use Microsoft_Uri::factory() to return a new Microsoft_Uri object.
  174.      *
  175.      * @param string $scheme         The scheme of the URI
  176.      * @param string $schemeSpecific The scheme-specific part of the URI
  177.      */
  178.     abstract protected function __construct($scheme$schemeSpecific '');
  179.  
  180.     /**
  181.      * Return a string representation of this URI.
  182.      *
  183.      * @return string 
  184.      */
  185.     abstract public function getUri();
  186.  
  187.     /**
  188.      * Returns TRUE if this URI is valid, or FALSE otherwise.
  189.      *
  190.      * @return boolean 
  191.      */
  192.     abstract public function valid();
  193. }

Documentation generated on Wed, 18 May 2011 12:07:00 +0200 by phpDocumentor 1.4.3