Permalink
Browse files

Code refactory to make easier

Code refactory to make easier
  • Loading branch information...
1 parent 09ce586 commit 8c81500160cb3e6d95b2885109fa3bdb7925800f @blanchonvincent blanchonvincent committed Oct 10, 2012
Showing with 63 additions and 27 deletions.
  1. +63 −27 library/Zend/Paginator/Factory.php
@@ -20,47 +20,83 @@
*/
abstract class Factory
{
+ /**
+ * Adapter plugin manager
+ * @var AdapterPluginManager
+ */
protected static $adapters;
- public static function factory($items, $adapter = null)
+ /**
+ * Create adapter from items if necessary, and return paginator
+ * @param Traversable/array $items
+ * @return Paginator
+ */
+ protected static function createAdapterFromItems($items)
{
- if (null === $adapter) {
- if ($items instanceof Traversable) {
- $items = ArrayUtils::iteratorToArray($items);
- }
- if (!is_array($items)) {
- throw new Exception\InvalidArgumentException(
- 'The factory needs an associative array '
- . 'or a Traversable object as an argument when '
- . "it's used with one parameter"
- );
- }
- if (!isset($items['adapter']) && !isset($items['items'])) {
- throw new Exception\InvalidArgumentException(
- 'The factory needs an associative array '
- . 'or a Traversable object with keys '
- . '"adapter" and "items"'
- );
- }
- $adapter = $items['adapter'];
- $items = $items['items'];
+ if ($items instanceof Traversable) {
+ $items = ArrayUtils::iteratorToArray($items);
}
- if (!$adapter instanceof AdapterInterface && !$adapter instanceof AdapterAggregateInterface) {
- $adapter = self::getAdapterPluginManager()->get($adapter, $items);
+ if (!is_array($items)) {
+ throw new Exception\InvalidArgumentException(
+ 'The factory needs an associative array '
+ . 'or a Traversable object as an argument when '
+ . "it's used with one parameter"
+ );
}
+ if (!isset($items['adapter']) && !isset($items['items'])) {
+ throw new Exception\InvalidArgumentException(
+ 'The factory needs an associative array '
+ . 'or a Traversable object with keys '
+ . '"adapter" and "items"'
+ );
+ }
+ $adapter = $items['adapter'];
+ $items = $items['items'];
+
+ $paginator = static::getAdapterFromManager($items, $adapter);
+ return $paginator;
+ }
+ /**
+ * Get adapter from manager if necessary, and return paginator
+ * @param mixed $items
+ * @param mixed $adapter
+ * @return Paginator
+ */
+ protected static function getAdapterFromManager($items, $adapter)
+ {
+ if ($adapter instanceof AdapterInterface || $adapter instanceof AdapterAggregateInterface) {
+ return new Paginator($adapter);
+ }
+ $adapter = static::getAdapterPluginManager()->get($adapter, $items);
return new Paginator($adapter);
}
/**
+ * Create paginator with items and adapter
+ * @param mixed $items
+ * @param mixed $adapter
+ * @return Paginator
+ */
+ public static function factory($items, $adapter = null)
+ {
+ if (null === $adapter) {
+ $paginator = static::createAdapterFromItems($items);
+ return $paginator;
+ }
+ $paginator = static::getAdapterFromManager($items, $adapter);
+ return $paginator;
+ }
+
+ /**
* Change the adapter plugin manager
*
* @param AdapterPluginManager $adapters
* @return void
*/
public static function setAdapterPluginManager(AdapterPluginManager $adapters)
{
- self::$adapters = $adapters;
+ static::$adapters = $adapters;
}
/**
@@ -70,9 +106,9 @@ public static function setAdapterPluginManager(AdapterPluginManager $adapters)
*/
public static function getAdapterPluginManager()
{
- if (self::$adapters === null) {
- self::$adapters = new AdapterPluginManager();
+ if (static::$adapters === null) {
+ static::$adapters = new AdapterPluginManager();
}
- return self::$adapters;
+ return static::$adapters;
}
}

0 comments on commit 8c81500

Please sign in to comment.