Filter.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Filter;
  11. use Sonata\AdminBundle\Filter\FilterInterface;
  12. abstract class Filter implements FilterInterface
  13. {
  14. protected $name = null;
  15. protected $value = null;
  16. protected $options = array();
  17. protected $condition;
  18. const CONDITION_OR = 'OR';
  19. const CONDITION_AND = 'AND';
  20. /**
  21. * @param string $name
  22. * @param array $options
  23. */
  24. public function initialize($name, array $options = array())
  25. {
  26. $this->name = $name;
  27. $this->setOptions($options);
  28. }
  29. /**
  30. * @return string
  31. */
  32. public function getName()
  33. {
  34. return $this->name;
  35. }
  36. /**
  37. * @param string $name
  38. * @param null $default
  39. * @return mixed
  40. */
  41. public function getOption($name, $default = null)
  42. {
  43. if (array_key_exists($name, $this->options)) {
  44. return $this->options[$name];
  45. }
  46. return $default;
  47. }
  48. /**
  49. * @return string
  50. */
  51. public function getFieldType()
  52. {
  53. return $this->getOption('field_type', 'text');
  54. }
  55. /**
  56. * @return array
  57. */
  58. public function getFieldOptions()
  59. {
  60. return $this->getOption('field_options', array('required' => false));
  61. }
  62. /**
  63. * @return string
  64. */
  65. public function getFieldName()
  66. {
  67. $fieldName = $this->getOption('field_name');
  68. if (!$fieldName) {
  69. throw new \RunTimeException(sprintf('The option `field_name` must be set for field : `%s`', $this->getName()));
  70. }
  71. return $fieldName;
  72. }
  73. /**
  74. * @param array $options
  75. * @return void
  76. */
  77. public function setOptions(array $options)
  78. {
  79. $this->options = array_merge($this->getDefaultOptions(), $options);
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function getOptions()
  85. {
  86. return $this->options;
  87. }
  88. /**
  89. * @param $value
  90. * @return void
  91. */
  92. public function setValue($value)
  93. {
  94. $this->value = $value;
  95. }
  96. /**
  97. * @return mixed
  98. */
  99. public function getValue()
  100. {
  101. return $this->value;
  102. }
  103. /**
  104. * @param $condition
  105. * @return void
  106. */
  107. public function setCondition($condition)
  108. {
  109. $this->condition = $condition;
  110. }
  111. /**
  112. * @return
  113. */
  114. public function getCondition()
  115. {
  116. return $this->condition;
  117. }
  118. }