StringReplaceFilter.class.php

Go to the documentation of this file.
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 
00015     final class StringReplaceFilter implements Filtrator
00016     {
00017         private $search     = null;
00018         private $replace    = null;
00019         
00020         private $count      = null;
00021         
00025         public static function create($search = null, $replace = null)
00026         {
00027             return new self($search, $replace);
00028         }
00029         
00030         public function __construct($search = null, $replace = null)
00031         {
00032             $this->search = $search;
00033             $this->replace = $replace;
00034         }
00035         
00039         public function setSearch($search)
00040         {
00041             $this->search = $search;
00042             
00043             return $this;
00044         }
00045         
00046         public function getSearch()
00047         {
00048             return $this->search;
00049         }
00050         
00054         public function setReplace($replace)
00055         {
00056             $this->replace = $replace;
00057             
00058             return $this;
00059         }
00060         
00061         public function getReplace()
00062         {
00063             return $this->replace;
00064         }
00065         
00066         public function getCount()
00067         {
00068             return $this->count;
00069         }
00070         
00071         public function apply($value)
00072         {
00073             if ($this->search === $this->replace)
00074                 return $value;
00075             
00076             return
00077                 str_replace(
00078                     $this->search,
00079                     $this->replace,
00080                     $value,
00081                     $this->count
00082                 );
00083         }
00084     }
00085 ?>