Filter.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /**
  18. * @param string $name
  19. * @param array $options
  20. */
  21. public function initialize($name, array $options = array())
  22. {
  23. $this->name = $name;
  24. $this->setOptions($options);
  25. }
  26. /**
  27. * @return string
  28. */
  29. public function getName()
  30. {
  31. return $this->name;
  32. }
  33. /**
  34. * @return array
  35. */
  36. public function getDefaultOptions()
  37. {
  38. return array();
  39. }
  40. /**
  41. * @param string $name
  42. * @param null $default
  43. * @return mixed
  44. */
  45. public function getOption($name, $default = null)
  46. {
  47. if (array_key_exists($name, $this->options)) {
  48. return $this->options[$name];
  49. }
  50. return $default;
  51. }
  52. /**
  53. * @return string
  54. */
  55. public function getFieldType()
  56. {
  57. return $this->getOption('field_type', 'text');
  58. }
  59. /**
  60. * @return array
  61. */
  62. public function getFieldOptions()
  63. {
  64. return $this->getOption('field_options', array('required' => false));
  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. }