ListMapper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Admin\FieldDescriptionCollection;
  16. use Sonata\AdminBundle\Builder\ListBuilderInterface;
  17. /**
  18. * This class is used to simulate the Form API
  19. *
  20. */
  21. class ListMapper
  22. {
  23. protected $listBuilder;
  24. protected $list;
  25. protected $admin;
  26. public function __construct(ListBuilderInterface $listBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
  27. {
  28. $this->listBuilder = $listBuilder;
  29. $this->list = $list;
  30. $this->admin = $admin;
  31. }
  32. public function addIdentifier($name, $type = null, array $fieldDescriptionOptions = array())
  33. {
  34. $fieldDescriptionOptions['identifier'] = true;
  35. if (!isset($fieldDescriptionOptions['route'])) {
  36. $fieldDescriptionOptions['route'] = 'edit';
  37. }
  38. return $this->add($name, $type, $fieldDescriptionOptions);
  39. }
  40. /**
  41. * @throws \RuntimeException
  42. *
  43. * @param mixed $name
  44. * @param mixed $type
  45. * @param array $fieldDescriptionOptions
  46. * @return \Sonata\AdminBundle\Datagrid\ListMapper
  47. */
  48. public function add($name, $type = null, array $fieldDescriptionOptions = array())
  49. {
  50. if ($name instanceof FieldDescriptionInterface) {
  51. $fieldDescription = $name;
  52. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  53. } else if (is_string($name) && !$this->admin->hasListFieldDescription($name)) {
  54. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  55. $this->admin->getClass(),
  56. $name,
  57. $fieldDescriptionOptions
  58. );
  59. } else {
  60. throw new \RuntimeException('invalid state');
  61. }
  62. if (!$fieldDescription->getLabel()) {
  63. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label'));
  64. }
  65. // add the field with the FormBuilder
  66. $this->listBuilder->addField($this->list, $type, $fieldDescription, $this->admin);
  67. return $this;
  68. }
  69. /**
  70. * @param string $name
  71. * @return array
  72. */
  73. public function get($name)
  74. {
  75. return $this->list->get($name);
  76. }
  77. /**
  78. * @param string $key
  79. * @return bool
  80. */
  81. public function has($key)
  82. {
  83. return $this->list->has($key);
  84. }
  85. /**
  86. * @param string $key
  87. * @return \Sonata\AdminBundle\Datagrid\ListMapper
  88. */
  89. public function remove($key)
  90. {
  91. $this->admin->removeListFieldDescription($key);
  92. $this->list->remove($key);
  93. return $this;
  94. }
  95. }