PreloadedExtension.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 exceptions and type guessers.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class PreloadedExtension implements FormExtensionInterface
  18. {
  19. /**
  20. * @var array
  21. */
  22. private $types = array();
  23. /**
  24. * @var array
  25. */
  26. private $typeExtensions = array();
  27. /**
  28. * @var FormTypeGuesserInterface
  29. */
  30. private $typeGuesser;
  31. /**
  32. * Creates a new preloaded extension.
  33. *
  34. * @param array $types The types that the extension should support.
  35. * @param array $typeExtensions The type extensions that the extension should support.
  36. * @param FormTypeGuesserInterface|null $typeGuesser The guesser that the extension should support.
  37. */
  38. public function __construct(array $types, array $typeExtensions, FormTypeGuesserInterface $typeGuesser = null)
  39. {
  40. $this->types = $types;
  41. $this->typeExtensions = $typeExtensions;
  42. $this->typeGuesser = $typeGuesser;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getType($name)
  48. {
  49. if (!isset($this->types[$name])) {
  50. throw new InvalidArgumentException(sprintf('The type "%s" can not be loaded by this extension', $name));
  51. }
  52. return $this->types[$name];
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function hasType($name)
  58. {
  59. return isset($this->types[$name]);
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getTypeExtensions($name)
  65. {
  66. return isset($this->typeExtensions[$name])
  67. ? $this->typeExtensions[$name]
  68. : array();
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function hasTypeExtensions($name)
  74. {
  75. return !empty($this->typeExtensions[$name]);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getTypeGuesser()
  81. {
  82. return $this->typeGuesser;
  83. }
  84. }