<?php
class Route {
const RESOURCE_PATTERN = '/'.'\[:(.*)\]'.'/';
const TOKEN_PARA_PATTERN = '/'.'\{:(.*)\}'.'/';
private $methodList;
private $url;
private $urlTokenList;
private $urlTokenCount;
private $controller;
private $action;
private $tokenParameterList;
private $resourceList = array();
private $resourceDetailList = array();
public function __construct($httpMethod, $httpUrl, $controller, $action) {
$this->methodList = explode('|', $httpMethod);
$this->httpUrl = $httpUrl;
$this->controller = $controller;
$this->action = $action;
$this->parseURL($httpUrl);
}
public function parseURL($url) {
$tmpUrl = $url;
if (substr($tmpUrl, 0, 1) == '/') {
$tmpUrl = substr($tmpUrl, 1);
}
if (substr($tmpUrl, -1) == '/') {
$tmpUrl = substr($tmpUrl, 0, -1);
}
$this->urlTokenList = explode('/', $tmpUrl);
$this->urlTokenCount = count($this->urlTokenList);
foreach($this->urlTokenList as $token) {
$isMatchedForResourcePattern = preg_match(self::RESOURCE_PATTERN, $token, $resourceMatches, PREG_OFFSET_CAPTURE);
if ($isMatchedForResourcePattern === 1) {
array_push($this->resourceList, $resourceMatches[1][0]);
}
$isMatchedForParaPattern = preg_match(self::TOKEN_PARA_PATTERN, $token, $paramMatches, PREG_OFFSET_CAPTURE);
}
}
public function getMethodList() {
return $this->methodList;
}
public function getUrl() {
return $this->url;
}
public function getUrlTokenList() {
return $this->urlTokenList;
}
public function getUrlTokenCount() {
return $this->urlTokenCount;
}
public function getController() {
return $this->controller;
}
public function getAction() {
return $this->action;
}
public function getTokenParameterList() {
return $this->tokenParameterList;
}
public function getResourceList() {
return $this->resourceList;
}
public function getResourceDetailList() {
return $this->resourceDetailList;
}
public function isMatched($url) {
return 0;
}
}
$ROUTES =
array(
array('httpMethod' => 'GET', 'url' => '[:music_category]/xml', 'controller' => 'CategoryController', 'action' => 'xml'),
array('httpMethod' => 'GET', 'url' => '[:music_category]/json', 'controller' => 'CategoryController', 'action' => 'json'),
array('httpMethod' => 'GET|POST', 'url' => 'xml', 'controller' => 'temp_controller', 'action' => 'xml'),
array('httpMethod' => 'GET|POST', 'url' => 'json', 'controller' => 'temp_controller', 'action' => 'json'),
array('httpMethod' => 'GET|POST', 'url' => 'post/[:music_category]/{:data}/xml', 'controller' => 'CategoryController', 'action' => 'post')
);
foreach ($ROUTES as $route) {
$routeObj = new Route($route['httpMethod'], $route['url'], $route['controller'], $route['action']);
print_r($routeObj->getMethodList());
printf($routeObj->getUrl());
print_r($routeObj->getUrlTokenList());
printf($routeObj->getController());
printf($routeObj->getAction());
print_r($routeObj->getTokenParameterList());
print_r($routeObj->getResourceList());
print_r($routeObj->getResourceDetailList());
}
preferences:
28.2 ms | 402 KiB | 5 Q