[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/restful/src/RESTful/ -> Query.php (source)

   1  <?php
   2  
   3  namespace RESTful;
   4  
   5  use RESTful\Exceptions\NoResultFound;
   6  use RESTful\Exceptions\MultipleResultsFound;
   7  
   8  class Query extends Itemization
   9  {
  10      public $filters = array(),
  11          $sorts = array(),
  12          $size;
  13  
  14      public function __construct($resource, $uri)
  15      {
  16          parent::__construct($resource, $uri);
  17          $this->size = $this->_size;
  18          $this->_parseUri($uri);
  19      }
  20  
  21      private function _parseUri($uri)
  22      {
  23          $parsed = parse_url($uri);
  24          $this->uri = $parsed['path'];
  25          if (array_key_exists('query', $parsed)) {
  26              foreach (explode('&', $parsed['query']) as $param) {
  27                  $param = explode('=', $param);
  28                  $key = urldecode($param[0]);
  29                  $val = (count($param) == 1) ? null : urldecode($param[1]);
  30  
  31                  // limit
  32                  if ($key == 'limit') {
  33                      $this->size = $this->_size = $val;
  34                  } // sorts
  35                  else if ($key == 'sort') {
  36                      array_push($this->sorts, $val);
  37                  } // everything else
  38                  else {
  39                      if (!array_key_exists($key, $this->filters)) {
  40                          $this->filters[$key] = array();
  41                      }
  42                      if (!is_array($val)) {
  43                          $val = array($val);
  44                      }
  45                      $this->filters[$key] = array_merge($this->filters[$key], $val);
  46                  }
  47              }
  48          }
  49      }
  50  
  51      protected function _buildUri($offset = null)
  52      {
  53          // params
  54          $params = array_merge(
  55              $this->filters,
  56              array(
  57                  'sort'   => $this->sorts,
  58                  'limit'  => $this->_size,
  59                  'offset' => ($offset == null) ? $this->_offset : $offset
  60              )
  61          );
  62          $getSingle = function ($v) {
  63              if (is_array($v) && count($v) == 1)
  64                  return $v[0];
  65              return $v;
  66          };
  67          $params = array_map($getSingle, $params);
  68  
  69          // url encode params
  70          // NOTE: http://stackoverflow.com/a/8171667/1339571
  71          $qs = http_build_query($params);
  72          $qs = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $qs);
  73  
  74          return $this->uri . '?' . $qs;
  75      }
  76  
  77      private function _reset()
  78      {
  79          $this->_page = null;
  80      }
  81  
  82      public function filter($expression)
  83      {
  84          if ($expression->op == '=') {
  85              $field = $expression->field;
  86          } else {
  87              $field = $expression->field . '[' . $expression->op . ']';
  88          }
  89          if (is_array($expression->val)) {
  90              $val = implode(',', $expression->val);
  91          } else {
  92              $val = $expression->val;
  93          }
  94          if (!array_key_exists($field, $this->filters)) {
  95              $this->filters[$field] = array();
  96          }
  97          array_push($this->filters[$field], $val);
  98          $this->_reset();
  99  
 100          return $this;
 101      }
 102  
 103      public function sort($expression)
 104      {
 105          $dir = $expression->ascending ? 'asc' : 'desc';
 106          array_push($this->sorts, $expression->field . ',' . $dir);
 107          $this->_reset();
 108  
 109          return $this;
 110      }
 111  
 112      public function limit($limit)
 113      {
 114          $this->size = $this->_size = $limit;
 115          $this->_reset();
 116  
 117          return $this;
 118      }
 119  
 120      public function all()
 121      {
 122          $items = array();
 123          foreach ($this as $item) {
 124              array_push($items, $item);
 125          }
 126  
 127          return $items;
 128      }
 129  
 130      public function first()
 131      {
 132          $prev_size = $this->_size;
 133          $this->_size = 1;
 134          $page = new Page($this->resource, $this->_buildUri());
 135          $this->_size = $prev_size;
 136          $item = count($page->items) != 0 ? $page->items[0] : null;
 137  
 138          return $item;
 139      }
 140  
 141      public function one()
 142      {
 143          $prev_size = $this->_size;
 144          $this->_size = 2;
 145          $page = new Page($this->resource, $this->_buildUri());
 146          $this->_size = $prev_size;
 147          if (count($page->items) == 1) {
 148              return $page->items[0];
 149          }
 150          if (count($page->items) == 0) {
 151              throw new NoResultFound();
 152          }
 153  
 154          throw new MultipleResultsFound();
 155      }
 156  
 157      public function paginate()
 158      {
 159          return new Pagination($this->resource, $this->_buildUri());
 160      }
 161  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1