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