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

Source for file URI.class.php

Documentation is available at URI.class.php

  1. <?php
  2.  
  3. /**
  4.  * Класс URI.
  5.  *
  6.  * @package energine
  7.  * @subpackage core
  8.  * @author 1m
  9.  * @copyright Energine 2006
  10.  * @version $Id$
  11.  */
  12.  
  13. //require_once('core/framework/Object.class.php');
  14.  
  15. /**
  16.  * URI (Unified Resource Identifier).
  17.  *
  18.  * @package energine
  19.  * @subpackage core
  20.  * @author 1m
  21.  * @final
  22.  */
  23. final class URI extends Object {
  24.  
  25.     /**
  26.      * @access private
  27.      * @var string схема (протокол) запроса
  28.      */
  29.     private $scheme;
  30.  
  31.     /**
  32.      * @access private
  33.      * @var string сервер (имя хоста)
  34.      */
  35.     private $host;
  36.  
  37.     /**
  38.      * @access private
  39.      * @var array путь
  40.      */
  41.     private $path;
  42.  
  43.     /**
  44.      * @access private
  45.      * @var string строка параметров
  46.      */
  47.     private $query;
  48.  
  49.     /**
  50.      * @access private
  51.      * @var string идентификатор фрагмента документа
  52.      */
  53.     private $fragment;
  54.  
  55.     /**
  56.      * Конструктор класса.
  57.      *
  58.      * @access public
  59.      * @param string $uri 
  60.      * @return void 
  61.      */
  62.     public function __construct($uri{
  63.         parent::__construct();
  64.  
  65.         if ($validatedURL self::validate($uri)) {
  66.             $this->setScheme($validatedURL[1]);
  67.             $this->setHost($validatedURL[2]);
  68.             $this->setPath($validatedURL[3]);
  69.             $this->setQuery(isset($validatedURL[5]$validatedURL[5'');
  70.         }
  71.         else {
  72.             $this->scheme = $this->host = $this->path = $this->query = $this->fragment = '';
  73.         }
  74.     }
  75.     /**
  76.      * Проверяет является ли переданная строка URLом
  77.      * 
  78.      * @param $uri УРЛ
  79.      * @return mixed array($scheme, $host, $path, $query) || false
  80.      * @access public
  81.      * @static
  82.      */
  83.     public static function validate($uri){
  84.         $result false;
  85.         if(preg_match('/^(\w+):\/\/([a-z0-9\.\-]+)(\/[^?]*)(\?(.*))?$/i'$uri$matches&& count($matches>= 4){
  86.            $result $matches;    
  87.         }
  88.         
  89.         return $result;
  90.     }
  91.  
  92.     /**
  93.      * Устанавливает схему (протокол) URI.
  94.      *
  95.      * @access public
  96.      * @param string $scheme 
  97.      * @return void 
  98.      */
  99.     public function setScheme($scheme{
  100.         $this->scheme = strtolower($scheme);
  101.     }
  102.  
  103.     /**
  104.      * Возвращает схему (протокол) URI.
  105.      *
  106.      * @access public
  107.      * @return string 
  108.      */
  109.     public function getScheme({
  110.         return $this->scheme;
  111.     }
  112.  
  113.     /**
  114.      * Устанавливает имя хоста.
  115.      *
  116.      * @access public
  117.      * @param string $host 
  118.      * @return void 
  119.      */
  120.     public function setHost($host{
  121.         $this->host = strtolower($host);
  122.     }
  123.  
  124.     /**
  125.      * Возвращает имя хоста.
  126.      *
  127.      * @access public
  128.      * @return string 
  129.      */
  130.     public function getHost({
  131.         return $this->host;
  132.     }
  133.     
  134.     /**
  135.      * Устанавливает порт
  136.      *
  137.      * @access public
  138.      * @return void 
  139.      */
  140.     public function setPort($port{
  141.         if (empty($port)) {
  142.             $port 80;
  143.         }
  144.         
  145.         $this->port $port;
  146.     }
  147.     /**
  148.      * Возвращает идентификатор порта
  149.      *
  150.      * @access public
  151.      * @return int 
  152.      */
  153.     public function getPort({
  154.         return $this->port;
  155.     }    
  156.  
  157.     /**
  158.      * Устанавливает путь.
  159.      *
  160.      * @access public
  161.      * @param $path 
  162.      * @return void 
  163.      */
  164.     public function setPath($path{
  165.         if (!is_array($path)) {
  166.             $path array_values(array_diff(explode('/'$path)array('')));
  167.         }
  168.         $this->path = $path;
  169.     }
  170.  
  171.     /**
  172.      * Возвращает путь в виде массива сегментов или в виде строки,
  173.      * если установлен флаг $asString.
  174.      *
  175.      * @access public
  176.      * @param boolean $asString 
  177.      * @return string 
  178.      */
  179.     public function getPath($asString true{
  180.         $path $this->path;
  181.         if ($asString{
  182.             if (!empty($path)) {
  183.                 $path '/'.implode('/'$path).'/';
  184.             }
  185.             else {
  186.                 $path '/';
  187.             }
  188.         }
  189.         return $path;
  190.     }
  191.  
  192.     /**
  193.      * Возвращает сегмент пути с индексом $pos.
  194.      *
  195.      * @access public
  196.      * @param int $pos 
  197.      * @return string 
  198.      */
  199.     public function getPathSegment($pos{
  200.         if (isset($this->path[$pos])) {
  201.             return $this->path[$pos];
  202.         }
  203.         return '';
  204.     }
  205.  
  206.     /**
  207.      * Устанавливает строку параметров.
  208.      *
  209.      * @access public
  210.      * @param string $query 
  211.      * @return void 
  212.      */
  213.     public function setQuery($query{
  214.         $this->query = strval($query);
  215.     }
  216.  
  217.     /**
  218.      * Возвращает строку параметров.
  219.      *
  220.      * @access public
  221.      * @return string 
  222.      */
  223.     public function getQuery({
  224.         return $this->query;
  225.     }
  226.  
  227.     /**
  228.      * Устанавливает идентификатор фрагмента документа.
  229.      *
  230.      * @access public
  231.      * @param string $fragment 
  232.      * @return void 
  233.      */
  234.     public function setFragment($fragment{
  235.         $this->fragment = strval($fragment);
  236.     }
  237.  
  238.     /**
  239.      * Возвращает идентификатор фрагмента документа.
  240.      *
  241.      * @return string 
  242.      * @access public
  243.      */
  244.     public function getFragment({
  245.         return $this->fragment;
  246.     }
  247.  
  248.     /**
  249.      * Возвращает строковое представление URI.
  250.      *
  251.      * @access public
  252.      * @return string 
  253.      */
  254.     public function __toString({
  255.         if (!empty($this->scheme&& !empty($this->host)) {
  256.             return $this->scheme.'://'.$this->host.
  257.                 (empty($this->path'/' $this->getPath(true)).
  258.                 (empty($this->query'' '?'.$this->query).
  259.                 (empty($this->fragment'' '#'.$this->fragment);
  260.         }
  261.         else {
  262.             return '';
  263.         }
  264.     }
  265. }
В создании документации нам помог: phpDocumentor