TemplateReference.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Templating;
  11. /**
  12. * Internal representation of a template.
  13. *
  14. * @author Victor Berchet <victor@suumit.com>
  15. *
  16. * @api
  17. */
  18. class TemplateReference implements TemplateReferenceInterface
  19. {
  20. protected $parameters;
  21. public function __construct($name = null, $engine = null)
  22. {
  23. $this->parameters = array(
  24. 'name' => $name,
  25. 'engine' => $engine,
  26. );
  27. }
  28. public function __toString()
  29. {
  30. return $this->getLogicalName();
  31. }
  32. /**
  33. * Sets a template parameter.
  34. *
  35. * @param string $name The parameter name
  36. * @param string $value The parameter value
  37. *
  38. * @return TemplateReferenceInterface The TemplateReferenceInterface instance
  39. *
  40. * @throws \InvalidArgumentException if the parameter is not defined
  41. *
  42. * @api
  43. */
  44. public function set($name, $value)
  45. {
  46. if (array_key_exists($name, $this->parameters)) {
  47. $this->parameters[$name] = $value;
  48. } else {
  49. throw new \InvalidArgumentException(sprintf('The template does not support the "%s" parameter.', $name));
  50. }
  51. return $this;
  52. }
  53. /**
  54. * Gets a template parameter.
  55. *
  56. * @param string $name The parameter name
  57. *
  58. * @return string The parameter value
  59. *
  60. * @throws \InvalidArgumentException if the parameter is not defined
  61. *
  62. * @api
  63. */
  64. public function get($name)
  65. {
  66. if (array_key_exists($name, $this->parameters)) {
  67. return $this->parameters[$name];
  68. }
  69. throw new \InvalidArgumentException(sprintf('The template does not support the "%s" parameter.', $name));
  70. }
  71. /**
  72. * Gets the template parameters.
  73. *
  74. * @return array An array of parameters
  75. *
  76. * @api
  77. */
  78. public function all()
  79. {
  80. return $this->parameters;
  81. }
  82. /**
  83. * Returns the path to the template.
  84. *
  85. * By default, it just returns the template name.
  86. *
  87. * @return string A path to the template or a resource
  88. *
  89. * @api
  90. */
  91. public function getPath()
  92. {
  93. return $this->parameters['name'];
  94. }
  95. /**
  96. * Returns the "logical" template name.
  97. *
  98. * The template name acts as a unique identifier for the template.
  99. *
  100. * @return string The template name
  101. *
  102. * @api
  103. */
  104. public function getLogicalName()
  105. {
  106. return $this->parameters['name'];
  107. }
  108. }