getOffset() public method

Get number of offset.
public getOffset ( integer | null $perPage = null ) : integer
$perPage integer | null
return integer
Example #1
0
 public function testGetOffsetParsesThePageNumber()
 {
     $parameters = new Parameters(['page' => ['number' => 2]]);
     $this->assertEquals(20, $parameters->getOffset(20));
 }
Example #2
0
 public function processAction($args)
 {
     $document = new Document();
     $element = NULL;
     $meta = array();
     try {
         switch ($args['action']) {
             case "init_repository":
                 $this->initRepository($args['psw']);
                 $session = $this->getSession();
                 $element = new Resource($session, new SessionSerializer());
                 break;
             case "log_in":
                 $this->logIn($args['psw']);
                 $session = $this->getSession();
                 $element = new Resource($session, new SessionSerializer());
                 break;
             case "log_out":
                 $this->logOut();
                 $session = $this->getSession();
                 $element = new Resource($session, new SessionSerializer());
                 break;
             case "get_session":
                 $session = $this->getSession();
                 $element = new Resource($session, new SessionSerializer());
                 break;
             case "get_post":
                 $postId = isset($args['id']) ? $args['id'] : NULL;
                 $post = $this->getPost($postId);
                 $element = (new Resource($post, new PostSerializer()))->fields(['tags']);
                 break;
             case "get_posts":
                 $parameters = new Parameters($args);
                 $sort = $parameters->getSort();
                 if ($sort == NULL) {
                     $sort = array();
                 }
                 $limit = $parameters->getLimit();
                 if ($limit == NULL) {
                     $limit = -1;
                 }
                 $offset = $parameters->getOffset();
                 if ($offset == NULL) {
                     $offset = 0;
                 }
                 $posts = $this->getPosts($sort, $limit, $offset);
                 $element = (new Collection($posts, new PostSerializer()))->fields(['tags']);
                 break;
             case "publish_post":
                 $id = $this->publishPost($args['title'], $args['body'], $args['tags'], $args['draft'], $args['token']);
                 $element = new Resource(new stdClass(), new ResultSerializer());
                 $document->addMeta("id", $id);
                 break;
             case "update_post":
                 $id = $this->updatePost($args['id'], $args['title'], $args['body'], $args['tags'], $args['draft'], $args['token']);
                 $element = new Resource(new stdClass(), new ResultSerializer());
                 $document->addMeta("id", $id);
                 break;
             case "delete_post":
                 $this->deletePost($args['id'], $args['token']);
                 $element = new Resource(new stdClass(), new ResultSerializer());
                 break;
             default:
                 throw new Exception("unknown action " . $args['action'], self::EBL_ERROR_BADREQUEST);
                 break;
         }
     } catch (Exception $e) {
         http_response_code($e->getCode());
         $document->setErrors(array(array("message" => $e->getMessage())));
     }
     if ($element != NULL) {
         $document->setData($element);
     }
     echo json_encode($document->toArray());
 }