PreloadedExtension.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 = array();
  20. private $typeExtensions = array();
  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. * @param FormTypeGuesserInterface|null $typeGuesser The guesser that the extension should support
  28. */
  29. public function __construct(array $types, array $typeExtensions, FormTypeGuesserInterface $typeGuesser = null)
  30. {
  31. $this->typeExtensions = $typeExtensions;
  32. $this->typeGuesser = $typeGuesser;
  33. foreach ($types as $type) {
  34. // Up to Symfony 2.8, types were identified by their names
  35. $this->types[$type->getName()] = $type;
  36. // Since Symfony 2.8, types are identified by their FQCN
  37. $this->types[\get_class($type)] = $type;
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getType($name)
  44. {
  45. if (!isset($this->types[$name])) {
  46. throw new InvalidArgumentException(sprintf('The type "%s" can not be loaded by this extension', $name));
  47. }
  48. return $this->types[$name];
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function hasType($name)
  54. {
  55. return isset($this->types[$name]);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getTypeExtensions($name)
  61. {
  62. return isset($this->typeExtensions[$name])
  63. ? $this->typeExtensions[$name]
  64. : array();
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function hasTypeExtensions($name)
  70. {
  71. return !empty($this->typeExtensions[$name]);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getTypeGuesser()
  77. {
  78. return $this->typeGuesser;
  79. }
  80. }