Filter.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. public function getLabel()
  63. {
  64. return $this->getOption('label', $this->getName());
  65. }
  66. /**
  67. * @return string
  68. */
  69. public function getFieldName()
  70. {
  71. $fieldName = $this->getOption('field_name');
  72. if (!$fieldName) {
  73. throw new \RunTimeException(sprintf('The option `field_name` must be set for field : `%s`', $this->getName()));
  74. }
  75. return $fieldName;
  76. }
  77. /**
  78. * @param array $options
  79. * @return void
  80. */
  81. public function setOptions(array $options)
  82. {
  83. $this->options = array_merge($this->getDefaultOptions(), $options);
  84. }
  85. /**
  86. * @return array
  87. */
  88. public function getOptions()
  89. {
  90. return $this->options;
  91. }
  92. /**
  93. * @param $value
  94. * @return void
  95. */
  96. public function setValue($value)
  97. {
  98. $this->value = $value;
  99. }
  100. /**
  101. * @return mixed
  102. */
  103. public function getValue()
  104. {
  105. return $this->value;
  106. }
  107. /**
  108. * @param $condition
  109. * @return void
  110. */
  111. public function setCondition($condition)
  112. {
  113. $this->condition = $condition;
  114. }
  115. /**
  116. * @return
  117. */
  118. public function getCondition()
  119. {
  120. return $this->condition;
  121. }
  122. }