From 402f229971e62976e185ab0e7532e77ba8160764 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Sat, 8 Oct 2011 14:58:36 +0200 Subject: [PATCH] Added Classmap --- src/BeSimple/SoapCommon/Classmap.php | 86 +++++++++++++++++++ .../Tests/SoapCommon/ClassmapTest.php | 67 +++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 src/BeSimple/SoapCommon/Classmap.php create mode 100644 tests/BeSimple/Tests/SoapCommon/ClassmapTest.php diff --git a/src/BeSimple/SoapCommon/Classmap.php b/src/BeSimple/SoapCommon/Classmap.php new file mode 100644 index 0000000..3f0ece6 --- /dev/null +++ b/src/BeSimple/SoapCommon/Classmap.php @@ -0,0 +1,86 @@ + + * (c) Francis Besset + * + * 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 + */ +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]); + } +} \ No newline at end of file diff --git a/tests/BeSimple/Tests/SoapCommon/ClassmapTest.php b/tests/BeSimple/Tests/SoapCommon/ClassmapTest.php new file mode 100644 index 0000000..55f5998 --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/ClassmapTest.php @@ -0,0 +1,67 @@ + + * (c) Francis Besset + * + * 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 + */ +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()); + } +} \ No newline at end of file