/** * Add filters to the Hydrator based on a predefined configuration format, if specified. * * @param AbstractHydrator $hydrator * @param ContainerInterface $container * @param array $config * @param ObjectManager $objectManager * * @throws ServiceNotCreatedException */ protected function configureHydratorFilters($hydrator, ContainerInterface $container, $config, $objectManager) { if (!$hydrator instanceof FilterEnabledInterface || !isset($config['filters']) || !is_array($config['filters'])) { return; } foreach ($config['filters'] as $name => $filterConfig) { $conditionMap = array('and' => FilterComposite::CONDITION_AND, 'or' => FilterComposite::CONDITION_OR); $condition = isset($filterConfig['condition']) ? $conditionMap[$filterConfig['condition']] : FilterComposite::CONDITION_OR; $filterService = $filterConfig['filter']; if (!$container->has($filterService)) { throw new ServiceNotCreatedException(sprintf('Invalid filter %s for field %s: service does not exist', $filterService, $name)); } $filterService = $container->get($filterService); if (!$filterService instanceof FilterInterface) { throw new ServiceNotCreatedException(sprintf('Filter service %s must implement FilterInterface', get_class($filterService))); } if ($filterService instanceof ObjectManagerAwareInterface) { $filterService->setObjectManager($objectManager); } $hydrator->addFilter($name, $filterService, $condition); } }
/** * {@inheritDoc} */ public function addFilter($name, $filter, $condition = Filter\FilterComposite::CONDITION_OR) { $this->resetCaches(); return parent::addFilter($name, $filter, $condition); }