DatagridMapper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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\Datagrid;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  13. use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
  14. use Sonata\AdminBundle\Mapper\BaseMapper;
  15. /**
  16. * This class is use to simulate the Form API.
  17. *
  18. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  19. */
  20. class DatagridMapper extends BaseMapper
  21. {
  22. /**
  23. * @var DatagridInterface
  24. */
  25. protected $datagrid;
  26. /**
  27. * @param DatagridBuilderInterface $datagridBuilder
  28. * @param DatagridInterface $datagrid
  29. * @param AdminInterface $admin
  30. */
  31. public function __construct(DatagridBuilderInterface $datagridBuilder, DatagridInterface $datagrid, AdminInterface $admin)
  32. {
  33. parent::__construct($datagridBuilder, $admin);
  34. $this->datagrid = $datagrid;
  35. }
  36. /**
  37. * @throws \RuntimeException
  38. *
  39. * @param string $name
  40. * @param string $type
  41. * @param array $filterOptions
  42. * @param string $fieldType
  43. * @param array $fieldOptions
  44. * @param array $fieldDescriptionOptions
  45. *
  46. * @return DatagridMapper
  47. */
  48. public function add($name, $type = null, array $filterOptions = array(), $fieldType = null, $fieldOptions = null, array $fieldDescriptionOptions = array())
  49. {
  50. if (is_array($fieldOptions)) {
  51. $filterOptions['field_options'] = $fieldOptions;
  52. }
  53. if ($fieldType) {
  54. $filterOptions['field_type'] = $fieldType;
  55. }
  56. if ($name instanceof FieldDescriptionInterface) {
  57. $fieldDescription = $name;
  58. $fieldDescription->mergeOptions($filterOptions);
  59. } elseif (is_string($name)) {
  60. if ($this->admin->hasFilterFieldDescription($name)) {
  61. throw new \RuntimeException(sprintf('Duplicate field name "%s" in datagrid mapper. Names should be unique.', $name));
  62. }
  63. if (!isset($filterOptions['field_name'])) {
  64. $filterOptions['field_name'] = substr(strrchr('.'.$name, '.'), 1);
  65. }
  66. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  67. $this->admin->getClass(),
  68. $name,
  69. array_merge($filterOptions, $fieldDescriptionOptions)
  70. );
  71. } else {
  72. throw new \RuntimeException(
  73. 'Unknown field name in datagrid mapper.'
  74. .' Field name should be either of FieldDescriptionInterface interface or string.'
  75. );
  76. }
  77. // add the field with the DatagridBuilder
  78. $this->builder->addFilter($this->datagrid, $type, $fieldDescription, $this->admin);
  79. return $this;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function get($name)
  85. {
  86. return $this->datagrid->getFilter($name);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function has($key)
  92. {
  93. return $this->datagrid->hasFilter($key);
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. final public function keys()
  99. {
  100. return array_keys($this->datagrid->getFilters());
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function remove($key)
  106. {
  107. $this->admin->removeFilterFieldDescription($key);
  108. $this->datagrid->removeFilter($key);
  109. return $this;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function reorder(array $keys)
  115. {
  116. $this->datagrid->reorderFilters($keys);
  117. return $this;
  118. }
  119. }