<?php
namespace Moto\Application\Util; use Moto\Application\Util\UniqueDuplicator\AbstractUniqueDuplicator; use Moto; class UniqueDuplicator { protected $_duplicator = null; public function __construct($options = array()) { if (empty($options)) { return; } $duplicator = null; if ($options instanceof AbstractUniqueDuplicator) { $duplicator = $options; } if (is_array($options) || $options instanceof \Traversable) { if (!array_key_exists('name', $options)) { throw new \InvalidArgumentException(__METHOD__ . ' expects argument "name"'); } if (!array_key_exists('options', $options)) { throw new \InvalidArgumentException(__METHOD__ . ' expects argument "options"'); } $className = ucfirst($options['name']); $class = 'Moto\Application\Util\UniqueDuplicator\\' . $className; if (class_exists($class) && $className != 'AbstractUniqueDuplicator') { $duplicator = new $class($options['options']); } else { throw new \RuntimeException(__METHOD__ . ' - expected class not exist '); } } if (is_null($duplicator)){ throw new \InvalidArgumentException(__METHOD__ . ' expects an array or Traversable or AbstractUniqueDuplicator'); } $this->setDuplicator($duplicator); } public function setDuplicator(AbstractUniqueDuplicator $duplicator) { $this->_duplicator = $duplicator; return $this; } public function getDuplicator() { return $this->_duplicator; } public function generate($name) { return $this->_duplicator->generate($name); } } 