PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/view/widget/google_login_oauth/src/external/URITemplateParser.php

https://gitlab.com/hop23typhu/doan_tienganh
PHP | 209 lines | 142 code | 19 blank | 48 comment | 53 complexity | dc829ddd424801e2bc4bf83bfb5e698c MD5 | raw file
  1. <?php
  2. /*
  3. Copyright (c) 2010 Kevin M Burns Jr, http://kevburnsjr.com/
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. /**
  22. * A URI Template Parser which is used by the apiREST class to resolve the REST requests
  23. * Blogpost: http://lab.kevburnsjr.com/php-uri-template-parser
  24. * Source: http://github.com/KevBurnsJr/php-uri-template-parser
  25. */
  26. class URI_Template_Parser {
  27. public static $operators = array('+', ';', '?', '/', '.');
  28. public static $reserved_operators = array('|', '!', '@');
  29. public static $explode_modifiers = array('+', '*');
  30. public static $partial_modifiers = array(':', '^');
  31. public static $gen_delims = array(':', '/', '?', '#', '[', ']', '@');
  32. public static $gen_delims_pct = array('%3A', '%2F', '%3F', '%23', '%5B', '%5D', '%40');
  33. public static $sub_delims = array('!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=');
  34. public static $sub_delims_pct = array('%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '%3B', '%3D');
  35. public static $reserved;
  36. public static $reserved_pct;
  37. public function __construct($template) {
  38. self::$reserved = array_merge(self::$gen_delims, self::$sub_delims);
  39. self::$reserved_pct = array_merge(self::$gen_delims_pct, self::$sub_delims_pct);
  40. $this->template = $template;
  41. }
  42. public function expand($data) {
  43. // Modification to make this a bit more performant (since gettype is very slow)
  44. if (! is_array($data)) {
  45. $data = (array)$data;
  46. }
  47. /*
  48. // Original code, which uses a slow gettype() statement, kept in place for if the assumption that is_array always works here is incorrect
  49. switch (gettype($data)) {
  50. case "boolean":
  51. case "integer":
  52. case "double":
  53. case "string":
  54. case "object":
  55. $data = (array)$data;
  56. break;
  57. }
  58. */
  59. // Resolve template vars
  60. preg_match_all('/\{([^\}]*)\}/', $this->template, $em);
  61. foreach ($em[1] as $i => $bare_expression) {
  62. preg_match('/^([\+\;\?\/\.]{1})?(.*)$/', $bare_expression, $lm);
  63. $exp = new StdClass();
  64. $exp->expression = $em[0][$i];
  65. $exp->operator = $lm[1];
  66. $exp->variable_list = $lm[2];
  67. $exp->varspecs = explode(',', $exp->variable_list);
  68. $exp->vars = array();
  69. foreach ($exp->varspecs as $varspec) {
  70. preg_match('/^([a-zA-Z0-9_]+)([\*\+]{1})?([\:\^][0-9-]+)?(\=[^,]+)?$/', $varspec, $vm);
  71. $var = new StdClass();
  72. $var->name = $vm[1];
  73. $var->modifier = isset($vm[2]) && $vm[2] ? $vm[2] : null;
  74. $var->modifier = isset($vm[3]) && $vm[3] ? $vm[3] : $var->modifier;
  75. $var->default = isset($vm[4]) ? substr($vm[4], 1) : null;
  76. $exp->vars[] = $var;
  77. }
  78. // Add processing flags
  79. $exp->reserved = false;
  80. $exp->prefix = '';
  81. $exp->delimiter = ',';
  82. switch ($exp->operator) {
  83. case '+':
  84. $exp->reserved = 'true';
  85. break;
  86. case ';':
  87. $exp->prefix = ';';
  88. $exp->delimiter = ';';
  89. break;
  90. case '?':
  91. $exp->prefix = '?';
  92. $exp->delimiter = '&';
  93. break;
  94. case '/':
  95. $exp->prefix = '/';
  96. $exp->delimiter = '/';
  97. break;
  98. case '.':
  99. $exp->prefix = '.';
  100. $exp->delimiter = '.';
  101. break;
  102. }
  103. $expressions[] = $exp;
  104. }
  105. // Expansion
  106. $this->expansion = $this->template;
  107. foreach ($expressions as $exp) {
  108. $part = $exp->prefix;
  109. $exp->one_var_defined = false;
  110. foreach ($exp->vars as $var) {
  111. $val = '';
  112. if ($exp->one_var_defined && isset($data[$var->name])) {
  113. $part .= $exp->delimiter;
  114. }
  115. // Variable present
  116. if (isset($data[$var->name])) {
  117. $exp->one_var_defined = true;
  118. $var->data = $data[$var->name];
  119. $val = self::val_from_var($var, $exp);
  120. // Variable missing
  121. } else {
  122. if ($var->default) {
  123. $exp->one_var_defined = true;
  124. $val = $var->default;
  125. }
  126. }
  127. $part .= $val;
  128. }
  129. if (! $exp->one_var_defined) $part = '';
  130. $this->expansion = str_replace($exp->expression, $part, $this->expansion);
  131. }
  132. return $this->expansion;
  133. }
  134. private function val_from_var($var, $exp) {
  135. $val = '';
  136. if (is_array($var->data)) {
  137. $i = 0;
  138. if ($exp->operator == '?' && ! $var->modifier) {
  139. $val .= $var->name . '=';
  140. }
  141. foreach ($var->data as $k => $v) {
  142. $del = $var->modifier ? $exp->delimiter : ',';
  143. $ek = rawurlencode($k);
  144. $ev = rawurlencode($v);
  145. // Array
  146. if ($k !== $i) {
  147. if ($var->modifier == '+') {
  148. $val .= $var->name . '.';
  149. }
  150. if ($exp->operator == '?' && $var->modifier || $exp->operator == ';' && $var->modifier == '*' || $exp->operator == ';' && $var->modifier == '+') {
  151. $val .= $ek . '=';
  152. } else {
  153. $val .= $ek . $del;
  154. }
  155. // List
  156. } else {
  157. if ($var->modifier == '+') {
  158. if ($exp->operator == ';' && $var->modifier == '*' || $exp->operator == ';' && $var->modifier == '+' || $exp->operator == '?' && $var->modifier == '+') {
  159. $val .= $var->name . '=';
  160. } else {
  161. $val .= $var->name . '.';
  162. }
  163. }
  164. }
  165. $val .= $ev . $del;
  166. $i ++;
  167. }
  168. $val = trim($val, $del);
  169. // Strings, numbers, etc.
  170. } else {
  171. if ($exp->operator == '?') {
  172. $val = $var->name . (isset($var->data) ? '=' : '');
  173. } else if ($exp->operator == ';') {
  174. $val = $var->name . ($var->data ? '=' : '');
  175. }
  176. $val .= rawurlencode($var->data);
  177. if ($exp->operator == '+') {
  178. $val = str_replace(self::$reserved_pct, self::$reserved, $val);
  179. }
  180. }
  181. return $val;
  182. }
  183. public function match($uri) {}
  184. public function __toString() {
  185. return $this->template;
  186. }
  187. }