This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Templating/TemplateReference.php

117 lines
2.7 KiB
PHP
Raw Normal View History

2011-02-10 17:20:44 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
2011-02-10 17:20:44 +00:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating;
/**
* Internal representation of a template.
*
* @author Victor Berchet <victor@suumit.com>
*/
class TemplateReference implements TemplateReferenceInterface
{
protected $parameters;
public function __construct($name = null, $engine = null)
{
$this->parameters = array(
'name' => $name,
'engine' => $engine,
);
}
public function __toString()
{
return json_encode($this->parameters);
}
/**
2011-04-08 10:24:28 +01:00
* Returns the template signature
*
* @return string A UID for the template
2011-02-10 17:20:44 +00:00
*/
public function getSignature()
{
return md5(serialize($this->parameters));
}
/**
2011-04-08 10:24:28 +01:00
* Sets a template parameter.
*
* @param string $name The parameter name
* @param string $value The parameter value
*
* @return TemplateReferenceInterface The TemplateReferenceInterface instance
*
* @throws \InvalidArgumentException if the parameter is not defined
2011-02-10 17:20:44 +00:00
*/
public function set($name, $value)
{
if (array_key_exists($name, $this->parameters)) {
$this->parameters[$name] = $value;
} else {
throw new \InvalidArgumentException(sprintf('The template does not support the "%s" parameter.', $name));
}
return $this;
}
/**
2011-04-08 10:24:28 +01:00
* Gets a template parameter.
*
* @param string $name The parameter name
*
* @return string The parameter value
*
* @throws \InvalidArgumentException if the parameter is not defined
2011-02-10 17:20:44 +00:00
*/
public function get($name)
{
if (array_key_exists($name, $this->parameters)) {
return $this->parameters[$name];
}
2011-02-27 17:29:20 +00:00
throw new \InvalidArgumentException(sprintf('The template does not support the "%s" parameter.', $name));
2011-02-10 17:20:44 +00:00
}
/**
2011-04-08 10:24:28 +01:00
* Gets the template parameters.
*
* @return array An array of parameters
2011-02-10 17:20:44 +00:00
*/
public function all()
{
return $this->parameters;
}
/**
2011-04-08 10:24:28 +01:00
* Returns the path to the template.
*
* By default, it just returns the template name.
*
* @return string A path to the template or a resource
*/
public function getPath()
{
return $this->parameters['name'];
}
/**
2011-04-08 10:24:28 +01:00
* Returns the template name
*
* @return string The template name
*/
public function getLogicalName()
{
return $this->parameters['name'];
}
2011-02-10 17:20:44 +00:00
}