* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Sonata\AdminBundle\Filter; use Sonata\AdminBundle\Filter\FilterInterface; abstract class Filter implements FilterInterface { protected $name = null; protected $value = null; protected $options = array(); protected $condition; const CONDITION_OR = 'OR'; const CONDITION_AND = 'AND'; /** * {@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() { return $this->getOption('field_type', 'text'); } /** * {@inheritdoc} */ public function getFieldOptions() { return $this->getOption('field_options', array('required' => false)); } /** * {@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; } /** * @param array $options * * @return void */ public function setOptions(array $options) { $this->options = array_merge($this->getDefaultOptions(), $options); } /** * @return array */ public function getOptions() { return $this->options; } /** * @param mixed $value * * @return void */ public function setValue($value) { $this->value = $value; } /** * @return mixed */ public function getValue() { return $this->value; } /** * @return string */ public function isActive() { $values = $this->getValue(); return !empty($values['value']); } /** * @param string $condition * * @return void */ public function setCondition($condition) { $this->condition = $condition; } /** * @return */ public function getCondition() { return $this->condition; } }