Overview

Namespaces

  • NGS
    • Client
      • Exception
    • Converter
    • Patterns
  • PHP

Classes

  • ApplicationProxy
  • CrudProxy
  • DomainProxy
  • HttpRequest
  • QueryString
  • ReportingProxy
  • RestHttp
  • StandardProxy
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: namespace NGS\Client;
  3: 
  4: use NGS\Converter\PrimitiveConverter;
  5: 
  6: require_once(__DIR__.'/../Converter/PrimitiveConverter.php');
  7: 
  8: /**
  9:  * Serializes various parameters into URL query string
 10:  *
 11:  * @package NGS\Client
 12:  */
 13: abstract class QueryString
 14: {
 15:     /**
 16:      * Serializes cube parameters into a query string
 17:      *
 18:      * @param array $dimensions
 19:      * @param array $facts
 20:      * @param array $order
 21:      * @return string
 22:      * @throws \InvalidArgumentException
 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:      * Serializes limit and offset to URL query string
 55:      *
 56:      * @param $limit string|int|float
 57:      * @param $offset string|int|float
 58:      * @return string
 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:      * Serializes limit, offset and order to URL query string
 77:      *
 78:      * @param $limit string|int|float
 79:      * @param $offset string|int|float
 80:      * @param $order array Array of string=>bool pairs, where key is property_name,
 81:      * and value is true for ascending, and false for descending order
 82:      * <code>
 83:      * $order = array(
 84:      *     'name' => true, // order by ascending name
 85:      *     'surname' => false // order by descending surname)
 86:      * );
 87:      * </code>
 88:      * @return string
 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: 
API documentation generated by ApiGen 2.8.0