1: <?php
2:
3: /*
4: * This file is part of SwiftMailer.
5: * (c) 2004-2009 Chris Corbyn
6: *
7: * For the full copyright and license information, please view the LICENSE
8: * file that was distributed with this source code.
9: */
10:
11: /**
12: * Processes bytes as they pass through a buffer and replaces sequences in it.
13: * @package Swift
14: * @author Chris Corbyn
15: */
16: class Swift_StreamFilters_StringReplacementFilter implements Swift_StreamFilter
17: {
18: /** The needle(s) to search for */
19: private $_search;
20:
21: /** The replacement(s) to make */
22: private $_replace;
23:
24: /**
25: * Create a new StringReplacementFilter with $search and $replace.
26: * @param string|array $search
27: * @param string|array $replace
28: */
29: public function __construct($search, $replace)
30: {
31: $this->_search = $search;
32: $this->_replace = $replace;
33: }
34:
35: /**
36: * Returns true if based on the buffer passed more bytes should be buffered.
37: * @param string $buffer
38: * @return boolean
39: */
40: public function shouldBuffer($buffer)
41: {
42: $endOfBuffer = substr($buffer, -1);
43: foreach ((array) $this->_search as $needle) {
44: if (false !== strpos($needle, $endOfBuffer)) {
45: return true;
46: }
47: }
48:
49: return false;
50: }
51:
52: /**
53: * Perform the actual replacements on $buffer and return the result.
54: * @param string $buffer
55: * @return string
56: */
57: public function filter($buffer)
58: {
59: return str_replace($this->_search, $this->_replace, $buffer);
60: }
61: }
62: