* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Sonata\AdminBundle\Filter; /** * Class Filter. * * @author Thomas Rabaix */ abstract class Filter implements FilterInterface { /** * @var string|null */ protected $name = null; /** * @var mixed|null */ protected $value = null; /** * @var array */ protected $options = array(); /** * @var string */ protected $condition; /** * {@inheritdoc} */ public function initialize($name, array $options = array()) { $this->name = $name; $this->setOptions($options); } /** * {@inheritdoc} */ public function getName() { return $this->name; } /** * {@inheritdoc} */ public function getFormName() { /* Symfony default form class sadly can't handle form element with dots in its name (when data get bound, the default dataMapper is a PropertyPathMapper). So use this trick to avoid any issue. */ return str_replace('.', '__', $this->name); } /** * {@inheritdoc} */ public function getOption($name, $default = null) { if (array_key_exists($name, $this->options)) { return $this->options[$name]; } return $default; } /** * {@inheritdoc} */ public function setOption($name, $value) { $this->options[$name] = $value; } /** * {@inheritdoc} */ public function getFieldType() { // NEXT_MAJOR: Remove ternary and keep 'Symfony\Component\Form\Extension\Core\Type\TextType' // (when requirement of Symfony is >= 2.8) return $this->getOption('field_type', method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ? 'Symfony\Component\Form\Extension\Core\Type\TextType' : 'text' ); } /** * {@inheritdoc} */ public function getFieldOptions() { return $this->getOption('field_options', array('required' => false)); } /** * {@inheritdoc} */ public function getFieldOption($name, $default = null) { if (isset($this->options['field_options'][$name]) && is_array($this->options['field_options'])) { return $this->options['field_options'][$name]; } return $default; } /** * {@inheritdoc} */ public function setFieldOption($name, $value) { $this->options['field_options'][$name] = $value; } /** * {@inheritdoc} */ public function getLabel() { return $this->getOption('label'); } /** * {@inheritdoc} */ public function setLabel($label) { $this->setOption('label', $label); } /** * {@inheritdoc} */ public function getFieldName() { $fieldName = $this->getOption('field_name'); if (!$fieldName) { throw new \RuntimeException(sprintf('The option `field_name` must be set for field: `%s`', $this->getName())); } return $fieldName; } /** * {@inheritdoc} */ public function getParentAssociationMappings() { return $this->getOption('parent_association_mappings', array()); } /** * {@inheritdoc} */ public function getFieldMapping() { $fieldMapping = $this->getOption('field_mapping'); if (!$fieldMapping) { throw new \RuntimeException(sprintf('The option `field_mapping` must be set for field: `%s`', $this->getName())); } return $fieldMapping; } /** * {@inheritdoc} */ public function getAssociationMapping() { $associationMapping = $this->getOption('association_mapping'); if (!$associationMapping) { throw new \RuntimeException(sprintf('The option `association_mapping` must be set for field: `%s`', $this->getName())); } return $associationMapping; } /** * Set options. * * @param array $options */ public function setOptions(array $options) { $this->options = array_merge( array('show_filter' => null, 'advanced_filter' => true), $this->getDefaultOptions(), $options ); } /** * Get options. * * @return array */ public function getOptions() { return $this->options; } /** * Set value. * * @param mixed $value */ public function setValue($value) { $this->value = $value; } /** * Get value. * * @return mixed */ public function getValue() { return $this->value; } /** * {@inheritdoc} */ public function isActive() { $values = $this->getValue(); return isset($values['value']) && false !== $values['value'] && '' !== $values['value']; } /** * {@inheritdoc} */ public function setCondition($condition) { $this->condition = $condition; } /** * {@inheritdoc} */ public function getCondition() { return $this->condition; } /** * {@inheritdoc} */ public function getTranslationDomain() { return $this->getOption('translation_domain'); } }