-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathUrlHelper.php
109 lines (95 loc) · 2.7 KB
/
UrlHelper.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
<?php
/**
* ArangoDB PHP client: URL helper methods
*
* @package ArangoDBClient
* @author Jan Steemann
* @copyright Copyright 2012, triagens GmbH, Cologne, Germany
*/
namespace ArangoDBClient;
/**
* Some helper methods to construct and process URLs
*
* @package ArangoDBClient
* @since 0.2
*/
abstract class UrlHelper
{
/**
* Get the document id from a location header
*
* @param string $location - HTTP response location header
*
* @return string - document id parsed from header
*/
public static function getDocumentIdFromLocation($location)
{
if (!is_string($location)) {
// can't do anything about it if location is not even a string
return null;
}
if (0 === strpos($location, '/_db/')) {
// /_db/<dbname>/_api/document/<collection>/<key>
@list(, , , , , $collectionName, $id) = explode('/', $location);
} else {
// /_api/document/<collection>/<key>
@list(, , , $collectionName, $id) = explode('/', $location);
}
if (is_string($id)) {
$id = urldecode($id);
}
return $collectionName . '/' . $id;
}
/**
* Construct a URL from a base URL and additional parts, separated with '/' each
*
* This function accepts variable arguments.
*
* @param string $baseUrl - base URL
* @param array $parts - URL parts to append
*
* @return string - assembled URL
*/
public static function buildUrl($baseUrl, array $parts = [])
{
$url = $baseUrl;
foreach ($parts as $part) {
if (strpos($part, '/') !== false) {
@list(,$part) = explode('/', $part);
}
$url .= '/' . rawurlencode($part);
}
return $url;
}
/**
* Append parameters to a URL
*
* Parameter values will be URL-encoded
*
* @param string $baseUrl - base URL
* @param array $params - an array of parameters
*
* @return string - the assembled URL
*/
public static function appendParamsUrl($baseUrl, $params)
{
foreach ($params as $key => &$value) {
if (is_bool($value)) {
$value = self::getBoolString($value);
}
}
return $baseUrl . '?' . http_build_query($params);
}
/**
* Get a string from a boolean value
*
* @param mixed $value - the value
*
* @return string - "true" if $value evaluates to true, "false" otherwise
*/
public static function getBoolString($value)
{
return $value ? 'true' : 'false';
}
}
class_alias(UrlHelper::class, '\triagens\ArangoDb\UrlHelper');