Commit 139720fb authored by guilhermeblanco's avatar guilhermeblanco

CHG: Updated Doctrine_Pager to become coding standards compliant

parent 05147fbe
...@@ -33,84 +33,90 @@ ...@@ -33,84 +33,90 @@
*/ */
class Doctrine_Pager class Doctrine_Pager
{ {
/** /**
* @var Doctrine_Query $query Doctrine_Query object related to the pager * @var Doctrine_Query $query Doctrine_Query object related to the pager
*/ */
protected $query; protected $query;
/** /**
* @var int $nbResults Number of results found * @var int $nbResults Number of results found
*/ */
protected $nbResults; protected $nbResults;
/** /**
* @var int $maxPerPage Maximum number of itens per page * @var int $maxPerPage Maximum number of itens per page
*/ */
protected $maxPerPage; protected $maxPerPage;
/** /**
* @var int $page Current page * @var int $page Current page
*/ */
protected $page; protected $page;
/** /**
* @var int $lastPage Last page (total of pages) * @var int $lastPage Last page (total of pages)
*/ */
protected $lastPage; protected $lastPage;
/** /**
* __construct * __construct
* *
* @param mixed $query Accepts either a Doctrine_Query object or a string (which does the Doctrine_Query class creation). * @param mixed $query Accepts either a Doctrine_Query object or a string
* (which does the Doctrine_Query class creation).
* @param int $page Current page * @param int $page Current page
* @param int $maxPerPage Maximum itens per page * @param int $maxPerPage Maximum itens per page
* @return void * @return void
*/ */
public function __construct( $query, $page, $maxPerPage = 0 ) public function __construct( $query, $page, $maxPerPage = 0 )
{ {
$this->setQuery($query); $this->setQuery($query);
$this->_setMaxPerPage($maxPerPage); $this->_setMaxPerPage($maxPerPage);
$this->_setPage($page); $this->_setPage($page);
$this->initialize(); $this->initialize();
} }
/** /**
* initialize * initialize
* *
* Initialize Pager object calculating number of results * Initialize Pager object calculating number of results
* *
* @return void * @return void
*/ */
protected function initialize() protected function initialize()
{ {
// etrieve the number of itens found
$count = $this->getQuery()->offset(0)->limit(0)->count(); $count = $this->getQuery()->offset(0)->limit(0)->count();
$this->setNbResults($count); $this->setNbResults($count);
$this->adjustOffset(); $this->adjustOffset();
} }
/** /**
* adjustOffset * adjustOffset
* *
* Adjusts last page of Doctrine_Pager, offset and limit of Doctrine_Query associated * Adjusts last page of Doctrine_Pager, offset and limit of Doctrine_Query associated
* *
* @return void * @return void
*/ */
protected function adjustOffset() protected function adjustOffset()
{ {
$this->setLastPage(max(1, ceil($this->getNbResults() / $this->getMaxPerPage()))); // Define new total of pages
$offset = ($this->getPage() - 1) * $this->getMaxPerPage(); $this->setLastPage(
max(1, ceil($this->getNbResults() / $this->getMaxPerPage()))
$p = $this->getQuery(); );
$p->offset($offset); $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
$p->limit($this->getMaxPerPage());
} // Assign new offset and limit to Doctrine_Query object
$p = $this->getQuery();
$p->offset($offset);
$p->limit($this->getMaxPerPage());
}
/** /**
...@@ -120,10 +126,10 @@ class Doctrine_Pager ...@@ -120,10 +126,10 @@ class Doctrine_Pager
* *
* @return int the number of results found * @return int the number of results found
*/ */
public function getNbResults() public function getNbResults()
{ {
return $this->nbResults; return $this->nbResults;
} }
/** /**
...@@ -134,10 +140,10 @@ class Doctrine_Pager ...@@ -134,10 +140,10 @@ class Doctrine_Pager
* @param $nb Number of results found on initial query fetch * @param $nb Number of results found on initial query fetch
* @return void * @return void
*/ */
protected function setNbResults($nb) protected function setNbResults($nb)
{ {
$this->nbResults = $nb; $this->nbResults = $nb;
} }
/** /**
...@@ -147,10 +153,10 @@ class Doctrine_Pager ...@@ -147,10 +153,10 @@ class Doctrine_Pager
* *
* @return int first page * @return int first page
*/ */
public function getFirstPage() public function getFirstPage()
{ {
return 1; return 1;
} }
/** /**
...@@ -160,10 +166,10 @@ class Doctrine_Pager ...@@ -160,10 +166,10 @@ class Doctrine_Pager
* *
* @return int last page (total of pages) * @return int last page (total of pages)
*/ */
public function getLastPage() public function getLastPage()
{ {
return $this->lastPage; return $this->lastPage;
} }
/** /**
...@@ -174,15 +180,15 @@ class Doctrine_Pager ...@@ -174,15 +180,15 @@ class Doctrine_Pager
* @param $page last page (total of pages) * @param $page last page (total of pages)
* @return void * @return void
*/ */
protected function setLastPage($page) protected function setLastPage($page)
{ {
$this->lastPage = $page; $this->lastPage = $page;
if ($this->getPage() > $page) if ($this->getPage() > $page)
{ {
$this->_setPage($page); $this->_setPage($page);
} }
} }
/** /**
...@@ -192,10 +198,10 @@ class Doctrine_Pager ...@@ -192,10 +198,10 @@ class Doctrine_Pager
* *
* @return int current page * @return int current page
*/ */
public function getPage() public function getPage()
{ {
return $this->page; return $this->page;
} }
/** /**
...@@ -205,10 +211,10 @@ class Doctrine_Pager ...@@ -205,10 +211,10 @@ class Doctrine_Pager
* *
* @return int next page * @return int next page
*/ */
public function getNextPage() public function getNextPage()
{ {
return min($this->getPage() + 1, $this->getLastPage()); return min($this->getPage() + 1, $this->getLastPage());
} }
/** /**
...@@ -218,26 +224,26 @@ class Doctrine_Pager ...@@ -218,26 +224,26 @@ class Doctrine_Pager
* *
* @return int previous page * @return int previous page
*/ */
public function getPreviousPage() public function getPreviousPage()
{ {
return max($this->getPage() - 1, $this->getFirstPage()); return max($this->getPage() - 1, $this->getFirstPage());
} }
/** /**
* haveToPaginate * haveToPaginate
* *
* Return true if it's necessary to paginate or false if not * Return true if it's necessary to paginate or false if not
* *
* @return bool true if it is necessary to paginate, false otherwise * @return bool true if it is necessary to paginate, false otherwise
*/ */
public function haveToPaginate() public function haveToPaginate()
{ {
return $this->getNbResults() > $this->getMaxPerPage(); return $this->getNbResults() > $this->getMaxPerPage();
} }
/** /**
* setPage * setPage
* *
* Defines the current page and automatically adjust offset and limits * Defines the current page and automatically adjust offset and limits
...@@ -245,11 +251,11 @@ class Doctrine_Pager ...@@ -245,11 +251,11 @@ class Doctrine_Pager
* @param $page current page * @param $page current page
* @return void * @return void
*/ */
public function setPage($page) public function setPage($page)
{ {
$this->_setPage($page); $this->_setPage($page);
$this->adjustOffset(); $this->adjustOffset();
} }
/** /**
...@@ -260,11 +266,11 @@ class Doctrine_Pager ...@@ -260,11 +266,11 @@ class Doctrine_Pager
* @param $page current page * @param $page current page
* @return void * @return void
*/ */
private function _setPage($page) private function _setPage($page)
{ {
$page = intval($page); $page = intval($page);
$this->page = ($page <= 0) ? 1 : $page; $this->page = ($page <= 0) ? 1 : $page;
} }
/** /**
...@@ -274,13 +280,13 @@ class Doctrine_Pager ...@@ -274,13 +280,13 @@ class Doctrine_Pager
* *
* @return int maximum number of itens per page * @return int maximum number of itens per page
*/ */
public function getMaxPerPage() public function getMaxPerPage()
{ {
return $this->maxPerPage; return $this->maxPerPage;
} }
/** /**
* setMaxPerPage * setMaxPerPage
* *
* Defines the maximum number of itens per page and automatically adjust offset and limits * Defines the maximum number of itens per page and automatically adjust offset and limits
...@@ -288,11 +294,11 @@ class Doctrine_Pager ...@@ -288,11 +294,11 @@ class Doctrine_Pager
* @param $max maximum number of itens per page * @param $max maximum number of itens per page
* @return void * @return void
*/ */
public function setMaxPerPage($max) public function setMaxPerPage($max)
{ {
$this->_setMaxPerPage($max); $this->_setMaxPerPage($max);
$this->adjustOffset(); $this->adjustOffset();
} }
/** /**
...@@ -303,16 +309,16 @@ class Doctrine_Pager ...@@ -303,16 +309,16 @@ class Doctrine_Pager
* @param $max maximum number of itens per page * @param $max maximum number of itens per page
* @return void * @return void
*/ */
private function _setMaxPerPage($max) private function _setMaxPerPage($max)
{ {
if ($max > 0) { if ($max > 0) {
$this->maxPerPage = $max; $this->maxPerPage = $max;
} else if ($max == 0) { } else if ($max == 0) {
$this->maxPerPage = 25; $this->maxPerPage = 25;
} else { } else {
$this->maxPerPage = abs($max); $this->maxPerPage = abs($max);
} }
} }
/** /**
...@@ -322,10 +328,10 @@ class Doctrine_Pager ...@@ -322,10 +328,10 @@ class Doctrine_Pager
* *
* @return Doctrine_Query Doctrine_Query object related to the pager * @return Doctrine_Query Doctrine_Query object related to the pager
*/ */
public function getQuery() public function getQuery()
{ {
return $this->query; return $this->query;
} }
/** /**
...@@ -333,17 +339,18 @@ class Doctrine_Pager ...@@ -333,17 +339,18 @@ class Doctrine_Pager
* *
* Defines the maximum number of itens per page * Defines the maximum number of itens per page
* *
* @param $query Accepts either a Doctrine_Query object or a string (which does the Doctrine_Query class creation). * @param $query Accepts either a Doctrine_Query object or a string
* (which does the Doctrine_Query class creation).
* @return void * @return void
*/ */
protected function setQuery($query) protected function setQuery($query)
{ {
if (is_string($query)) { if (is_string($query)) {
$query = Doctrine_Query::create()->from($query); $query = Doctrine_Query::create()->from($query);
} }
$this->query = $query; $this->query = $query;
} }
/** /**
...@@ -351,13 +358,12 @@ class Doctrine_Pager ...@@ -351,13 +358,12 @@ class Doctrine_Pager
* executes the query and populates the data set * executes the query and populates the data set
* *
* @param $params Optional parameters to Doctrine_Query::execute * @param $params Optional parameters to Doctrine_Query::execute
* @param $hydrationMode Hyddration Mode of Doctrine_Query::execute returned ResultSet. Doctrine::Default is FETCH_RECORD * @param $hydrationMode Hyddration Mode of Doctrine_Query::execute
* returned ResultSet. Doctrine::Default is FETCH_RECORD
* @return Doctrine_Collection the root collection * @return Doctrine_Collection the root collection
*/ */
public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD)
{ {
return $this->getQuery()->execute($params, $hydrationMode); return $this->getQuery()->execute($params, $hydrationMode);
} }
} }
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment