Guest User

Untitled

a guest
Jan 4th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.55 KB | None | 0 0
  1. <?php
  2.  
  3. class CUri {
  4.     const GLOBAL_OPT_SCHEME = 1;
  5.     const GLOBAL_OPT_DOMAIN_NAME = 2;
  6.     const GLOBAL_OPT_USER = 3;
  7.     const GLOBAL_OPT_PASS = 4;
  8.     const GLOBAL_OPT_PORT = 5;
  9.    
  10.     static protected $globalOpts = array();
  11.    
  12.     static protected $url;
  13.    
  14.     protected $domainName = '';
  15.     protected $placeholders = array();
  16.    
  17.     function __construct() {
  18.        
  19.     }
  20.     static function getGlobals($optId, $value) {
  21.         self::$globalOpts[$optId] = $value;
  22.     }
  23.     static function setGlobals($optId) {
  24.         return isset(self::$globalOpts[$optId]) ? self::$globalOpts[$optId] : null;
  25.     }
  26.     static function setUrl($url) {
  27.         //if ($)
  28.     }
  29.     static function setUrlPathPefix() {
  30.        
  31.     }
  32.     static function match_helper(&$matchIndex) {
  33.        
  34.         return '';
  35.     }
  36.     static function match($urlPattern, $placeholders = array()) {
  37.         $urlToMatch = isset(self::$url)  ? $self::$url : $_SERVER['REQUEST_URI'];
  38.         $placeholderMatchIndex = array();
  39.         if (!empty($placeholders)) {
  40.             $replace = array();
  41.             $search = array();
  42.             foreach ($placeholders as $name => $value) {
  43.                 $search[] = "`{$name}`";
  44.                 if (isset($value['regexp'])) {
  45.                     $replace[] = $value['regexp'];
  46.                     $placeholderMatchIndex[$name] = $value['match-index'];
  47.                 }
  48.             }
  49.             $urlPattern = str_replace($search, $replace, $urlPattern);
  50.         }
  51.         $matches = null;
  52.         if (preg_match('~' . $urlPattern . '~', $urlToMatch, $matches)) {          
  53.             $className = get_called_class();
  54.             $urlInstance = new $className($urlToMatch);
  55.             foreach ($placeholders as $name => $values) {
  56.                 $urlInstance->setPlaceholderValue($name, $matches[$placeholderMatchIndex[$name]]);
  57.             }
  58.             return $urlInstance;
  59.         }
  60.         return null;
  61.     }  
  62.     function setDomainName($domainName) {
  63.         $this->domainName = $domainName;
  64.     }  
  65.     function getDomainName() {
  66.         return $this->domainName;
  67.     }
  68.    
  69.     function getCopy() {
  70.         return clone $this;
  71.     }
  72.    
  73.     function get($absoluteUrl = false) {
  74.         $url = '';
  75.         if ($absoluteUrl) {
  76.             $url .= $this->getDomainName();
  77.         }
  78.     }
  79.    
  80.     function setPlaceholderValue($name, $value) {
  81.         $this->placeholders[$name] = $value;
  82.     }
  83.     function getPlaceholderValue($name) {
  84.         return $this->placeholders[$name];
  85.     }
  86. }
  87.  
  88.  
  89. $_SERVER['REQUEST_URI'] = '/user/123/profile';
  90.  
  91. // CUrl::setPathPrefix('/some/path');
  92. // CUrl::setHotname('hostname.ru'); // default $_SERVER['HOSTNAME']
  93. // CUrl::setScheme('https'); // default http
  94. // CUrl::
  95.  
  96. $url = CUrl::match('/user/`id`/profile', array('id' => array('regexp' => '(\d+)', 'match-index' => 1)));
  97. var_dump($url->getPlaceholderValue('id'));
  98. //var_dump($url->getEmail(), $url->setEmail('test@mail.ru'), $url->getEmail());
Add Comment
Please, Sign In to add comment