Filter.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * @param $name
  50. * @param $value
  51. */
  52. public function setOption($name, $value)
  53. {
  54. $this->options[$name] = $value;
  55. }
  56. /**
  57. * @return string
  58. */
  59. public function getFieldType()
  60. {
  61. return $this->getOption('field_type', 'text');
  62. }
  63. /**
  64. * @return array
  65. */
  66. public function getFieldOptions()
  67. {
  68. return $this->getOption('field_options', array('required' => false));
  69. }
  70. /**
  71. * @return string
  72. */
  73. public function getLabel()
  74. {
  75. return $this->getOption('label');
  76. }
  77. /**
  78. * @param $label
  79. */
  80. public function setLabel($label)
  81. {
  82. $this->setOption('label', $label);
  83. }
  84. /**
  85. * @return string
  86. */
  87. public function getFieldName()
  88. {
  89. $fieldName = $this->getOption('field_name');
  90. if (!$fieldName) {
  91. throw new \RunTimeException(sprintf('The option `field_name` must be set for field : `%s`', $this->getName()));
  92. }
  93. return $fieldName;
  94. }
  95. /**
  96. * @param array $options
  97. * @return void
  98. */
  99. public function setOptions(array $options)
  100. {
  101. $this->options = array_merge($this->getDefaultOptions(), $options);
  102. }
  103. /**
  104. * @return array
  105. */
  106. public function getOptions()
  107. {
  108. return $this->options;
  109. }
  110. /**
  111. * @param $value
  112. * @return void
  113. */
  114. public function setValue($value)
  115. {
  116. $this->value = $value;
  117. }
  118. /**
  119. * @return mixed
  120. */
  121. public function getValue()
  122. {
  123. return $this->value;
  124. }
  125. /**
  126. * @param $condition
  127. * @return void
  128. */
  129. public function setCondition($condition)
  130. {
  131. $this->condition = $condition;
  132. }
  133. /**
  134. * @return
  135. */
  136. public function getCondition()
  137. {
  138. return $this->condition;
  139. }
  140. }