Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#4319 from bakura10/hyd…
Browse files Browse the repository at this point in the history
…rator-plugin-manager

Add various plugin manager
  • Loading branch information
weierophinney committed Apr 26, 2013
2 parents dbfc773 + f1ccaa9 commit b1c0133
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/Hydrator/HydratorPluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Hydrator;

use Zend\ServiceManager\AbstractPluginManager;
use Zend\Stdlib\Exception;

/**
* Plugin manager implementation for hydrators.
*
* Enforces that adapters retrieved are instances of HydratorInterface
*/
class HydratorPluginManager extends AbstractPluginManager
{
/**
* Whether or not to share by default
*
* @var bool
*/
protected $shareByDefault = false;

/**
* Default set of adapters
*
* @var array
*/
protected $invokableClasses = array(
'arrayserializable' => 'Zend\Stdlib\Hydrator\ArraySerializable',
'classmethods' => 'Zend\Stdlib\Hydrator\ClassMethods',
'objectproperty' => 'Zend\Stdlib\Hydrator\ObjectProperty',
'reflection' => 'Zend\Stdlib\Hydrator\Reflection'
);

/**
* {@inheritDoc}
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof HydratorInterface) {
// we're okay
return;
}

throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement Zend\Stdlib\Hydrator\HydratorInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin))
));
}
}
42 changes: 42 additions & 0 deletions test/Hydrator/HydratorManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Stdlib\Hydrator;

use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\Hydrator\HydratorPluginManager;

/**
* @group Zend_Stdlib
*/
class HydratorManagerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var HydratorPluginManager
*/
protected $manager;

public function setUp()
{
$this->manager = new HydratorPluginManager();
}

public function testRegisteringInvalidElementRaisesException()
{
$this->setExpectedException('Zend\Stdlib\Exception\RuntimeException');
$this->manager->setService('test', $this);
}

public function testLoadingInvalidElementRaisesException()
{
$this->manager->setInvokableClass('test', get_class($this));
$this->setExpectedException('Zend\Stdlib\Exception\RuntimeException');
$this->manager->get('test');
}
}

0 comments on commit b1c0133

Please sign in to comment.