Util.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace Luracast\Restler;
  3. /**
  4. * Describe the purpose of this class/interface/trait
  5. *
  6. * @category Framework
  7. * @package Restler
  8. * @author R.Arul Kumaran <arul@luracast.com>
  9. * @copyright 2010 Luracast
  10. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  11. * @link http://luracast.com/products/restler/
  12. *
  13. */
  14. class Util
  15. {
  16. /**
  17. * @var Restler instance injected at runtime
  18. */
  19. public static $restler;
  20. /**
  21. * verify if the given data type string is scalar or not
  22. *
  23. * @static
  24. *
  25. * @param string $type data type as string
  26. *
  27. * @return bool true or false
  28. */
  29. public static function isObjectOrArray($type)
  30. {
  31. if (is_array($type)) {
  32. foreach ($type as $t) {
  33. if (static::isObjectOrArray($t)) {
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. return !(boolean)strpos('|bool|boolean|int|float|string|', $type);
  40. }
  41. /**
  42. * Get the value deeply nested inside an array / object
  43. *
  44. * Using isset() to test the presence of nested value can give a false positive
  45. *
  46. * This method serves that need
  47. *
  48. * When the deeply nested property is found its value is returned, otherwise
  49. * false is returned.
  50. *
  51. * @param array|object $from array to extract the value from
  52. * @param string|array $key ... pass more to go deeply inside the array
  53. * alternatively you can pass a single array
  54. *
  55. * @return null|mixed null when not found, value otherwise
  56. */
  57. public static function nestedValue($from, $key/**, $key2 ... $key`n` */)
  58. {
  59. if (is_array($key)) {
  60. $keys = $key;
  61. } else {
  62. $keys = func_get_args();
  63. array_shift($keys);
  64. }
  65. foreach ($keys as $key) {
  66. if (is_array($from) && isset($from[$key])) {
  67. $from = $from[$key];
  68. continue;
  69. } elseif (is_object($from) && isset($from->{$key})) {
  70. $from = $from->{$key};
  71. continue;
  72. }
  73. return null;
  74. }
  75. return $from;
  76. }
  77. public static function getResourcePath(
  78. $className,
  79. $resourcePath = null,
  80. $prefix = ''
  81. ) {
  82. if (is_null($resourcePath)) {
  83. if (Defaults::$autoRoutingEnabled) {
  84. $resourcePath = strtolower($className);
  85. if (false !== ($index = strrpos($className, '\\'))) {
  86. $resourcePath = substr($resourcePath, $index + 1);
  87. }
  88. if (false !== ($index = strrpos($resourcePath, '_'))) {
  89. $resourcePath = substr($resourcePath, $index + 1);
  90. }
  91. } else {
  92. $resourcePath = '';
  93. }
  94. } else {
  95. $resourcePath = trim($resourcePath, '/');
  96. }
  97. if (strlen($resourcePath) > 0) {
  98. $resourcePath .= '/';
  99. }
  100. return $prefix . $resourcePath;
  101. }
  102. /**
  103. * Compare two strings and remove the common
  104. * sub string from the first string and return it
  105. *
  106. * @static
  107. *
  108. * @param string $fromPath
  109. * @param string $usingPath
  110. * @param string $char
  111. * optional, set it as
  112. * blank string for char by char comparison
  113. *
  114. * @return string
  115. */
  116. public static function removeCommonPath($fromPath, $usingPath, $char = '/')
  117. {
  118. if (empty($fromPath)) {
  119. return '';
  120. }
  121. $fromPath = explode($char, $fromPath);
  122. $usingPath = explode($char, $usingPath);
  123. while (count($usingPath)) {
  124. if (count($fromPath) && $fromPath[0] == $usingPath[0]) {
  125. array_shift($fromPath);
  126. } else {
  127. break;
  128. }
  129. array_shift($usingPath);
  130. }
  131. return implode($char, $fromPath);
  132. }
  133. /**
  134. * Compare two strings and split the common
  135. * sub string from the first string and return it as array
  136. *
  137. * @static
  138. *
  139. * @param string $fromPath
  140. * @param string $usingPath
  141. * @param string $char
  142. * optional, set it as
  143. * blank string for char by char comparison
  144. *
  145. * @return array with 2 strings first is the common string and second is the remaining in $fromPath
  146. */
  147. public static function splitCommonPath($fromPath, $usingPath, $char = '/')
  148. {
  149. if (empty($fromPath)) {
  150. return array('', '');
  151. }
  152. $fromPath = explode($char, $fromPath);
  153. $usingPath = explode($char, $usingPath);
  154. $commonPath = array();
  155. while (count($usingPath)) {
  156. if (count($fromPath) && $fromPath[0] == $usingPath[0]) {
  157. $commonPath [] = array_shift($fromPath);
  158. } else {
  159. break;
  160. }
  161. array_shift($usingPath);
  162. }
  163. return array(implode($char, $commonPath), implode($char, $fromPath));
  164. }
  165. /**
  166. * Parses the request to figure out the http request type
  167. *
  168. * @static
  169. *
  170. * @return string which will be one of the following
  171. * [GET, POST, PUT, PATCH, DELETE]
  172. * @example GET
  173. */
  174. public static function getRequestMethod()
  175. {
  176. $method = $_SERVER['REQUEST_METHOD'];
  177. if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
  178. $method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
  179. } elseif (
  180. !empty(Defaults::$httpMethodOverrideProperty)
  181. && isset($_REQUEST[Defaults::$httpMethodOverrideProperty])
  182. ) {
  183. // support for exceptional clients who can't set the header
  184. $m = strtoupper($_REQUEST[Defaults::$httpMethodOverrideProperty]);
  185. if ($m == 'PUT' || $m == 'DELETE' ||
  186. $m == 'POST' || $m == 'PATCH'
  187. ) {
  188. $method = $m;
  189. }
  190. }
  191. // support for HEAD request
  192. if ($method == 'HEAD') {
  193. $method = 'GET';
  194. }
  195. return $method;
  196. }
  197. /**
  198. * Pass any content negotiation header such as Accept,
  199. * Accept-Language to break it up and sort the resulting array by
  200. * the order of negotiation.
  201. *
  202. * @static
  203. *
  204. * @param string $accept header value
  205. *
  206. * @return array sorted by the priority
  207. */
  208. public static function sortByPriority($accept)
  209. {
  210. $acceptList = array();
  211. $accepts = explode(',', strtolower($accept));
  212. if (!is_array($accepts)) {
  213. $accepts = array($accepts);
  214. }
  215. foreach ($accepts as $pos => $accept) {
  216. $parts = explode(';', $accept);
  217. $type = trim(array_shift($parts));
  218. $parameters = [];
  219. foreach ($parts as $part) {
  220. $part = explode('=', $part);
  221. if (2 !== count($part)) {
  222. continue;
  223. }
  224. $key = strtolower(trim($part[0]));
  225. $parameters[$key] = trim($part[1], ' "');
  226. }
  227. $quality = isset($parameters['q']) ? (float)$parameters['q'] : (1000 - $pos) / 1000;
  228. $acceptList[$type] = $quality;
  229. }
  230. arsort($acceptList);
  231. return $acceptList;
  232. }
  233. public static function getShortName($className)
  234. {
  235. // @CHANGE LDR
  236. if (!is_string($className)) return;
  237. $className = explode('\\', $className);
  238. return end($className);
  239. }
  240. }