DatagridMapper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. */
  11. namespace Sonata\AdminBundle\Datagrid;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  14. use Sonata\AdminBundle\Model\ModelManagerInterface;
  15. use Sonata\AdminBundle\Datagrid\DatagridInterface;
  16. use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
  17. /**
  18. * This class is use to simulate the Form API
  19. *
  20. */
  21. class DatagridMapper
  22. {
  23. protected $datagridBuilder;
  24. protected $datagrid;
  25. protected $admin;
  26. /**
  27. * @param \Sonata\AdminBundle\Builder\DatagridBuilderInterface $datagridBuilder
  28. * @param DatagridInterface $datagrid
  29. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  30. */
  31. public function __construct(DatagridBuilderInterface $datagridBuilder, DatagridInterface $datagrid, AdminInterface $admin)
  32. {
  33. $this->datagridBuilder = $datagridBuilder;
  34. $this->datagrid = $datagrid;
  35. $this->admin = $admin;
  36. }
  37. /**
  38. * @throws \RuntimeException
  39. * @param $name
  40. * @param null $type
  41. * @param array $filterOptions
  42. * @param null $fieldType
  43. * @param array $fieldOptions
  44. * @return DatagridMapper
  45. */
  46. public function add($name, $type = null, array $filterOptions = array(), $fieldType = null, $fieldOptions = null)
  47. {
  48. if (is_array($fieldOptions)) {
  49. $filterOptions['field_options'] = $fieldOptions;
  50. }
  51. if ($fieldType) {
  52. $filterOptions['field_type'] = $fieldType;
  53. }
  54. $filterOptions['field_name'] = isset($filterOptions['field_name']) ? $filterOptions['field_name'] : $name;
  55. if ($name instanceof FieldDescriptionInterface) {
  56. $fieldDescription = $name;
  57. $fieldDescription->mergeOptions($filterOptions);
  58. } else if (is_string($name) && !$this->admin->hasFilterFieldDescription($name)) {
  59. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  60. $this->admin->getClass(),
  61. $name,
  62. $filterOptions
  63. );
  64. } else {
  65. throw new \RuntimeException('invalid state');
  66. }
  67. // add the field with the DatagridBuilder
  68. $this->datagridBuilder->addFilter($this->datagrid, $type, $fieldDescription, $this->admin);
  69. return $this;
  70. }
  71. /**
  72. * @param string $name
  73. * @return FilterInterface
  74. */
  75. public function get($name)
  76. {
  77. return $this->datagrid->get($name);
  78. }
  79. /**
  80. * @param string $key
  81. * @return boolean
  82. */
  83. public function has($key)
  84. {
  85. return $this->datagrid->has($key);
  86. }
  87. /**
  88. * @param string $key
  89. * @return void
  90. */
  91. public function remove($key)
  92. {
  93. $this->admin->removeFilterFieldDescription($key);
  94. $this->datagrid->remove($key);
  95. }
  96. }