1<?php
2
3/**
4 * Smarty Method SetAutoloadFilters
5 *
6 * Smarty::setAutoloadFilters() method
7 *
8 * @package    Smarty
9 * @subpackage PluginsInternal
10 * @author     Uwe Tews
11 */
12class Smarty_Internal_Method_SetAutoloadFilters
13{
14    /**
15     * Valid for Smarty and template object
16     *
17     * @var int
18     */
19    public $objMap = 3;
20
21    /**
22     * Valid filter types
23     *
24     * @var array
25     */
26    private $filterTypes = array('pre' => true, 'post' => true, 'output' => true, 'variable' => true);
27
28    /**
29     * Set autoload filters
30     *
31     * @api Smarty::setAutoloadFilters()
32     *
33     * @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
34     * @param array                                                           $filters filters to load automatically
35     * @param string                                                          $type    "pre", "output", … specify
36     *                                                                                 the filter type to set.
37     *                                                                                 Defaults to none treating
38     *                                                                                 $filters' keys as the
39     *                                                                                 appropriate types
40     *
41     * @return \Smarty|\Smarty_Internal_Template
42     * @throws \SmartyException
43     */
44    public function setAutoloadFilters(Smarty_Internal_TemplateBase $obj, $filters, $type = null)
45    {
46        $smarty = $obj->_getSmartyObj();
47        if ($type !== null) {
48            $this->_checkFilterType($type);
49            $smarty->autoload_filters[ $type ] = (array)$filters;
50        } else {
51            foreach ((array)$filters as $type => $value) {
52                $this->_checkFilterType($type);
53            }
54            $smarty->autoload_filters = (array)$filters;
55        }
56        return $obj;
57    }
58
59    /**
60     * Check if filter type is valid
61     *
62     * @param string $type
63     *
64     * @throws \SmartyException
65     */
66    public function _checkFilterType($type)
67    {
68        if (!isset($this->filterTypes[ $type ])) {
69            throw new SmartyException("Illegal filter type '{$type}'");
70        }
71    }
72}
73