Skip to content
Browse files

Add paginator factory & paginator adapater plugin manager

Add paginator factory & paginator adapater plugin manager
  • Loading branch information...
1 parent bcc6ee9 commit f8d6046e3ea05993d5d28a3a01c59465e12d6f8b @blanchonvincent blanchonvincent committed
Showing with 115 additions and 0 deletions.
  1. +62 −0 library/Zend/Paginator/AdapterPluginManager.php
  2. +53 −0 library/Zend/Paginator/Factory.php
View
62 library/Zend/Paginator/AdapterPluginManager.php
@@ -0,0 +1,62 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Paginator
+ */
+
+namespace Zend\Paginator;
+
+use Zend\ServiceManager\AbstractPluginManager;
+
+/**
+ * Plugin manager implementation for paginator adapters.
+ *
+ * Enforces that adapters retrieved are instances of
+ * Adapter\AdapterInterface. Additionally, it registers a number of default
+ * adapters available.
+ *
+ * @category Zend
+ * @package Zend_Paginator
+ */
+class AdapterPluginManager extends AbstractPluginManager
+{
+ /**
+ * Default set of adapters
+ *
+ * @var array
+ */
+ protected $invokableClasses = array(
+ 'array' => 'Zend\Paginator\Adapter\ArrayAdapter',
+ 'dbselect' => 'Zend\Paginator\Adapter\DbSelect',
+ 'iterator' => 'Zend\Paginator\Adapter\Iterator',
+ 'null' => 'Zend\Paginator\Adapter\Null',
+ );
+
+ /**
+ * Validate the plugin
+ *
+ * Checks that the adapter loaded is an instance
+ * of Adapter\AdapterInterface.
+ *
+ * @param mixed $plugin
+ * @return void
+ * @throws Exception\RuntimeException if invalid
+ */
+ public function validatePlugin($plugin)
+ {
+ if ($plugin instanceof Adapter\AdapterInterface) {
+ // we're okay
+ return;
+ }
+
+ throw new Exception\RuntimeException(sprintf(
+ 'Plugin of type %s is invalid; must implement %s\Adapter\AdapterInterface',
+ (is_object($plugin) ? get_class($plugin) : gettype($plugin)),
+ __NAMESPACE__
+ ));
+ }
+}
View
53 library/Zend/Paginator/Factory.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Paginator
+ */
+
+namespace Zend\Paginator;
+
+use Zend\Paginator\Adapter\AdapterInterface;
+
+/**
+ * @category Zend
+ * @package Zend_Paginator
+ */
+class Factory implements Countable, IteratorAggregate
+{
+ public static function factory($items, $adapter)
+ {
+ if(!$adapter instanceof AdapterInterface && $adapter instanceof AdapterAggregateInterface) {
+ $adapter = self::getAdapterPluginManager()->get($adapter, $items);
+ }
+
+ return new Paginator($adapter);
+ }
+
+ /**
+ * Change the adapter plugin manager
+ *
+ * @param AdapterPluginManager $adapters
+ * @return void
+ */
+ public static function setAdapterPluginManager(AdapterPluginManager $adapters)
+ {
+ self::$adapters = $adapters;
+ }
+
+ /**
+ * Get the adapter plugin manager
+ *
+ * @return AdapterPluginManager
+ */
+ public static function getAdapterPluginManager()
+ {
+ if (self::$adapters === null) {
+ self::$adapters = new AdapterPluginManager();
+ }
+ return self::$adapters;
+ }
+}

0 comments on commit f8d6046

Please sign in to comment.
Something went wrong with that request. Please try again.