1: <?php
2: namespace NGS\Client;
3:
4: use NGS\Converter\PrimitiveConverter;
5:
6: require_once(__DIR__.'/../Converter/PrimitiveConverter.php');
7:
8: 9: 10: 11: 12:
13: abstract class QueryString
14: {
15: 16: 17: 18: 19: 20: 21: 22: 23:
24: public static function prepareCubeCall(array $dimensions, array $facts, array $order)
25: {
26: $params = array();
27:
28: if ($dimensions) {
29: $params[] = 'dimensions='.implode(',', array_map(function($a){ return rawurlencode($a); }, $dimensions));
30: }
31: if ($facts) {
32: $params[] = 'facts='.implode(',', array_map(function($a){ return rawurlencode($a); }, $facts));
33: }
34: if ($order) {
35: array_walk($order, function(&$el, $key) {
36: if (is_int($key)) {
37: $el = rawurlencode($el);
38: } elseif (is_bool($el)) {
39: $el = ($el ? '' : '-') . rawurlencode($key);
40: } else {
41: throw new \InvalidArgumentException('Cube order was not a string array or a string=>bool array');
42: }
43: });
44: $params[] = 'order='.implode(',', $order);
45: }
46: if (!$params) {
47: throw new \InvalidArgumentException('Cube must have at least one argument');
48: }
49: return implode('&', $params);
50: }
51:
52:
53: 54: 55: 56: 57: 58: 59:
60: public static function formatLimitAndOffset($limit, $offset)
61: {
62: $limit = $limit === null ? null : PrimitiveConverter::toInteger($limit);
63: $offset = $offset === null ? null : PrimitiveConverter::toInteger($offset);
64:
65: $args = array();
66:
67: if($limit)
68: $args[] = 'limit='.$limit;
69: if($offset)
70: $args[] = 'offset='.$offset;
71:
72: return $args ? '?'.implode('&', $args) : '';
73: }
74:
75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89:
90: public static function formatLimitAndOffsetAndOrder($limit, $offset, array $order=null)
91: {
92: $limit = $limit === null ? null : PrimitiveConverter::toInteger($limit);
93: $offset = $offset === null ? null : PrimitiveConverter::toInteger($offset);
94:
95: $args = array();
96:
97: if ($limit) {
98: $args[] = 'limit='.$limit;
99: }
100: if ($offset) {
101: $args[] = 'offset='.$offset;
102: }
103: if ($order) {
104: array_walk($order, function(&$v, $k) { $v = $v ? $k : '-'.$k; });
105: $args[] = 'order='.implode (',', $order);
106: }
107:
108: return implode('&', $args);
109: }
110: }
111: