Browse files

Updte factory for one argument

Updte factory for one argument
  • Loading branch information...
1 parent 69818cb commit 3da4598d95a3967d5be9598d095822e50f0ecb3b @blanchonvincent blanchonvincent committed Sep 27, 2012
Showing with 29 additions and 6 deletions.
  1. +1 −1 library/Zend/Paginator/AdapterPluginManager.php
  2. +28 −5 library/Zend/Paginator/Factory.php
View
2 library/Zend/Paginator/AdapterPluginManager.php
@@ -35,7 +35,7 @@ class AdapterPluginManager extends AbstractPluginManager
'iterator' => 'Zend\Paginator\Adapter\Iterator',
'null' => 'Zend\Paginator\Adapter\Null',
);
-
+
/**
* Attempt to create an instance via an invokable class
*
View
33 library/Zend/Paginator/Factory.php
@@ -10,7 +10,9 @@
namespace Zend\Paginator;
+use Traversable;
use Zend\Paginator\Adapter\AdapterInterface;
+use Zend\Stdlib\ArrayUtils;
/**
* @category Zend
@@ -19,16 +21,37 @@
abstract class Factory
{
protected static $adapters;
-
- public static function factory($items, $adapter)
- {
+
+ public static function factory($items, $adapter = null)
+ {
+ 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(!$adapter instanceof AdapterInterface && !$adapter instanceof AdapterAggregateInterface) {
$adapter = self::getAdapterPluginManager()->get($adapter, $items);
}
-
+
return new Paginator($adapter);
}
-
+
/**
* Change the adapter plugin manager
*

0 comments on commit 3da4598

Please sign in to comment.