* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Sonata\AdminBundle\Filter; use Symfony\Component\DependencyInjection\ContainerInterface; /** * @author Thomas Rabaix */ class FilterFactory implements FilterFactoryInterface { /** * @var ContainerInterface */ protected $container; /** * @var string[] */ protected $types; /** * @param ContainerInterface $container * @param string[] $types */ public function __construct(ContainerInterface $container, array $types = array()) { $this->container = $container; $this->types = $types; } /** * {@inheritdoc} */ public function create($name, $type, array $options = array()) { if (!$type) { throw new \RuntimeException('The type must be defined'); } $id = isset($this->types[$type]) ? $this->types[$type] : false; if ($id) { $filter = $this->container->get($id); } elseif (class_exists($type)) { $filter = new $type(); } else { throw new \RuntimeException(sprintf('No attached service to type named `%s`', $type)); } if (!$filter instanceof FilterInterface) { throw new \RuntimeException(sprintf('The service `%s` must implement `FilterInterface`', $type)); } $filter->initialize($name, $options); return $filter; } }