From e32514abe4b970bfbc5090ec64096d5ceee6f7c5 Mon Sep 17 00:00:00 2001 From: Ghislain Loaec Date: Fri, 6 Apr 2018 10:43:58 +0200 Subject: [PATCH] Add Classmap (common) --- src/BeSimple/SoapCommon/Classmap.php | 93 ++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/BeSimple/SoapCommon/Classmap.php diff --git a/src/BeSimple/SoapCommon/Classmap.php b/src/BeSimple/SoapCommon/Classmap.php new file mode 100644 index 0000000..6f5fc4c --- /dev/null +++ b/src/BeSimple/SoapCommon/Classmap.php @@ -0,0 +1,93 @@ + + * (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]); + } + + public function addClassmap(Classmap $classmap) + { + foreach ($classmap->all() as $type => $classname) { + $this->add($type, $classname); + } + } +} \ No newline at end of file