Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class CUri {
- const GLOBAL_OPT_SCHEME = 1;
- const GLOBAL_OPT_DOMAIN_NAME = 2;
- const GLOBAL_OPT_USER = 3;
- const GLOBAL_OPT_PASS = 4;
- const GLOBAL_OPT_PORT = 5;
- static protected $globalOpts = array();
- static protected $url;
- protected $domainName = '';
- protected $placeholders = array();
- function __construct() {
- }
- static function getGlobals($optId, $value) {
- self::$globalOpts[$optId] = $value;
- }
- static function setGlobals($optId) {
- return isset(self::$globalOpts[$optId]) ? self::$globalOpts[$optId] : null;
- }
- static function setUrl($url) {
- //if ($)
- }
- static function setUrlPathPefix() {
- }
- static function match_helper(&$matchIndex) {
- return '';
- }
- static function match($urlPattern, $placeholders = array()) {
- $urlToMatch = isset(self::$url) ? $self::$url : $_SERVER['REQUEST_URI'];
- $placeholderMatchIndex = array();
- if (!empty($placeholders)) {
- $replace = array();
- $search = array();
- foreach ($placeholders as $name => $value) {
- $search[] = "`{$name}`";
- if (isset($value['regexp'])) {
- $replace[] = $value['regexp'];
- $placeholderMatchIndex[$name] = $value['match-index'];
- }
- }
- $urlPattern = str_replace($search, $replace, $urlPattern);
- }
- $matches = null;
- if (preg_match('~' . $urlPattern . '~', $urlToMatch, $matches)) {
- $className = get_called_class();
- $urlInstance = new $className($urlToMatch);
- foreach ($placeholders as $name => $values) {
- $urlInstance->setPlaceholderValue($name, $matches[$placeholderMatchIndex[$name]]);
- }
- return $urlInstance;
- }
- return null;
- }
- function setDomainName($domainName) {
- $this->domainName = $domainName;
- }
- function getDomainName() {
- return $this->domainName;
- }
- function getCopy() {
- return clone $this;
- }
- function get($absoluteUrl = false) {
- $url = '';
- if ($absoluteUrl) {
- $url .= $this->getDomainName();
- }
- }
- function setPlaceholderValue($name, $value) {
- $this->placeholders[$name] = $value;
- }
- function getPlaceholderValue($name) {
- return $this->placeholders[$name];
- }
- }
- $_SERVER['REQUEST_URI'] = '/user/123/profile';
- // CUrl::setPathPrefix('/some/path');
- // CUrl::setHotname('hostname.ru'); // default $_SERVER['HOSTNAME']
- // CUrl::setScheme('https'); // default http
- // CUrl::
- $url = CUrl::match('/user/`id`/profile', array('id' => array('regexp' => '(\d+)', 'match-index' => 1)));
- var_dump($url->getPlaceholderValue('id'));
- //var_dump($url->getEmail(), $url->setEmail('test@mail.ru'), $url->getEmail());
Add Comment
Please, Sign In to add comment