Link.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * This class manages url links between controllers and actions.
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. class Staple_Link
  24. {
  25. protected $link;
  26. protected static $upper = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
  27. protected static $lower = array('-a','-b','-c','-d','-e','-f','-g','-h','-i','-j','-k','-l','-m','-n','-o','-p','-q','-r','-s','-t','-u','-v','-w','-x','-y','-z');
  28. public function __construct($route = NULL, array $get = array())
  29. {
  30. if(isset($route))
  31. {
  32. $this->link = self::get($route,$get);
  33. }
  34. }
  35. public function __toString()
  36. {
  37. return $this->link;
  38. }
  39. public static function Create($route, array $get = array())
  40. {
  41. return new static($route, $get);
  42. }
  43. /**
  44. * Creates the shortest link possible to the specified controller/action.
  45. * The array must include a controller, followed by an action, followed by any parameters
  46. * sent to the action as unique items in the array. Parameters may only be of type string
  47. * or int.
  48. *
  49. * @param mixed $route
  50. * @param array $get
  51. * @return string
  52. */
  53. public static function get($route, array $get = array())
  54. {
  55. //Convert Get array to get string.
  56. $getString = self::getArraytoString($get);
  57. //Set the link base
  58. $base = Staple_Config::getValue('application', 'public_location');
  59. //Is the link an array or a string?
  60. if(!is_array($route))
  61. {
  62. //Return a string link.
  63. $link = $base.$route;
  64. }
  65. else
  66. {
  67. //Process and Controller/Action/Parameter route
  68. //Setup the default link and the case-insensitive replacements.
  69. $link = '#';
  70. //Count the route elements
  71. $routesize = count($route);
  72. if($routesize == 0)
  73. {
  74. $link = $base; //An empty array returns a base link.
  75. }
  76. elseif($routesize == 1)
  77. {
  78. if(ctype_alnum((string)$route[0]))
  79. {
  80. $controller = (string)$route[0];
  81. if($controller == 'index')
  82. {
  83. $link = $base;
  84. }
  85. else
  86. {
  87. $controller = self::urlCase($controller);
  88. $link = $base.$controller;
  89. }
  90. }
  91. else
  92. {
  93. throw new Exception('Bad Link',Staple_Error::LINK_ERROR);
  94. }
  95. }
  96. else
  97. {
  98. //Extract the Controller, Action and Parameters.
  99. $controller = (string)array_shift($route);
  100. $action = (string)array_shift($route);
  101. //URL Encode parameter values.
  102. $params = array();
  103. foreach($route as $value)
  104. {
  105. $params[] = urlencode($value);
  106. }
  107. //Check that the route follows valid syntax are valid.
  108. if(ctype_alnum($controller) && ctype_alnum($action))
  109. {
  110. if($controller == 'index' && $action == 'index' && $params == array())
  111. {
  112. $link = $base;
  113. }
  114. elseif($controller != 'index' && $action == 'index' && $params == array())
  115. {
  116. $link = $base.$controller;
  117. }
  118. else
  119. {
  120. if(count($params) > 0)
  121. {
  122. $paramstring = '/'.implode('/', $params);
  123. }
  124. else
  125. {
  126. $paramstring = '';
  127. }
  128. //Convert action to case-insensitive value
  129. $controller = self::urlCase($controller);
  130. $action = self::urlCase($action);
  131. $link = $base.$controller.'/'.$action.$paramstring;
  132. }
  133. }
  134. else
  135. {
  136. throw new Exception('Bad Link',Staple_Error::LINK_ERROR);
  137. }
  138. }
  139. }
  140. //Finally append the get string
  141. if(strlen($getString) > 2)
  142. {
  143. $link .= '?'.$getString;
  144. }
  145. //Return the link.
  146. return $link;
  147. }
  148. /**
  149. * Creates a URL case-insensitive version of the supplied string.
  150. *
  151. * @param string $url
  152. * @return string;
  153. */
  154. public static function urlCase($url)
  155. {
  156. return str_replace(self::$upper, self::$lower, $url);
  157. }
  158. /**
  159. * Converts a url controller or action name back to the case sensitive version.
  160. *
  161. * @param string $method
  162. * @return string
  163. */
  164. public static function methodCase($method)
  165. {
  166. return str_replace(self::$lower, self::$upper, $method);
  167. }
  168. /**
  169. * Converts a get array key/value pairset to a get string.
  170. *
  171. * @param array $get
  172. * @return string
  173. */
  174. public static function getArraytoString(array $get)
  175. {
  176. $getString = '';
  177. foreach($get as $gkey=>$gvalue)
  178. {
  179. $getString .= urlencode($gkey).'='.urlencode($gvalue).'&';
  180. }
  181. if(substr($getString, strlen($getString)-1,1) == '&')
  182. {
  183. $getString = substr($getString,0,strlen($getString)-1);
  184. }
  185. return $getString;
  186. }
  187. }
  188. ?>