Skip to content

Commit

Permalink
feat(PropelModelPager): change PropelModelPager::count() propelorm#208
Browse files Browse the repository at this point in the history
PropelModelPager::count() now return the number of items in the collection

remove unused variables, add phpdocs
  • Loading branch information
jaugustin committed Sep 22, 2013
1 parent 61c20d9 commit c44330f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
5 changes: 4 additions & 1 deletion UPDATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ After:

## `Propel\Generator\Model\ForeignKey::getForeignColumnNames` method has been removed.

## `Propel\Generator\Model\Domain::printSize` method has been renamed to
## `Propel\Generator\Model\Domain::printSize` method has been renamed to
`Propel\Generator\Model\Domain::getSizeDefinition`.

## `Propel\Generator\Model\Table::printPrimaryKey` method has been removed.
Expand Down Expand Up @@ -152,3 +152,6 @@ This could break, behaviors that call `parent` methods

All methods from `Propel\Runtime\Om\Persistent` Interface have been removed
All Base Object classes now implements `Propel\Runtime\Om\ActiveRecordInterface` this could be use to identify a Propel Object

## `Propel\Runtime\Util\PropelModelPager::count()` now return the number of items in the collection.
To get the old behavior use `Propel\Runtime\Util\PropelModelPager::getNbResults()`.
50 changes: 36 additions & 14 deletions src/Propel/Runtime/Util/PropelModelPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,65 @@
namespace Propel\Runtime\Util;

use Propel\Runtime\ActiveQuery\ModelCriteria;
use Propel\Runtime\Collection\Collection;
use Propel\Runtime\Connection\ConnectionInterface;

/**
* Implements a pager based on a ModelCriteria
* The code from this class heavily borrows from symfony's sfPager class
*
* @author Fabien Potencier <[email protected]>
* @author François Zaninotto
* @version $Revision$
*/
class PropelModelPager implements \IteratorAggregate, \Countable
{
/**
* @var ModelCriteria
*/
protected $query;

/**
* @var int current page
*/
protected $page;

/**
* @var int number of item per page
*/
protected $maxPerPage;

/**
* @var int index of the last page
*/
protected $lastPage;

/**
* @var int number of item the query return without pagination
*/
protected $nbResults;

protected $objects;

protected $parameters;

/**
* @var int
*/
protected $currentMaxLink;

/**
* @var int
*/
protected $maxRecordLimit;

/**
* @var Collection|array|mixed
*/
protected $results;

/**
* @var ConnectionInterface
*/
protected $con;

public function __construct(ModelCriteria $query, $maxPerPage = 10)
{
$this->parameters = array();

$this->setQuery($query);
$this->setMaxPerPage($maxPerPage);
$this->setPage(1);
Expand All @@ -68,7 +90,7 @@ public function getQuery()
return $this->query;
}

public function init($con = null)
public function init(ConnectionInterface $con = null)
{
$this->con = $con;
$hasMaxRecordLimit = false !== $this->getMaxRecordLimit();
Expand Down Expand Up @@ -140,16 +162,16 @@ public function setMaxRecordLimit($limit)
$this->maxRecordLimit = $limit;
}

public function getLinks($nb_links = 5)
public function getLinks($nbLinks = 5)
{
$links = array();
$tmp = $this->page - floor($nb_links / 2);
$check = $this->lastPage - $nb_links + 1;
$tmp = $this->page - floor($nbLinks / 2);
$check = $this->lastPage - $nbLinks + 1;
$limit = ($check > 0) ? $check : 1;
$begin = ($tmp > 0) ? (($tmp > $limit) ? $limit : $tmp) : 1;

$i = (int) $begin;
while (($i < $begin + $nb_links) && ($i <= $this->lastPage)) {
while (($i < $begin + $nbLinks) && ($i <= $this->lastPage)) {
$links[] = $i++;
}

Expand Down Expand Up @@ -414,14 +436,14 @@ public function getIterator()
}

/**
* Returns the total number of results.
* Returns the number of items in the result collection.
*
* @see Countable
* @return int
*/
public function count()
{
return $this->getNbResults();
return count($this->getResults());
}

}
14 changes: 14 additions & 0 deletions tests/Propel/Tests/Runtime/Util/PropelModelPagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,18 @@ public function testIsOddAndIsEven()
}
}

public function testCountableInterface()
{
BookQuery::create()->deleteAll();
$pager = $this->getPager(10);
$this->assertCount(0, $pager);

$this->createBooks(15);
$pager = $this->getPager(10);
$this->assertCount(10, $pager);

$pager = $this->getPager(10, 2);
$this->assertCount(5, $pager);
}

}

0 comments on commit c44330f

Please sign in to comment.