1<?php
2
3/**
4 * Smarty Method Append
5 *
6 * Smarty::append() method
7 *
8 * @package    Smarty
9 * @subpackage PluginsInternal
10 * @author     Uwe Tews
11 */
12class Smarty_Internal_Method_Append
13{
14    /**
15     * Valid for all objects
16     *
17     * @var int
18     */
19    public $objMap = 7;
20
21    /**
22     * appends values to template variables
23     *
24     * @api  Smarty::append()
25     * @link http://www.smarty.net/docs/en/api.append.tpl
26     *
27     * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
28     * @param  array|string                                           $tpl_var the template variable name(s)
29     * @param  mixed                                                  $value   the value to append
30     * @param  bool                                                   $merge   flag if array elements shall be merged
31     * @param  bool                                                   $nocache if true any output of this variable will
32     *                                                                         be not cached
33     *
34     * @return \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty
35     */
36    public function append(Smarty_Internal_Data $data, $tpl_var, $value = null, $merge = false, $nocache = false)
37    {
38        if (is_array($tpl_var)) {
39            // $tpl_var is an array, ignore $value
40            foreach ($tpl_var as $_key => $_val) {
41                if ($_key != '') {
42                    $this->append($data, $_key, $_val, $merge, $nocache);
43                }
44            }
45        } else {
46            if ($tpl_var != '' && isset($value)) {
47                if (!isset($data->tpl_vars[ $tpl_var ])) {
48                    $tpl_var_inst = $data->ext->getTemplateVars->_getVariable($data, $tpl_var, null, true, false);
49                    if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
50                        $data->tpl_vars[ $tpl_var ] = new Smarty_Variable(null, $nocache);
51                    } else {
52                        $data->tpl_vars[ $tpl_var ] = clone $tpl_var_inst;
53                    }
54                }
55                if (!(is_array($data->tpl_vars[ $tpl_var ]->value) ||
56                      $data->tpl_vars[ $tpl_var ]->value instanceof ArrayAccess)
57                ) {
58                    settype($data->tpl_vars[ $tpl_var ]->value, 'array');
59                }
60                if ($merge && is_array($value)) {
61                    foreach ($value as $_mkey => $_mval) {
62                        $data->tpl_vars[ $tpl_var ]->value[ $_mkey ] = $_mval;
63                    }
64                } else {
65                    $data->tpl_vars[ $tpl_var ]->value[] = $value;
66                }
67            }
68            if ($data->_isTplObj() && $data->scope) {
69                $data->ext->_updateScope->_updateScope($data, $tpl_var);
70            }
71        }
72        return $data;
73    }
74}