URI_Template_Parser

A URI Template Parser which is used by the apiREST class to resolve the REST requests Blogpost: http://lab.kevburnsjr.com/php-uri-template-parser Source: http://github.

Defined (1)

The class is defined in the following location(s).

/gdwpm-api/external/URITemplateParser.php  
  1. class URI_Template_Parser { 
  2.  
  3. public static $operators = array('+', ';', '?', '/', '.'); 
  4. public static $reserved_operators = array('|', '!', '@'); 
  5. public static $explode_modifiers = array('+', '*'); 
  6. public static $partial_modifiers = array(':', '^'); 
  7.  
  8. public static $gen_delims = array(':', '/', '?', '#', '[', ']', '@'); 
  9. public static $gen_delims_pct = array('%3A', '%2F', '%3F', '%23', '%5B', '%5D', '%40'); 
  10. public static $sub_delims = array('!', '$', '&', '\'', '(', ')', '*', '+', ', ', ';', '='); 
  11. public static $sub_delims_pct = array('%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '%3B', '%3D'); 
  12. public static $reserved; 
  13. public static $reserved_pct; 
  14.  
  15. public function __construct($template) { 
  16. self::$reserved = array_merge(self::$gen_delims, self::$sub_delims); 
  17. self::$reserved_pct = array_merge(self::$gen_delims_pct, self::$sub_delims_pct); 
  18. $this->template = $template; 
  19.  
  20. public function expand($data) { 
  21. // Modification to make this a bit more performant (since gettype is very slow) 
  22. if (! is_array($data)) { 
  23. $data = (array)$data; 
  24. /** 
  25. // Original code, which uses a slow gettype() statement, kept in place for if the assumption that is_array always works here is incorrect 
  26. switch (gettype($data)) { 
  27. case "boolean": 
  28. case "integer": 
  29. case "double": 
  30. case "string": 
  31. case "object": 
  32. $data = (array)$data; 
  33. break; 
  34. */ 
  35.  
  36. // Resolve template vars 
  37. preg_match_all('/\{([^\}]*)\}/', $this->template, $em); 
  38.  
  39. foreach ($em[1] as $i => $bare_expression) { 
  40. preg_match('/^([\+\;\?\/\.]{1})?(.*)$/', $bare_expression, $lm); 
  41. $exp = new StdClass(); 
  42. $exp->expression = $em[0][$i]; 
  43. $exp->operator = $lm[1]; 
  44. $exp->variable_list = $lm[2]; 
  45. $exp->varspecs = explode(', ', $exp->variable_list); 
  46. $exp->vars = array(); 
  47. foreach ($exp->varspecs as $varspec) { 
  48. preg_match('/^([a-zA-Z0-9_]+)([\*\+]{1})?([\:\^][0-9-]+)?(\=[^, ]+)?$/', $varspec, $vm); 
  49. $var = new StdClass(); 
  50. $var->name = $vm[1]; 
  51. $var->modifier = isset($vm[2]) && $vm[2] ? $vm[2] : null; 
  52. $var->modifier = isset($vm[3]) && $vm[3] ? $vm[3] : $var->modifier; 
  53. $var->default = isset($vm[4]) ? substr($vm[4], 1) : null; 
  54. $exp->vars[] = $var; 
  55.  
  56. // Add processing flags 
  57. $exp->reserved = false; 
  58. $exp->prefix = ''; 
  59. $exp->delimiter = ', '; 
  60. switch ($exp->operator) { 
  61. case '+': 
  62. $exp->reserved = 'true'; 
  63. break; 
  64. case ';': 
  65. $exp->prefix = ';'; 
  66. $exp->delimiter = ';'; 
  67. break; 
  68. case '?': 
  69. $exp->prefix = '?'; 
  70. $exp->delimiter = '&'; 
  71. break; 
  72. case '/': 
  73. $exp->prefix = '/'; 
  74. $exp->delimiter = '/'; 
  75. break; 
  76. case '.': 
  77. $exp->prefix = '.'; 
  78. $exp->delimiter = '.'; 
  79. break; 
  80. $expressions[] = $exp; 
  81.  
  82. // Expansion 
  83. $this->expansion = $this->template; 
  84.  
  85. foreach ($expressions as $exp) { 
  86. $part = $exp->prefix; 
  87. $exp->one_var_defined = false; 
  88. foreach ($exp->vars as $var) { 
  89. $val = ''; 
  90. if ($exp->one_var_defined && isset($data[$var->name])) { 
  91. $part .= $exp->delimiter; 
  92. // Variable present 
  93. if (isset($data[$var->name])) { 
  94. $exp->one_var_defined = true; 
  95. $var->data = $data[$var->name]; 
  96.  
  97. $val = self::val_from_var($var, $exp); 
  98.  
  99. // Variable missing 
  100. } else { 
  101. if ($var->default) { 
  102. $exp->one_var_defined = true; 
  103. $val = $var->default; 
  104. $part .= $val; 
  105. if (! $exp->one_var_defined) $part = ''; 
  106. $this->expansion = str_replace($exp->expression, $part, $this->expansion); 
  107.  
  108. return $this->expansion; 
  109.  
  110. private function val_from_var($var, $exp) { 
  111. $val = ''; 
  112. if (is_array($var->data)) { 
  113. $i = 0; 
  114. if ($exp->operator == '?' && ! $var->modifier) { 
  115. $val .= $var->name . '='; 
  116. foreach ($var->data as $k => $v) { 
  117. $del = $var->modifier ? $exp->delimiter : ', '; 
  118. $ek = rawurlencode($k); 
  119. $ev = rawurlencode($v); 
  120.  
  121. // Array 
  122. if ($k !== $i) { 
  123. if ($var->modifier == '+') { 
  124. $val .= $var->name . '.'; 
  125. if ($exp->operator == '?' && $var->modifier || $exp->operator == ';' && $var->modifier == '*' || $exp->operator == ';' && $var->modifier == '+') { 
  126. $val .= $ek . '='; 
  127. } else { 
  128. $val .= $ek . $del; 
  129.  
  130. // List 
  131. } else { 
  132. if ($var->modifier == '+') { 
  133. if ($exp->operator == ';' && $var->modifier == '*' || $exp->operator == ';' && $var->modifier == '+' || $exp->operator == '?' && $var->modifier == '+') { 
  134. $val .= $var->name . '='; 
  135. } else { 
  136. $val .= $var->name . '.'; 
  137. $val .= $ev . $del; 
  138. $i ++; 
  139. $val = trim($val, $del); 
  140.  
  141. // Strings, numbers, etc. 
  142. } else { 
  143. if ($exp->operator == '?') { 
  144. $val = $var->name . (isset($var->data) ? '=' : ''); 
  145. } else if ($exp->operator == ';') { 
  146. $val = $var->name . ($var->data ? '=' : ''); 
  147. $val .= rawurlencode($var->data); 
  148. if ($exp->operator == '+') { 
  149. $val = str_replace(self::$reserved_pct, self::$reserved, $val); 
  150. return $val; 
  151.  
  152. public function match($uri) {} 
  153.  
  154. public function __toString() { 
  155. return $this->template;