PreloadedExtension.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Form;
  11. use Symfony\Component\Form\Exception\InvalidArgumentException;
  12. /**
  13. * A form extension with preloaded types, type extensions and type guessers.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class PreloadedExtension implements FormExtensionInterface
  18. {
  19. private $types = [];
  20. private $typeExtensions = [];
  21. private $typeGuesser;
  22. /**
  23. * Creates a new preloaded extension.
  24. *
  25. * @param FormTypeInterface[] $types The types that the extension should support
  26. * @param FormTypeExtensionInterface[][] $typeExtensions The type extensions that the extension should support
  27. */
  28. public function __construct(array $types, array $typeExtensions, FormTypeGuesserInterface $typeGuesser = null)
  29. {
  30. $this->typeExtensions = $typeExtensions;
  31. $this->typeGuesser = $typeGuesser;
  32. foreach ($types as $type) {
  33. $this->types[\get_class($type)] = $type;
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getType(string $name)
  40. {
  41. if (!isset($this->types[$name])) {
  42. throw new InvalidArgumentException(sprintf('The type "%s" can not be loaded by this extension.', $name));
  43. }
  44. return $this->types[$name];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function hasType(string $name)
  50. {
  51. return isset($this->types[$name]);
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getTypeExtensions(string $name)
  57. {
  58. return $this->typeExtensions[$name]
  59. ?? [];
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function hasTypeExtensions(string $name)
  65. {
  66. return !empty($this->typeExtensions[$name]);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getTypeGuesser()
  72. {
  73. return $this->typeGuesser;
  74. }
  75. }