1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Grafizzi\Graph\Filter\StringFilter: a component of the Grafizzi library. |
6
|
|
|
* |
7
|
|
|
* (c) 2012 Frédéric G. MARAND <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Grafizzi is free software: you can redistribute it and/or modify it under the |
10
|
|
|
* terms of the GNU Lesser General Public License as published by the Free |
11
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
12
|
|
|
* later version. |
13
|
|
|
* |
14
|
|
|
* Grafizzi is distributed in the hope that it will be useful, but WITHOUT ANY |
15
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
16
|
|
|
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
17
|
|
|
* details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
20
|
|
|
* along with Grafizzi, in the COPYING.LESSER.txt file. If not, see |
21
|
|
|
* <http://www.gnu.org/licenses/> |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace Grafizzi\Graph\Filter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A Grafizzi filter that also outputs its result to the passed-in string. |
28
|
|
|
* |
29
|
|
|
* Since it has to be passed by reference, the argument to use is like: |
30
|
|
|
* array('out' => &$some_string). |
31
|
|
|
*/ |
32
|
|
|
class StringFilter extends AbstractFilter implements FilterInterface { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The reference to the variable in which to copy the result. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $string = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* An optional callable to be invoked on the data. |
43
|
|
|
* |
44
|
|
|
* @var callable |
45
|
|
|
* It receives the input and returns its after filtering. |
46
|
|
|
*/ |
47
|
|
|
public $callback = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $args |
51
|
|
|
* $args[0] is the only used element in this array. |
52
|
|
|
* @throws \InvalidArgumentException |
53
|
|
|
*/ |
54
|
3 |
|
public function __construct(array $args = array()) { |
55
|
3 |
|
if (isset($args['out'])) { |
56
|
3 |
|
$this->string = &$args['out']; |
57
|
|
|
} |
58
|
3 |
|
if (isset($args['callback'])) { |
59
|
3 |
|
if (is_callable($args['callback'])) { |
60
|
3 |
|
$this->callback = $args['callback']; |
61
|
|
|
} |
62
|
|
|
else { |
63
|
|
|
throw new \InvalidArgumentException("Invalid callback passed to " . __CLASS__); |
64
|
|
|
} |
65
|
|
|
} |
66
|
3 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
3 |
|
public function filter($input) { |
72
|
|
|
/** @var string $stdout */ |
73
|
3 |
|
$stdout = isset($this->callback) |
74
|
3 |
|
? call_user_func($this->callback, $input) |
75
|
3 |
|
: $input; |
76
|
3 |
|
if (isset($this->string)) { |
77
|
3 |
|
$this->string = $stdout; |
78
|
|
|
} |
79
|
3 |
|
$ret = array($stdout, null); |
80
|
3 |
|
return $ret; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|