PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/sonata-project/admin-bundle/Datagrid/DatagridMapper.php

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