Exemple #1
0
 private function render($resource, $offset, $limit)
 {
     if ($resource instanceof ORM) {
         $json = new stdClass();
         $json->meta = new stdClass();
         $json->objects = array();
         $prefixSource = ORM::factory("DailyPhoto")->where("day", "<", date("Y-m-d"));
         $items = !empty($resource->id) ? array($resource) : $resource->find_all();
         $allItems = !empty($resource->id) ? 1 : $prefixSource->count_all();
         foreach ($items as $item) {
             if ($item instanceof Model_DailyPhoto) {
                 $json->objects[] = $this->formatPhoto($item->photo->as_array(), $item->photo);
             } else {
                 if ($item instanceof Model_Photo) {
                     $json->objects[] = $this->formatPhoto($item->as_array(), $item);
                 } else {
                     if ($item instanceof ORM) {
                         $json->objects[] = $item->as_array();
                     } else {
                         $json->objects[] = $item;
                     }
                 }
             }
         }
         $json->meta->prev = null;
         $json->meta->next = null;
         $json->meta->offset = $offset;
         $json->meta->limit = $limit;
         $json->meta->total_count = $allItems;
         if ($offset + $limit < $json->meta->total_count) {
             $dt = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
             $dt['offset'] = $offset + $limit;
             $json->meta->next = Helper_Domain::getCleanURI() . $this->implodeGets($dt);
         }
         if ($offset > 0) {
             $dt = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
             $dt['offset'] = $offset - $limit;
             $json->meta->prev = Helper_Domain::getCleanURI() . $this->implodeGets($dt);
         }
     } else {
         $json = $resource;
     }
     echo json_encode($json);
 }
Exemple #2
0
 public function render($resource)
 {
     if (is_a($resource, 'ORM')) {
         $json = new stdClass();
         $json->meta = new stdClass();
         $json->objects = array();
         $limit = filter_var(Arr::get($_GET, 'limit', 30), FILTER_SANITIZE_NUMBER_INT);
         $offset = filter_var(Arr::get($_GET, 'offset', 0), FILTER_SANITIZE_NUMBER_INT);
         $prefixSource = clone $resource;
         $items = $resource->limit($limit)->offset($offset)->find_all();
         $allItems = $prefixSource->count_all();
         foreach ($items as $item) {
             if ($item instanceof Model_Photo) {
                 $json->objects[] = $this->formatPhoto($item->as_array(), $item);
             } else {
                 if ($item instanceof ORM) {
                     $json->objects[] = $item->as_array();
                 } else {
                     $json->objects[] = $item;
                 }
             }
         }
         $json->meta->prev = null;
         $json->meta->next = null;
         $json->meta->offset = $offset;
         $json->meta->limit = $limit;
         $json->meta->total_count = $allItems;
         if ($offset + $limit < $json->meta->total_count) {
             $dt = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
             $dt['offset'] = $offset + $limit;
             $json->meta->next = Helper_Domain::getCleanURI() . $this->implodeGets($dt);
         }
         if ($offset > 0) {
             $dt = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
             $dt['offset'] = $offset - $limit;
             $json->meta->prev = Helper_Domain::getCleanURI() . $this->implodeGets($dt);
         }
     } else {
         $json = $resource;
     }
     echo json_encode($json);
 }