1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8/**
9 * Class TikiFilter_PregFilter
10 *
11 * Use to create Tiki filters based on the PHP preg_filter() method
12 */
13class TikiFilter_PregFilter implements Zend\Filter\FilterInterface
14{
15
16	/**
17	 * @var
18	 */
19	private $pattern;
20	private $replacement;
21	private $limit;
22	private $count;
23
24	/**
25	 * TikiFilter_PregFilter constructor.
26	 *
27	 * See PHP documentation for preg_filter() for parameter definitions
28	 * @param $pattern
29	 * @param $replacement
30	 * @param int $limit
31	 * @param null $count
32	 */
33	function __construct($pattern, $replacement, $limit = -1, $count = null)
34	{
35		$this->pattern = $pattern;
36		$this->replacement = $replacement;
37		$this->limit = $limit;
38		$this->count = $count;
39	}
40
41	/**
42	 * @param mixed $subject
43	 * @return mixed
44	 */
45	function filter($subject)
46	{
47		if (is_null($this->count)) {
48			$return = preg_filter($this->pattern, $this->replacement, $subject, $this->limit);
49		} else {
50			$return = preg_filter($this->pattern, $this->replacement, $subject, $this->limit, $this->count);
51		}
52		//return empty string rather than null
53		return ! empty($return) ? $subject : '';
54	}
55}
56