1 <?php
2
3 /**
4 * ArangoDB PHP client: URL helper methods
5 *
6 * @package triagens\ArangoDb
7 * @author Jan Steemann
8 * @copyright Copyright 2012, triagens GmbH, Cologne, Germany
9 */
10
11 namespace triagens\ArangoDb;
12
13 /**
14 * Some helper methods to construct and process URLs
15 *
16 * @package triagens\ArangoDb
17 * @since 0.2
18 */
19 abstract class UrlHelper
20 {
21 /**
22 * Get the document id from a location header
23 *
24 * @param string $location - HTTP response location header
25 *
26 * @return string - document id parsed from header
27 */
28 public static function getDocumentIdFromLocation($location)
29 {
30 if (!is_string($location)) {
31 // can't do anything about it if location is not even a string
32 return null;
33 }
34
35 if (0 === strpos($location, '/_db/')) {
36 // /_db/<dbname>/_api/document/<collection>/<key>
37 @list(, , , , , , $id) = explode('/', $location);
38 } else {
39 // /_api/document/<collection>/<key>
40 @list(, , , , $id) = explode('/', $location);
41 }
42
43 if (is_string($id)) {
44 $id = urldecode($id);
45 }
46
47 return $id;
48 }
49
50 /**
51 * Construct a URL from a base URL and additional parts, separated with '/' each
52 *
53 * This function accepts variable arguments.
54 *
55 * @param string $baseUrl - base URL
56 * @param array $parts - URL parts to append
57 *
58 * @return string - assembled URL
59 */
60 public static function buildUrl($baseUrl, array $parts = [])
61 {
62 $url = $baseUrl;
63
64 foreach ($parts as $part) {
65 $url .= '/' . urlencode($part);
66 }
67
68 return $url;
69 }
70
71 /**
72 * Append parameters to a URL
73 *
74 * Parameter values will be URL-encoded
75 *
76 * @param string $baseUrl - base URL
77 * @param array $params - an array of parameters
78 *
79 * @return string - the assembled URL
80 */
81 public static function appendParamsUrl($baseUrl, $params)
82 {
83 foreach ($params as $key => &$value) {
84 if (is_bool($value)) {
85 $value = self::getBoolString($value);
86 }
87 }
88
89 return $baseUrl . '?' . http_build_query($params);
90 }
91
92 /**
93 * Get a string from a boolean value
94 *
95 * @param mixed $value - the value
96 *
97 * @return string - "true" if $value evaluates to true, "false" otherwise
98 */
99 public static function getBoolString($value)
100 {
101 return $value ? 'true' : 'false';
102 }
103 }
104