Permalink
Browse files

feat(PropelModelPager): change PropelModelPager::count() #208

PropelModelPager::count() now return the number of items in the collection

remove unused variables, add phpdocs
  • Loading branch information...
1 parent 61c20d9 commit c44330f075562091c0a9f9071019770eac02b540 @jaugustin jaugustin committed Sep 22, 2013
Showing with 54 additions and 15 deletions.
  1. +4 −1 UPDATE.md
  2. +36 −14 src/Propel/Runtime/Util/PropelModelPager.php
  3. +14 −0 tests/Propel/Tests/Runtime/Util/PropelModelPagerTest.php
View
@@ -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.
@@ -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()`.
@@ -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 <fabien.potencier@symfony-project.com>
* @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);
@@ -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();
@@ -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++;
}
@@ -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());
}
}
@@ -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.