3 * Smarty Internal Plugin CompileBase
11 * This class does extend all internal compile plugins
14 * @subpackage Compiler
16 abstract class Smarty_Internal_CompileBase
19 * Array of names of required attribute required by tag
23 public $required_attributes = array();
26 * Array of names of optional attribute required by tag
27 * use array('_any') if there is no restriction of attributes names
31 public $optional_attributes = array();
34 * Shorttag attribute order defined by its names
38 public $shorttag_order = array();
41 * Array of names of valid option flags
45 public $option_flags = array('nocache');
48 * Mapping array for boolqn option value
52 public $optionMap = array(1 => true, 0 => false, 'true' => true, 'false' => false);
55 * Mapping array with attributes as key
59 public $mapCache = array();
62 * This function checks if the attributes passed are valid
63 * The attributes passed for the tag to compile are checked against the list of required and
64 * optional attributes. Required attributes must be present. Optional attributes are check against
65 * the corresponding list. The keyword '_any' specifies that any attribute will be accepted
68 * @param object $compiler compiler object
69 * @param array $attributes attributes applied to the tag
71 * @return array of mapped attributes for further processing
73 public function getAttributes($compiler, $attributes)
75 $_indexed_attr = array();
76 if (!isset($this->mapCache[ 'option' ])) {
77 $this->mapCache[ 'option' ] = array_fill_keys($this->option_flags, true);
79 foreach ($attributes as $key => $mixed) {
81 if (!is_array($mixed)) {
83 if (isset($this->mapCache[ 'option' ][ trim($mixed, '\'"') ])) {
84 $_indexed_attr[ trim($mixed, '\'"') ] = true;
85 // shorthand attribute ?
86 } elseif (isset($this->shorttag_order[ $key ])) {
87 $_indexed_attr[ $this->shorttag_order[ $key ] ] = $mixed;
89 // too many shorthands
90 $compiler->trigger_template_error('too many shorthand attributes', null, true);
94 foreach ($mixed as $k => $v) {
96 if (isset($this->mapCache[ 'option' ][ $k ])) {
98 $_indexed_attr[ $k ] = $v;
101 $v = trim($v, '\'" ');
103 if (isset($this->optionMap[ $v ])) {
104 $_indexed_attr[ $k ] = $this->optionMap[ $v ];
106 $compiler->trigger_template_error("illegal value '" . var_export($v, true) .
107 "' for option flag '{$k}'", null, true);
110 // must be named attribute
112 $_indexed_attr[ $k ] = $v;
117 // check if all required attributes present
118 foreach ($this->required_attributes as $attr) {
119 if (!isset($_indexed_attr[ $attr ])) {
120 $compiler->trigger_template_error("missing \"" . $attr . "\" attribute", null, true);
123 // check for not allowed attributes
124 if ($this->optional_attributes != array('_any')) {
125 if (!isset($this->mapCache[ 'all' ])) {
126 $this->mapCache[ 'all' ] =
127 array_fill_keys(array_merge($this->required_attributes, $this->optional_attributes,
128 $this->option_flags), true);
130 foreach ($_indexed_attr as $key => $dummy) {
131 if (!isset($this->mapCache[ 'all' ][ $key ]) && $key !== 0) {
132 $compiler->trigger_template_error("unexpected \"" . $key . "\" attribute", null, true);
136 // default 'false' for all option flags not set
137 foreach ($this->option_flags as $flag) {
138 if (!isset($_indexed_attr[ $flag ])) {
139 $_indexed_attr[ $flag ] = false;
142 if (isset($_indexed_attr[ 'nocache' ]) && $_indexed_attr[ 'nocache' ]) {
143 $compiler->tag_nocache = true;
145 return $_indexed_attr;
149 * Push opening tag name on stack
150 * Optionally additional data can be saved on stack
152 * @param object $compiler compiler object
153 * @param string $openTag the opening tag's name
154 * @param mixed $data optional data saved
156 public function openTag($compiler, $openTag, $data = null)
158 array_push($compiler->_tag_stack, array($openTag, $data));
163 * Raise an error if this stack-top doesn't match with expected opening tags
165 * @param object $compiler compiler object
166 * @param array|string $expectedTag the expected opening tag names
168 * @return mixed any type the opening tag's name or saved data
170 public function closeTag($compiler, $expectedTag)
172 if (count($compiler->_tag_stack) > 0) {
174 list($_openTag, $_data) = array_pop($compiler->_tag_stack);
175 // open tag must match with the expected ones
176 if (in_array($_openTag, (array) $expectedTag)) {
177 if (is_null($_data)) {
178 // return opening tag
181 // return restored data
185 // wrong nesting of tags
186 $compiler->trigger_template_error("unclosed {$compiler->smarty->left_delimiter}" . $_openTag .
187 "{$compiler->smarty->right_delimiter} tag");
191 // wrong nesting of tags
192 $compiler->trigger_template_error("unexpected closing tag", null, true);