/framework/vendor/swift/lib/classes/Swift/StreamFilters/StringReplacementFilter.php
PHP | 66 lines | 27 code | 9 blank | 30 comment | 1 complexity | c7994884f113f5a2926c9f40b9927ea6 MD5 | raw file
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//@require 'Swift/StreamFilter.php'; 12 13/** 14 * Processes bytes as they pass through a buffer and replaces sequences in it. 15 * @package Swift 16 * @author Chris Corbyn 17 */ 18class Swift_StreamFilters_StringReplacementFilter implements Swift_StreamFilter 19{ 20 21 /** The needle(s) to search for */ 22 private $_search; 23 24 /** The replacement(s) to make */ 25 private $_replace; 26 27 /** 28 * Create a new StringReplacementFilter with $search and $replace. 29 * @param string|array $search 30 * @param string|array $replace 31 */ 32 public function __construct($search, $replace) 33 { 34 $this->_search = $search; 35 $this->_replace = $replace; 36 } 37 38 /** 39 * Returns true if based on the buffer passed more bytes should be buffered. 40 * @param string $buffer 41 * @return boolean 42 */ 43 public function shouldBuffer($buffer) 44 { 45 $endOfBuffer = substr($buffer, -1); 46 foreach ((array) $this->_search as $needle) 47 { 48 if (false !== strpos($needle, $endOfBuffer)) 49 { 50 return true; 51 } 52 } 53 return false; 54 } 55 56 /** 57 * Perform the actual replacements on $buffer and return the result. 58 * @param string $buffer 59 * @return string 60 */ 61 public function filter($buffer) 62 { 63 return str_replace($this->_search, $this->_replace, $buffer); 64 } 65 66}