Added Classmap
This commit is contained in:
parent
c2a2413330
commit
402f229971
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the BeSimpleSoapCommon.
|
||||
*
|
||||
* (c) Christian Kerl <christian-kerl@web.de>
|
||||
* (c) Francis Besset <francis.besset@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace BeSimple\SoapCommon;
|
||||
|
||||
/**
|
||||
* @author Francis Besset <francis.besset@gmail.com>
|
||||
*/
|
||||
class Classmap
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $classmap = array();
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->classmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function get($type)
|
||||
{
|
||||
if (!$this->has($type)) {
|
||||
throw new \InvalidArgumentException(sprintf('The type "%s" does not exists', $type));
|
||||
}
|
||||
|
||||
return $this->classmap[$type];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $classname
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function add($type, $classname)
|
||||
{
|
||||
if ($this->has($type)) {
|
||||
throw new \InvalidArgumentException(sprintf('The type "%s" already exists', $type));
|
||||
}
|
||||
|
||||
$this->classmap[$type] = $classname;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $classmap
|
||||
*/
|
||||
public function set(array $classmap)
|
||||
{
|
||||
$this->classmap = array();
|
||||
|
||||
foreach ($classmap as $type => $classname) {
|
||||
$this->add($type, $classname);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function has($type)
|
||||
{
|
||||
return isset($this->classmap[$type]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the BeSimpleSoapCommon.
|
||||
*
|
||||
* (c) Christian Kerl <christian-kerl@web.de>
|
||||
* (c) Francis Besset <francis.besset@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace BeSimple\SoapCommon\Tests;
|
||||
|
||||
use BeSimple\SoapCommon\Classmap;
|
||||
|
||||
/**
|
||||
* UnitTest for \BeSimple\SoapCommon\Classmap.
|
||||
*
|
||||
* @author Francis Besset <francis.besset@gmail.com>
|
||||
*/
|
||||
class ClassmapTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testAll()
|
||||
{
|
||||
$classmap = new Classmap();
|
||||
|
||||
$this->assertSame(array(), $classmap->all());
|
||||
}
|
||||
|
||||
public function testAdd()
|
||||
{
|
||||
$classmap = new Classmap();
|
||||
|
||||
$classmap->add('foobar', 'BeSimple\SoapCommon\Classmap');
|
||||
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
$classmap->add('foobar', 'BeSimple\SoapCommon\Classmap');
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$classmap = new Classmap();
|
||||
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
$classmap->get('foobar');
|
||||
|
||||
$classmap->add('foobar', 'BeSimple\SoapCommon\Classmap');
|
||||
$this->assertSame(array('foobar' => 'BeSimple\SoapCommon\Classmap'), $classmap->get('foobar'));
|
||||
}
|
||||
|
||||
public function testSet()
|
||||
{
|
||||
$classmap = new Classmap();
|
||||
|
||||
$classmap->add('foobar', 'BeSimple\SoapCommon\Tests\ClassmapTest');
|
||||
$classmap->add('foo', 'BeSimple\SoapCommon\Tests\Classmap');
|
||||
|
||||
$map = array(
|
||||
'foobar' => 'BeSimple\SoapCommon\Classmap',
|
||||
'barfoo' => 'BeSimple\SoapCommon\Tests\ClassmapTest',
|
||||
);
|
||||
$classmap->set($map);
|
||||
|
||||
$this->assertSame($map, $classmap->all());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue