1
0
mirror of synced 2025-01-17 22:11:41 +03:00

CHG: Updated Doctrine_Pager to become coding standards compliant

This commit is contained in:
guilhermeblanco 2007-11-25 19:09:51 +00:00
parent 05147fbeb6
commit 139720fb9f

View File

@ -33,84 +33,90 @@
*/
class Doctrine_Pager
{
/**
/**
* @var Doctrine_Query $query Doctrine_Query object related to the pager
*/
protected $query;
/**
protected $query;
/**
* @var int $nbResults Number of results found
*/
protected $nbResults;
protected $nbResults;
/**
* @var int $maxPerPage Maximum number of itens per page
*/
protected $maxPerPage;
/**
protected $maxPerPage;
/**
* @var int $page Current page
*/
protected $page;
/**
protected $page;
/**
* @var int $lastPage Last page (total of pages)
*/
protected $lastPage;
protected $lastPage;
/**
* __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 $maxPerPage Maximum itens per page
* @return void
*/
public function __construct( $query, $page, $maxPerPage = 0 )
{
$this->setQuery($query);
public function __construct( $query, $page, $maxPerPage = 0 )
{
$this->setQuery($query);
$this->_setMaxPerPage($maxPerPage);
$this->_setPage($page);
$this->_setMaxPerPage($maxPerPage);
$this->_setPage($page);
$this->initialize();
}
$this->initialize();
}
/**
* initialize
*
* Initialize Pager object calculating number of results
*
* @return void
*/
protected function initialize()
{
/**
* initialize
*
* Initialize Pager object calculating number of results
*
* @return void
*/
protected function initialize()
{
// etrieve the number of itens found
$count = $this->getQuery()->offset(0)->limit(0)->count();
$this->setNbResults($count);
$this->adjustOffset();
}
/**
* adjustOffset
*
* Adjusts last page of Doctrine_Pager, offset and limit of Doctrine_Query associated
*
* @return void
*/
protected function adjustOffset()
{
$this->setLastPage(max(1, ceil($this->getNbResults() / $this->getMaxPerPage())));
$offset = ($this->getPage() - 1) * $this->getMaxPerPage();
$this->setNbResults($count);
$p = $this->getQuery();
$p->offset($offset);
$p->limit($this->getMaxPerPage());
}
$this->adjustOffset();
}
/**
* adjustOffset
*
* Adjusts last page of Doctrine_Pager, offset and limit of Doctrine_Query associated
*
* @return void
*/
protected function adjustOffset()
{
// Define new total of pages
$this->setLastPage(
max(1, ceil($this->getNbResults() / $this->getMaxPerPage()))
);
$offset = ($this->getPage() - 1) * $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
*
* @return int the number of results found
*/
public function getNbResults()
{
return $this->nbResults;
}
public function getNbResults()
{
return $this->nbResults;
}
/**
@ -134,10 +140,10 @@ class Doctrine_Pager
* @param $nb Number of results found on initial query fetch
* @return void
*/
protected function setNbResults($nb)
{
$this->nbResults = $nb;
}
protected function setNbResults($nb)
{
$this->nbResults = $nb;
}
/**
@ -147,10 +153,10 @@ class Doctrine_Pager
*
* @return int first page
*/
public function getFirstPage()
{
return 1;
}
public function getFirstPage()
{
return 1;
}
/**
@ -160,10 +166,10 @@ class Doctrine_Pager
*
* @return int last page (total of pages)
*/
public function getLastPage()
{
return $this->lastPage;
}
public function getLastPage()
{
return $this->lastPage;
}
/**
@ -174,15 +180,15 @@ class Doctrine_Pager
* @param $page last page (total of pages)
* @return void
*/
protected function setLastPage($page)
{
$this->lastPage = $page;
protected function setLastPage($page)
{
$this->lastPage = $page;
if ($this->getPage() > $page)
{
$this->_setPage($page);
}
}
if ($this->getPage() > $page)
{
$this->_setPage($page);
}
}
/**
@ -192,10 +198,10 @@ class Doctrine_Pager
*
* @return int current page
*/
public function getPage()
{
return $this->page;
}
public function getPage()
{
return $this->page;
}
/**
@ -205,10 +211,10 @@ class Doctrine_Pager
*
* @return int next page
*/
public function getNextPage()
{
return min($this->getPage() + 1, $this->getLastPage());
}
public function getNextPage()
{
return min($this->getPage() + 1, $this->getLastPage());
}
/**
@ -218,26 +224,26 @@ class Doctrine_Pager
*
* @return int previous page
*/
public function getPreviousPage()
{
return max($this->getPage() - 1, $this->getFirstPage());
}
public function getPreviousPage()
{
return max($this->getPage() - 1, $this->getFirstPage());
}
/**
* haveToPaginate
*
* Return true if it's necessary to paginate or false if not
*
* @return bool true if it is necessary to paginate, false otherwise
*/
public function haveToPaginate()
{
return $this->getNbResults() > $this->getMaxPerPage();
}
/**
/**
* haveToPaginate
*
* Return true if it's necessary to paginate or false if not
*
* @return bool true if it is necessary to paginate, false otherwise
*/
public function haveToPaginate()
{
return $this->getNbResults() > $this->getMaxPerPage();
}
/**
* setPage
*
* Defines the current page and automatically adjust offset and limits
@ -245,11 +251,11 @@ class Doctrine_Pager
* @param $page current page
* @return void
*/
public function setPage($page)
{
$this->_setPage($page);
$this->adjustOffset();
}
public function setPage($page)
{
$this->_setPage($page);
$this->adjustOffset();
}
/**
@ -260,11 +266,11 @@ class Doctrine_Pager
* @param $page current page
* @return void
*/
private function _setPage($page)
{
$page = intval($page);
$this->page = ($page <= 0) ? 1 : $page;
}
private function _setPage($page)
{
$page = intval($page);
$this->page = ($page <= 0) ? 1 : $page;
}
/**
@ -274,13 +280,13 @@ class Doctrine_Pager
*
* @return int maximum number of itens per page
*/
public function getMaxPerPage()
{
return $this->maxPerPage;
}
/**
public function getMaxPerPage()
{
return $this->maxPerPage;
}
/**
* setMaxPerPage
*
* Defines the maximum number of itens per page and automatically adjust offset and limits
@ -288,11 +294,11 @@ class Doctrine_Pager
* @param $max maximum number of itens per page
* @return void
*/
public function setMaxPerPage($max)
{
$this->_setMaxPerPage($max);
$this->adjustOffset();
}
public function setMaxPerPage($max)
{
$this->_setMaxPerPage($max);
$this->adjustOffset();
}
/**
@ -303,16 +309,16 @@ class Doctrine_Pager
* @param $max maximum number of itens per page
* @return void
*/
private function _setMaxPerPage($max)
{
if ($max > 0) {
$this->maxPerPage = $max;
} else if ($max == 0) {
$this->maxPerPage = 25;
} else {
$this->maxPerPage = abs($max);
}
}
private function _setMaxPerPage($max)
{
if ($max > 0) {
$this->maxPerPage = $max;
} else if ($max == 0) {
$this->maxPerPage = 25;
} else {
$this->maxPerPage = abs($max);
}
}
/**
@ -322,10 +328,10 @@ class Doctrine_Pager
*
* @return Doctrine_Query Doctrine_Query object related to the pager
*/
public function getQuery()
{
return $this->query;
}
public function getQuery()
{
return $this->query;
}
/**
@ -333,17 +339,18 @@ class Doctrine_Pager
*
* 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
*/
protected function setQuery($query)
{
if (is_string($query)) {
$query = Doctrine_Query::create()->from($query);
}
protected function setQuery($query)
{
if (is_string($query)) {
$query = Doctrine_Query::create()->from($query);
}
$this->query = $query;
}
$this->query = $query;
}
/**
@ -351,13 +358,12 @@ class Doctrine_Pager
* executes the query and populates the data set
*
* @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
*/
public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD)
{
return $this->getQuery()->execute($params, $hydrationMode);
}
public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD)
{
return $this->getQuery()->execute($params, $hydrationMode);
}
}
?>