Permalink
Browse files

Adapters and twig views

  • Loading branch information...
1 parent b7d8bce commit 9650fd95d2440573afecc9943ca07e548bf43631 javiacei committed Sep 23, 2011
@@ -14,6 +14,8 @@
* @package IdeupSimplePaginatorBundle
* @subpackage DependencyInjection
* @author Francisco Javier Aceituno <javier.aceituno@ideup.com>
+ * @author Moises Maciá <moises.macia@ideup.com>
+ * @author Gustavo Piltcher
*/
class IdeupSimplePaginatorExtension extends Extension
{
@@ -0,0 +1,39 @@
+<?php
+
+namespace Ideup\SimplePaginatorBundle\Paginator\Adapter;
+
+/**
+ * AdapterInterface
+ *
+ * @package IdeupSimplePaginatorBundle
+ * @subpackage Adapter
+ * @author Francisco Javier Aceituno <javier.aceituno@ideup.com>
+ */
+interface AdapterInterface
+{
+ /**
+ * @return integer. Returns the total number of elements
+ */
+ public function getTotalResults();
+
+ /**
+ * This method sets the first element to display
+ *
+ * @param integer $offset
+ * @return AdapterInterface. Returns itself
+ */
+ public function setOffset($offset);
+
+ /**
+ * This method sets the maximum number of elements
+ *
+ * @param integer $lenght
+ * @return AdapterInterface. Returns itself
+ */
+ public function setLength($length);
+
+ /**
+ * @return array. Returns the collection of paged objects
+ */
+ public function getResult();
+}
@@ -0,0 +1,61 @@
+<?php
+
+namespace Ideup\SimplePaginatorBundle\Paginator\Adapter;
+
+use
+ Ideup\SimplePaginatorBundle\Paginator\Adapter\AdapterInterface
+;
+
+/**
+ * ArrayAdapter
+ *
+ * @package IdeupSimplePaginatorBundle
+ * @subpackage Adapter
+ * @author Francisco Javier Aceituno <javier.aceituno@ideup.com>
+
+ */
+class ArrayAdapter implements AdapterInterface
+{
+ protected $collection;
+ protected $offset;
+ protected $length;
+
+ public function __construct(array $collection)
+ {
+ $this->collection = $collection;
+ }
+
+ /**
+ * {@inheritdoc }
+ */
+ public function getTotalResults()
+ {
+ return count($this->collection);
+ }
+
+ /**
+ * {@inheritdoc }
+ */
+ public function setOffset($offset)
+ {
+ $this->offset = $offset;
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc }
+ */
+ public function setLength($length)
+ {
+ $this->length = $length;
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc }
+ */
+ public function getResult()
+ {
+ return \array_slice($this->collection, $this->offset, $this->length);
+ }
+}
@@ -0,0 +1,68 @@
+<?php
+
+namespace Ideup\SimplePaginatorBundle\Paginator\Adapter\Doctrine\ORM;
+
+use
+ Doctrine\ORM\Query,
+ DoctrineExtensions\Paginate\Paginate,
+
+ Ideup\SimplePaginatorBundle\Paginator\Adapter\AdapterInterface
+;
+
+
+/**
+ * QueryAdapter
+ *
+ * @package IdeupSimplePaginatorBundle
+ * @subpackage Adapter
+ * @author Francisco Javier Aceituno <javier.aceituno@ideup.com>
+
+ */
+class QueryAdapter implements AdapterInterface
+{
+ protected $query;
+
+ /**
+ * Constructor
+ *
+ * @param Doctrine\ORM\Query $query
+ */
+ public function __construct(Query $query)
+ {
+ $this->query = $query;
+ }
+
+ /**
+ * {@inheritdoc }
+ */
+ public function getTotalResults()
+ {
+ return (int)Paginate::getTotalQueryResults($this->query);
+ }
+
+ /**
+ * {@inheritdoc }
+ */
+ public function setOffset($offset)
+ {
+ $this->query->setFirstResult($offset);
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc }
+ */
+ public function setLength($length)
+ {
+ $this->query->setMaxResults($length);
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc }
+ */
+ public function getResult()
+ {
+ return $this->query->getResult();
+ }
+}
View
@@ -3,15 +3,12 @@
namespace Ideup\SimplePaginatorBundle\Paginator;
use
- Symfony\Component\HttpFoundation\Request,
- Doctrine\ORM\Query as Query,
- DoctrineExtensions\Paginate\Paginate
+ Symfony\Component\HttpFoundation\Request
;
/**
- * IdeupSimplePaginatorBundle
+ * Paginator
*
- * Class that defines the Dependency Injection Extension to expose the bundle's semantic configuration
* @package IdeupSimplePaginatorBundle
* @subpackage Paginator
* @author Francisco Javier Aceituno <javier.aceituno@ideup.com>
@@ -23,52 +20,51 @@ class Paginator
* @var array $currentPage
*/
protected $currentPage;
+
/**
* @var array $itemsPerPage
*/
protected $itemsPerPage;
+
/**
* @var array $maxPagerItems
*/
protected $maxPagerItems;
+
/**
* @var array $totalItems
*/
protected $totalItems;
/**
- * @var array $offset
- */
- protected $offset;
-
- /**
* @param Symfony\Component\HttpFoundation\Request $request
*/
public function __construct(Request $request)
- {
- $paginatorId = $request->query->get('paginatorId');
+ {
+ $paginatorId = $request->get('paginatorId');
$this->setFallbackValues();
- $page = (int)$request->query->get('page');
+ $page = (int)$request->get('page');
+
$this->currentPage = array(
md5($paginatorId) => ($page > 0) ? $page : $this->getFirstPage(),
);
- // TODO: get the default values from config.yml
$itemsPerPage = (int)$request->query->get('limit');
$this->setItemsPerPage(($itemsPerPage > 0) ? $itemsPerPage : 10, $paginatorId);
- $this->setMaxPagerItems(10, $paginatorId);
- $this->totalItems = array(md5($paginatorId) => 0);
+ // TODO: MaxPagerItems load by config.yml
+ $this->setMaxPagerItems(3, $paginatorId);
+ $this->totalItems = array(md5($paginatorId) => 0);
}
private function setFallbackValues()
{
$hash = md5(null);
$this->currentPage[$hash] = 0;
$this->itemsPerPage[$hash] = 10;
- $this->maxPagerItems[$hash] = 10;
- $this->totalItems[$hash] = 0;
+ $this->maxPagerItems[$hash] = 3;
+ $this->totalItems[$hash] = 0;
}
/**
@@ -88,6 +84,7 @@ public function getItemsPerPage($id = null)
public function setItemsPerPage($itemsPerPage, $id = null)
{
$this->itemsPerPage[md5($id)] = (int)$itemsPerPage;
+ return $this;
}
/**
@@ -107,33 +104,42 @@ public function getMaxPagerItems($id = null)
public function setMaxPagerItems($maxPagerItems, $id = null)
{
$this->maxPagerItems[md5($id)] = (int)$maxPagerItems;
+ return $this;
}
-
- /**
- * @param int $offset
- * @param string $id
- */
- public function setOffset($offset, $id = null)
+
+ protected function getAdapterOf($collection)
{
- $this->offset[md5($id)] = (int)$offset;
+ if (\is_array($collection)) {
+ $className = 'Array';
+ } else {
+ $r = new \ReflectionClass($collection);
+ $className = $r->getName();
+ }
+
+ $adapterName =
+ __NAMESPACE__ . '\\Adapter\\' . $className . 'Adapter'
+ ;
+
+ return new $adapterName($collection);
}
/**
* Transforms the given Doctrine DQL into a paginated query
* If you need to paginate various queries in the same controller, you need to specify an $id
*
- * @param Doctrine\ORM\Query $query
+ * @param mixed $collection
* @param string $id
* @return Doctrine\ORM\Query
*/
- public function paginate(Query $query, $id = null)
+ public function paginate($collection, $id = null)
{
- $this->totalItems[md5($id)] = (int)Paginate::getTotalQueryResults($query);
+ $adapter = $this->getAdapterOf($collection);
+
+ $this->totalItems[md5($id)] = $adapter->getTotalResults();
$offset = ($this->getCurrentPage($id) - 1) * $this->getItemsPerPage($id);
- $this->setOffset($offset, $id);
- return $query->setFirstResult($offset)->setMaxResults($this->getItemsPerPage($id));
- }
+ return $adapter->setOffset($offset)->setLength($this->getItemsPerPage($id));
+ }
/**
* @param string $id
* @return int
@@ -177,17 +183,22 @@ public function getMaxPageInRange($id = null)
return $this->getLastPage($id);
}
- return $min + $this->getMaxPagerItems($id);
+ return $min + $this->getMaxPagerItems($id) - 1;
}
/**
* @param string $id
* @return int
*/
public function getMinPageInRange($id = null)
- {
- $maxItems = $this->getMaxPagerItems($id);
- return (int)floor($this->getCurrentPage($id)/$maxItems) * $maxItems + $this->getFirstPage();
+ {
+ $offset = floor(($this->getMaxPagerItems($id) - 1)/2);
+
+ if ($this->getCurrentPage($id) > $offset) {
+ return $this->getCurrentPage($id) - $offset;
+ }
+
+ return $this->getFirstPage();
}
/**
@@ -220,16 +231,4 @@ public function getFirstPage()
{
return 1;
}
-
- /**
- * Get the initial offset of current page
- *
- * @param string $id
- * @return int
- */
- public function getOffset($id = null)
- {
- $hash = md5($id);
- return isset($this->offset[$hash]) ? $this->offset[$hash] : 0;
- }
}
Oops, something went wrong.

0 comments on commit 9650fd9

Please sign in to comment.