<?php
namespace Moto\Json\Request; class Collection { protected $_page = 1; protected $_limit = 0; public $sort = null; public $filter = null; public function __construct($data = null) { if (null !== $data) $this->exchangeArray($data); } public function setPage($page) { $page = (int) $page; if ($page < 1) $page = 1; $this->_page = $page; return $this; } public function getPage() { return $this->_page; } public function setLimit($limit) { $limit = (int) $limit; if ($limit < 0) $limit = 1; $this->_limit = $limit; return $this; } public function getLimit() { return $this->_limit; } public function exchangeArray($data) { if (!empty($data['page'])) $this->setPage($data['page']); if (!empty($data['limit'])) $this->setLimit($data['limit']); } public function getOffset() { $offset = ($this->_page ? $this->_page - 1 : 0) * $this->_limit; return $offset; } public function isPagingActive() { return $this->_limit && $this->_page; } }