Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * This file contains the class XML_Query2XML_Data_Source_PHPCallback.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  XML
8
 * @package   XML_Query2XML
9
 * @author    Lukas Feiler <lukas.feiler@lukasfeiler.com>
10
 * @copyright 2009 Lukas Feiler
11
 * @license   http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
12
 * @version   CVS: $Id: PHPCallback.php 302587 2010-08-20 23:53:59Z clockwerx $
13
 * @link      http://pear.php.net/package/XML_Query2XML
14
 * @access    private
15
 */
16
 
17
/**
18
 * XML_Query2XML_Data_Source_PHPCallback extends the class
19
 * XML_Query2XML_Data_Source.
20
 */
21
require_once 'XML/Query2XML/Data/Source.php';
22
 
23
 
24
/**
25
 * Data Source Class that invokes a callback function, using the return value as the
26
 * data source.
27
 *
28
 * This command class does not accept a pre-processor.
29
 *
30
 * usage:
31
 * <code>
32
 * function myFunction($record) { ... }
33
 * $commandObject = new XML_Query2XML_Data_Source_PHPCallback('myFunction');
34
 * </code>
35
 *
36
 * @category  XML
37
 * @package   XML_Query2XML
38
 * @author    Lukas Feiler <lukas.feiler@lukasfeiler.com>
39
 * @copyright 2006 Lukas Feiler
40
 * @license   http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
41
 * @version   Release: 1.7.2
42
 * @link      http://pear.php.net/package/XML_Query2XML
43
 * @access    private
44
 * @since     Release 1.7.1RC1
45
 */
46
class XML_Query2XML_Data_Source_PHPCallback extends XML_Query2XML_Data_Source
47
{
48
    /**
49
     * A pseudo-type callback.
50
     * @var mixed A string or an array.
51
     */
52
    private $_callback = null;
53
 
54
    /**
55
     * The string definition of the callback
56
     * as it was passed to the constructor.
57
     * This will be returned by toString().
58
     * @var string
59
     */
60
    private $_callbackString = '';
61
 
62
    /**
63
     * The arguments to be bassed to the callback function.
64
     * @var array An index array of arguments.
65
     */
66
    private $_args = array();
67
 
68
    /**
69
     * Constructor
70
     *
71
     * The following formats are supported for $callback:
72
     * - 'myFunction'
73
     * - 'myFunction(arg1, arg2, ...)'
74
     * - 'MyClass::myStaticMethod'
75
     * - 'MyClass::myStaticMethod(arg1, arg2, ...)'
76
     * You can also pass additional string arguments to the callback function by
77
     * specifing them within the opening and closing brace; e.g. 'Utils::limit(12)'
78
     * will result in Util::limit() being called by execute() with the $record as
79
     * the first and '12' as the second argument.
80
     * If you do not want to pass additional arguments to the callback function,
81
     * the opening and closing brace are optional.
82
     *
83
     * @param string $callback The callback as a string.
84
     *
85
     * @throws XML_Query2XML_ConfigException If $callback is not callable.
86
     */
87
    public function __construct($callback)
88
    {
89
        $this->_callbackString = $callback;
90
 
91
        $braceOpen = strpos($callback, '(');
92
        if ($braceOpen !== false) {
93
            $braceClose = strpos($callback, ')');
94
            if ($braceOpen + 1 < $braceClose) {
95
                $argsString  = substr(
96
                    $callback, $braceOpen + 1, $braceClose - $braceOpen - 1
97
                );
98
                $this->_args = explode(',', str_replace(', ', ',', $argsString));
99
            }
100
            if ($braceOpen < $braceClose) {
101
                $callback = substr($callback, 0, $braceOpen);
102
            }
103
        }
104
        if (strpos($callback, '::') !== false) {
105
            $callback = explode('::', $callback);
106
        }
107
        if (!is_callable($callback, false, $callableName)) {
108
            /*
109
            * unit tests: _applyColumnStringToRecord/
110
            *  throwConfigException_callback_function1.phpt
111
            *  throwConfigException_callback_function2.phpt
112
            *  throwConfigException_callback_method1.phpt
113
            *  throwConfigException_callback_method2.phpt
114
            */
115
            throw new XML_Query2XML_ConfigException(
116
                $this->getConfigPath() . 'The method/function "'
117
                . $callableName . '" is not callable.'
118
            );
119
        }
120
        $this->_callback = $callback;
121
    }
122
 
123
    /**
124
     * Creates a new instance of this class.
125
     * This method is called by XML_Query2XML.
126
     *
127
     * @param string $callback   The callback as a string.
128
     * @param string $configPath The configuration path within the $options array.
129
     *
130
     * @return XML_Query2XML_Data_Source_PHPCallback
131
     */
132
    public function create($callback, $configPath)
133
    {
134
        $source = new XML_Query2XML_Data_Source_PHPCallback($callback);
135
        $source->setConfigPath($configPath);
136
        return $source;
137
    }
138
 
139
    /**
140
     * Called by XML_Query2XML for every record in the result set.
141
     *
142
     * @param array $record An associative array.
143
     *
144
     * @return mixed Whatever the callback function returned.
145
     */
146
    public function execute(array $record)
147
    {
148
        return call_user_func_array(
149
            $this->_callback,
150
            array_merge(array($record), $this->_args)
151
        );
152
    }
153
 
154
    /**
155
     * This method is called by XML_Query2XML in case the asterisk shortcut was used.
156
     *
157
     * The interface XML_Query2XML_Data_Source requires an implementation of
158
     * this method.
159
     *
160
     * @param string $columnName The column name that is to replace every occurance
161
     *                           of the asterisk character '*' in any of the
162
     *                           arguments specified in the
163
     *                           'myFunction(arg1, arg2, ...)' or
164
     *                           'MyClass::myStaticMethod(arg1, arg2, ...)' notation.
165
     *
166
     * @return void
167
     */
168
    public function replaceAsterisks($columnName)
169
    {
170
        foreach ($this->_args as $key => $arg) {
171
            $this->_args[$key] = str_replace('*', $columnName, $this->_args[$key]);
172
        }
173
    }
174
 
175
    /**
176
     * Returns a string representation of this class.
177
     *
178
     * @return string
179
     */
180
    public function toString()
181
    {
182
        return get_class($this) . '(' . $this->_callbackString . ')';
183
    }
184
}
185
?>