1<?php
2/**
3 * PHPTAL templating engine
4 *
5 * PHP Version 5
6 *
7 * @category HTML
8 * @package  PHPTAL
9 * @author   Kornel Lesiński <kornel@aardvarkmedia.co.uk>
10 * @license  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
11 * @version  SVN: $Id: $
12 * @link     http://phptal.org/
13 */
14
15/**
16 * Base class for prefilters.
17 *
18 * You should extend this class and override methods you're interested in.
19 *
20 * Order of calls is undefined and may change.
21 *
22 * @package PHPTAL
23 */
24abstract class PHPTAL_PreFilter implements PHPTAL_Filter
25{
26    /**
27     * @see getPHPTAL()
28     */
29    private $phptal;
30
31
32    /**
33     * Receives DOMElement (of PHP5 DOM API) of parsed file (documentElement), or element
34     * that has phptal:filter attribute. Should edit DOM in place.
35     * Prefilters are called only once before template is compiled, so they can be slow.
36     *
37     * Default implementation does nothing. Override it.
38     *
39     * @param DOMElement $node PHP5 DOM node to modify in place
40     *
41     * @return void
42     */
43    public function filterElement(DOMElement $node)
44    {
45    }
46
47    /**
48     * Receives root PHPTAL DOM node of parsed file and should edit it in place.
49     * Prefilters are called only once before template is compiled, so they can be slow.
50     *
51     * Default implementation does nothing. Override it.
52     *
53     * @see PHPTAL_Dom_Element class for methods and fields available.
54     *
55     * @param PHPTAL_Dom_Element $root PHPTAL DOM node to modify in place
56     *
57     * @return void
58     */
59    public function filterDOM(PHPTAL_Dom_Element $root)
60    {
61    }
62
63    /**
64     * Receives DOM node that had phptal:filter attribute calling this filter.
65     * Should modify node in place.
66     * Prefilters are called only once before template is compiled, so they can be slow.
67     *
68     * Default implementation calls filterDOM(). Override it.
69     *
70     * @param PHPTAL_Dom_Element $node PHPTAL DOM node to modify in place
71     *
72     * @return void
73     */
74    public function filterDOMFragment(PHPTAL_Dom_Element $node)
75    {
76        $this->filterDOM($node);
77    }
78
79    /**
80     * Receives template source code and is expected to return new source.
81     * Prefilters are called only once before template is compiled, so they can be slow.
82     *
83     * Default implementation does nothing. Override it.
84     *
85     * @param string $src markup to filter
86     *
87     * @return string
88     */
89    public function filter($src)
90    {
91        return $src;
92    }
93
94    /**
95     * Returns (any) string that uniquely identifies this filter and its settings,
96     * which is used to (in)validate template cache.
97     *
98     * Unlike other filter methods, this one is called on every execution.
99     *
100     * Override this method if result of the filter depends on its configuration.
101     *
102     * @return string
103     */
104    public function getCacheId()
105    {
106        return get_class($this);
107    }
108
109    /**
110     * Returns PHPTAL class instance that is currently using this prefilter.
111     * May return NULL if PHPTAL didn't start filtering yet.
112     *
113     * @return PHPTAL or NULL
114     */
115    final protected function getPHPTAL()
116    {
117        return $this->phptal;
118    }
119
120    /**
121     * Set which instance of PHPTAL is using this filter.
122     * Must be done before calling any filter* methods.
123     *
124     * @param PHPTAL $phptal instance
125     */
126    final function setPHPTAL(PHPTAL $phptal)
127    {
128        $this->phptal = $phptal;
129    }
130}
131
132
133