Ejemplo n.º 1
0
 public function execute(Request $req, Response $res, callable $next = null)
 {
     try {
         $collection = new \Liba\Collection($this->boot()->get($name));
         $params = $req->getQueryParams();
         /*
         $filter = isset($params['filter']) ? $params['filter'] : null;
         $order = isset($params['order']) ? $params['order'] : null;
         $offset = isset($params['offset']) ? $params['offset'] : null;
         $limit = isset($params['limit']) ? $params['limit'] : null;
         $result = new \Platform\Cursor($collection->query($filter, $order, $offset, $limit));
         */
         $limit = 10;
         $offset = 0;
         $order = null;
         $filter = null;
         foreach ($params as $param => $value) {
             if (preg_match("/limit\\((\\d+)\\)/", $param, $m)) {
                 $limit = $m[1];
             } elseif (preg_match("/limit\\((\\d+),(\\d+)\\)/", $param, $m)) {
                 $limit = $m[1];
                 $offset = $m[2];
             } elseif (preg_match("/sort\\(([_|-])(.*)\\)/", $param, $m)) {
                 $order = "{$m[2]} " . ($m[1] == '-' ? 'DESC' : 'ASC');
             } else {
                 $w = "`{$param}` = '{$value}'";
             }
         }
         $count = $collection->count();
         $result = new \Platform\Cursor($collection->query($w, $order, $offset, $limit));
         $start = $offset;
         $stop = intval($offset) + intval($limit);
         $reponse = new JsonResponse(['status' => 'ok', '__params' => $params, '__where' => $w, 'data' => $result->map(function (\Liba\Collection\Item $item) {
             $item->labels = $item->getCollection()->labels($item->id);
             return $item->getData();
         })->process()]);
         $reponse = $reponse->withHeader('Content-Range', "items {$start}-{$stop}/{$count}");
         return $next($req, $reponse);
     } catch (\Exception $ex) {
         return new JsonResponse(['status' => 'error', 'error' => $ex->getMessage()], 500);
     }
 }
Ejemplo n.º 2
0
 public function execute(Request $req, Response $res, callable $next = null)
 {
     try {
         $name = urldecode($req->getAttribute('name'));
         $params = $req->getQueryParams();
         $collection = new \Liba\Collection(new Type($name));
         $limit = null;
         $offset = 0;
         $order = null;
         $filter = null;
         $w = [];
         foreach ($params as $param => $value) {
             if (preg_match("/limit\\((\\d+)\\)/", $param, $m)) {
                 $limit = $m[1];
             } elseif (preg_match("/limit\\((\\d+),(\\d+)\\)/", $param, $m)) {
                 $limit = $m[1];
                 $offset = $m[2];
             } elseif (preg_match("/sort\\(([_|-])(.*)\\)/", $param, $m)) {
                 $order = "{$m[2]} " . ($m[1] == '-' ? 'DESC' : 'ASC');
             } else {
                 $w[] = "`{$param}` = '{$value}'";
             }
         }
         $count = $collection->filter($w)->count();
         $result = $collection->query($w, $order, $offset, $limit);
         $start = $offset;
         $stop = intval($offset) + intval($limit);
         $reponse = new JsonResponse(['status' => 'ok', '__collection' => $collection->getType()->getEntityType(), '__params' => $params, '__where' => $w, 'data' => $result->map(function (\Liba\Collection\Item $item) {
             $item->set('@labels', $item->getLabels());
             $item->set('@properties', $item->getProperties());
             return $item->getData();
         })->process()]);
         $reponse = $reponse->withHeader('Content-Range', "items {$start}-{$stop}/{$count}");
         return $next($req, $reponse);
     } catch (\Exception $ex) {
         return new JsonResponse(['__collection' => $collection->getType()->getEntityType(), '__params' => $params, '__where' => $w, 'status' => 'error', 'error' => $ex->getMessage()], 500);
     }
 }