|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Templating; |
|
|
13 |
|
|
|
14 |
/** |
|
|
15 |
* Internal representation of a template. |
|
|
16 |
* |
|
|
17 |
* @author Victor Berchet <victor@suumit.com> |
|
|
18 |
* |
|
|
19 |
* @api |
|
|
20 |
*/ |
|
|
21 |
class TemplateReference implements TemplateReferenceInterface |
|
|
22 |
{ |
|
|
23 |
protected $parameters; |
|
|
24 |
|
|
|
25 |
public function __construct($name = null, $engine = null) |
|
|
26 |
{ |
|
|
27 |
$this->parameters = array( |
|
|
28 |
'name' => $name, |
|
|
29 |
'engine' => $engine, |
|
|
30 |
); |
|
|
31 |
} |
|
|
32 |
|
|
|
33 |
public function __toString() |
|
|
34 |
{ |
|
|
35 |
return $this->getLogicalName(); |
|
|
36 |
} |
|
|
37 |
|
|
|
38 |
/** |
|
|
39 |
* Sets a template parameter. |
|
|
40 |
* |
|
|
41 |
* @param string $name The parameter name |
|
|
42 |
* @param string $value The parameter value |
|
|
43 |
* |
|
|
44 |
* @return TemplateReferenceInterface The TemplateReferenceInterface instance |
|
|
45 |
* |
|
|
46 |
* @throws \InvalidArgumentException if the parameter is not defined |
|
|
47 |
* |
|
|
48 |
* @api |
|
|
49 |
*/ |
|
|
50 |
public function set($name, $value) |
|
|
51 |
{ |
|
|
52 |
if (array_key_exists($name, $this->parameters)) { |
|
|
53 |
$this->parameters[$name] = $value; |
|
|
54 |
} else { |
|
|
55 |
throw new \InvalidArgumentException(sprintf('The template does not support the "%s" parameter.', $name)); |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
return $this; |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
/** |
|
|
62 |
* Gets a template parameter. |
|
|
63 |
* |
|
|
64 |
* @param string $name The parameter name |
|
|
65 |
* |
|
|
66 |
* @return string The parameter value |
|
|
67 |
* |
|
|
68 |
* @throws \InvalidArgumentException if the parameter is not defined |
|
|
69 |
* |
|
|
70 |
* @api |
|
|
71 |
*/ |
|
|
72 |
public function get($name) |
|
|
73 |
{ |
|
|
74 |
if (array_key_exists($name, $this->parameters)) { |
|
|
75 |
return $this->parameters[$name]; |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
throw new \InvalidArgumentException(sprintf('The template does not support the "%s" parameter.', $name)); |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
/** |
|
|
82 |
* Gets the template parameters. |
|
|
83 |
* |
|
|
84 |
* @return array An array of parameters |
|
|
85 |
* |
|
|
86 |
* @api |
|
|
87 |
*/ |
|
|
88 |
public function all() |
|
|
89 |
{ |
|
|
90 |
return $this->parameters; |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
/** |
|
|
94 |
* Returns the path to the template. |
|
|
95 |
* |
|
|
96 |
* By default, it just returns the template name. |
|
|
97 |
* |
|
|
98 |
* @return string A path to the template or a resource |
|
|
99 |
* |
|
|
100 |
* @api |
|
|
101 |
*/ |
|
|
102 |
public function getPath() |
|
|
103 |
{ |
|
|
104 |
return $this->parameters['name']; |
|
|
105 |
} |
|
|
106 |
|
|
|
107 |
/** |
|
|
108 |
* Returns the "logical" template name. |
|
|
109 |
* |
|
|
110 |
* The template name acts as a unique identifier for the template. |
|
|
111 |
* |
|
|
112 |
* @return string The template name |
|
|
113 |
* |
|
|
114 |
* @api |
|
|
115 |
*/ |
|
|
116 |
public function getLogicalName() |
|
|
117 |
{ |
|
|
118 |
return $this->parameters['name']; |
|
|
119 |
} |
|
|
120 |
} |