Forms.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Grav\Plugin\Form;
  3. use Grav\Common\Page\Interfaces\PageInterface;
  4. use Grav\Common\Page\Page;
  5. use Grav\Framework\Form\Interfaces\FormFactoryInterface;
  6. use Grav\Framework\Form\Interfaces\FormInterface;
  7. class Forms
  8. {
  9. /** @var array|FormFactoryInterface[] */
  10. private $types;
  11. /** @var FormInterface|null */
  12. private $form;
  13. /**
  14. * Forms constructor.
  15. */
  16. public function __construct()
  17. {
  18. $this->registerType('form', new FormFactory());
  19. }
  20. /**
  21. * @param string $type
  22. * @param FormFactoryInterface $factory
  23. */
  24. public function registerType(string $type, FormFactoryInterface $factory): void
  25. {
  26. $this->types[$type] = $factory;
  27. }
  28. /**
  29. * @param string $type
  30. */
  31. public function unregisterType($type): void
  32. {
  33. unset($this->types[$type]);
  34. }
  35. /**
  36. * @param string $type
  37. * @return bool
  38. */
  39. public function hasType(string $type): bool
  40. {
  41. return isset($this->types[$type]);
  42. }
  43. /**
  44. * @return array
  45. */
  46. public function getTypes(): array
  47. {
  48. return array_keys($this->types);
  49. }
  50. /**
  51. * @param PageInterface $page
  52. * @param string|null $name
  53. * @param array|null $form
  54. * @return FormInterface|null
  55. */
  56. public function createPageForm(PageInterface $page, string $name = null, array $form = null): ?FormInterface
  57. {
  58. if (null === $form) {
  59. [$name, $form] = $this->getPageParameters($page, $name);
  60. }
  61. if (null === $form) {
  62. return null;
  63. }
  64. $type = $form['type'] ?? 'form';
  65. $factory = $this->types[$type] ?? null;
  66. if ($factory) {
  67. if (is_callable([$factory, 'createFormForPage'])) {
  68. return $factory->createFormForPage($page, $name, $form);
  69. }
  70. if ($page instanceof Page) {
  71. // @phpstan-ignore-next-line
  72. return $factory->createPageForm($page, $name, $form);
  73. }
  74. }
  75. return null;
  76. }
  77. /**
  78. * @return FormInterface|null
  79. */
  80. public function getActiveForm(): ?FormInterface
  81. {
  82. return $this->form;
  83. }
  84. /**
  85. * @param FormInterface $form
  86. * @return void
  87. */
  88. public function setActiveForm(FormInterface $form): void
  89. {
  90. $this->form = $form;
  91. }
  92. /**
  93. * @param PageInterface $page
  94. * @param string|null $name
  95. * @return array
  96. */
  97. protected function getPageParameters(PageInterface $page, ?string $name): array
  98. {
  99. $forms = $page->forms();
  100. if ($name) {
  101. // If form with given name was found, use that.
  102. $form = $forms[$name] ?? null;
  103. } else {
  104. // Otherwise pick up the first form.
  105. $form = reset($forms) ?: null;
  106. $name = (string)key($forms);
  107. }
  108. return [$name, $form];
  109. }
  110. }