root/trunk/patForms/Filter/Function.php

Revision 186, 2.1 kB (checked in by schst, 4 years ago)

added a flexible function filter

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms filter that uses any PHP functions to filter values
4  *
5  * Removes leading and trailing whitespace from
6  * user input
7  *
8  * $Id$
9  *
10  * @package        patForms
11  * @subpackage    Filter
12  */
13
14 /**
15  * patForms filter that uses any PHP functions to filter values
16  *
17  * @package        patForms
18  * @subpackage    Filter
19  * @author        Stephan Schmidt <schst@php-tools.net>
20  * @license        LGPL, see license.txt for details
21  * @link        http://www.php-tools.net
22  * @version        1.0
23  */
24 class patForms_Filter_Function extends patForms_Filter
25 {
26    /**
27     * in function
28     *
29     * @access    private
30     * @var        mixed
31     */
32     var $_inFunction = null;
33
34    /**
35     * out function
36     *
37     * @access    private
38     * @var        mixed
39     */
40     var $_outFunction = null;
41
42    /**
43     * type of the filter
44     *
45     * @access    private
46     */
47     var $_type = PATFORMS_FILTER_TYPE_HTTP;
48
49    /**
50     * set the function for incoming data
51     *
52     * @access    public
53     * @param    mixed        function name | array with class/object and function name
54     * @return    boolean
55     */
56     function setInFunction($func)
57     {
58         $this->_inFunction = $func;
59         return true;
60     }
61     
62    /**
63     * set the function for outgoing data
64     *
65     * @access    public
66     * @param    mixed        function name | array with class/object and function name
67     * @return    boolean
68     */
69     function setOutFunction($func)
70     {
71         $this->_outFunction = $func;
72         return true;
73     }
74     
75    /**
76     * Filter value that is returned by patForms
77     *
78     * This method is applied when patForms_Element::getValue()
79     * or patForms::getValues() is called.
80     *
81     * @abstract
82     * @access    public
83     * @param    string    value
84     * @return    float    filtered value
85     */
86     function out( $value )
87     {
88         if (is_null($this->_outFunction)) {
89             return $value;
90         }
91         if (!is_callable($this->_outFunction)) {
92             return $value;
93         }
94         $value = call_user_func($this->_outFunction, $value);
95         return $value;
96     }
97
98    /**
99     * Filter value that is passed to patForms
100     *
101     * @abstract
102     * @access    public
103     * @param    mixed    value
104     * @return    mixed    filtered value
105     */
106     function in( $value )
107     {
108         if (is_null($this->_inFunction)) {
109             return $value;
110         }
111         if (!is_callable($this->_inFunction)) {
112             return $value;
113         }
114         $value = call_user_func($this->_inFunction, $value);
115         return $value;
116     }
117 }
118 ?>
Note: See TracBrowser for help on using the browser.