PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/vendor/swift/lib/classes/Swift/StreamFilters/StringReplacementFilter.php

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