DatagridMapper.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. public function __construct(DatagridBuilderInterface $datagridBuilder, DatagridInterface $datagrid, AdminInterface $admin)
  27. {
  28. $this->datagridBuilder = $datagridBuilder;
  29. $this->datagrid = $datagrid;
  30. $this->admin = $admin;
  31. }
  32. /**
  33. * @throws \RuntimeException
  34. * @param string $name
  35. * @param array $fieldDescriptionOptions
  36. * @return \Sonata\AdminBundle\Datagrid\FilterInterface
  37. */
  38. public function add($name, array $fieldDescriptionOptions = array())
  39. {
  40. if ($name instanceof FieldDescriptionInterface) {
  41. $fieldDescription = $name;
  42. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  43. } else if (is_string($name) && !$this->admin->hasFormFieldDescription($name)) {
  44. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  45. $this->admin->getClass(),
  46. $name,
  47. $fieldDescriptionOptions
  48. );
  49. $this->datagridBuilder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  50. $this->admin->addListFieldDescription($name, $fieldDescription);
  51. } else if (is_string($name) && $this->admin->hasFormFieldDescription($name)) {
  52. $fieldDescription = $this->admin->getFormFieldDescription($name);
  53. } else {
  54. throw new \RuntimeException('invalid state');
  55. }
  56. // add the field with the FormBuilder
  57. return $this->datagridBuilder->addFilter(
  58. $this->datagrid,
  59. $fieldDescription
  60. );
  61. }
  62. /**
  63. * @param string $name
  64. * @return
  65. */
  66. public function get($name)
  67. {
  68. return $this->datagrid->get($name);
  69. }
  70. /**
  71. * @param string $key
  72. * @return boolean
  73. */
  74. public function has($key)
  75. {
  76. return $this->datagrid->has($key);
  77. }
  78. /**
  79. * @param string $key
  80. * @return void
  81. */
  82. public function remove($key)
  83. {
  84. $this->admin->removeFilterFieldDescription($key);
  85. $this->datagrid->remove($key);
  86. }
  87. }