Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function handle($annotation)
 {
     parent::handle($annotation);
     $maxLimit = $annotation->maxLimit;
     $limit = $annotation->limit;
     $offset = $annotation->offset;
     $range = $this->req->query->get('range');
     if (1 === preg_match('#^([0-9]+)-([0-9]+)$#', $range, $matches)) {
         list($range, $offset, $limit) = $matches;
         if ($limit < $offset) {
             throw new RestHttpException("Range offset cannot be greater than limit ({$range}).", 400);
         }
         $limit = $limit - $offset + 1;
         if ($maxLimit < $limit) {
             throw new RestHttpException("Range limit cannot exceed {$maxLimit} ({$range}).", 400);
         }
     }
     $this->req->attributes->add(['limit' => (int) $limit, 'offset' => (int) $offset, 'maxLimit' => (int) $maxLimit]);
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function handle($annotation)
 {
     parent::handle($annotation);
     $sanitize = true === $annotation->sanitize;
     $accepted = false === $annotation->accepted ? false : (array) $annotation->accepted;
     $criteria = [];
     foreach ($this->req->query->all() as $key => $value) {
         if (in_array($key, self::RESERVED_WORDS)) {
             continue;
         }
         if (false !== $accepted && !in_array($key, $accepted)) {
             throw new RestHttpException(sprintf("You are not allowed to filter by \"{$key}\". Available: %s.", 0 === count($accepted) ? 'none' : implode(', ', $accepted)));
         }
         if (false !== strpos($value, ',')) {
             $value = new OrArray(explode(',', $value));
         } elseif (false !== strpos($value, ';')) {
             $value = new AndArray(explode(';', $value));
         }
         $criteria[$sanitize ? $this->sanitizeFieldName($key) : $key] = $value;
     }
     $this->req->attributes->add(['criteria' => $criteria]);
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function handle($annotation)
 {
     parent::handle($annotation);
     $sanitize = true === $annotation->sanitize;
     $accepted = false === $annotation->accepted ? false : (array) $annotation->accepted;
     $defaultSort = (array) $annotation->sort;
     $defaultDesc = (array) $annotation->desc;
     $sort = $this->req->query->get('sort');
     if (is_string($sort)) {
         $sort = explode(',', $sort);
     } else {
         $sort = $defaultSort;
     }
     if (false !== $accepted) {
         foreach ($sort as $name) {
             if (!in_array($name, $accepted)) {
                 throw new RestHttpException(sprintf("You are not allowed to sort by \"{$name}\". Available: %s.", 0 === count($accepted) ? 'none' : implode(', ', $accepted)));
             }
         }
     }
     $sort = $sanitize ? array_map([$this, 'sanitizeFieldName'], $sort) : $sort;
     $sort = array_fill_keys($sort, 'asc');
     $desc = $this->req->query->get('desc');
     if (is_string($desc)) {
         $desc = explode(',', $desc);
     } else {
         $desc = $defaultDesc;
     }
     $desc = $sanitize ? array_map([$this, 'sanitizeFieldName'], $desc) : $desc;
     foreach ($desc as $field) {
         if (isset($sort[$field])) {
             $sort[$field] = 'desc';
         } else {
             throw new RestHttpException("Cannot order by '{$field}' desc cause it is missing from sort list.");
         }
     }
     $this->req->attributes->add(['sort' => $sort]);
 }