00001 <?php 00002 /*************************************************************************** 00003 * Copyright (C) 2006-2007 by Konstantin V. Arkhipov * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU Lesser General Public License as * 00007 * published by the Free Software Foundation; either version 3 of the * 00008 * License, or (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 /* $Id: StringReplaceFilter.class.php 4687 2007-12-09 18:57:18Z voxus $ */ 00012 00016 final class StringReplaceFilter implements Filtrator 00017 { 00018 private $search = null; 00019 private $replace = null; 00020 00021 private $count = null; 00022 00026 public static function create($search = null, $replace = null) 00027 { 00028 return new self($search, $replace); 00029 } 00030 00031 public function __construct($search = null, $replace = null) 00032 { 00033 $this->search = $search; 00034 $this->replace = $replace; 00035 } 00036 00040 public function setSearch($search) 00041 { 00042 $this->search = $search; 00043 00044 return $this; 00045 } 00046 00047 public function getSearch() 00048 { 00049 return $this->search; 00050 } 00051 00055 public function setReplace($replace) 00056 { 00057 $this->replace = $replace; 00058 00059 return $this; 00060 } 00061 00062 public function getReplace() 00063 { 00064 return $this->replace; 00065 } 00066 00067 public function getCount() 00068 { 00069 return $this->count; 00070 } 00071 00072 public function apply($value) 00073 { 00074 if ($this->search === $this->replace) 00075 return $value; 00076 00077 return 00078 str_replace( 00079 $this->search, 00080 $this->replace, 00081 $value, 00082 $this->count 00083 ); 00084 } 00085 } 00086 ?>