1: <?php
2: /**
3: * Horde_Text_Filter:: is a parent class for defining stackable text filters.
4: *
5: * Copyright 1999-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Chuck Hagenbuch <chuck@horde.org>
11: * @author Jan Schneider <jan@horde.org>
12: * @category Horde
13: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
14: * @package Text_Filter
15: */
16: class Horde_Text_Filter
17: {
18: /**
19: * Attempts to return a concrete instance based on $driver.
20: *
21: * @param string $driver Either a driver name, or the full class name to
22: * use (class must extend Horde_Text_Filter_Base).
23: * @param array $params A hash containing any additional configuration
24: * parameters a subclass might need.
25: *
26: * @return Horde_Text_Filter_Base The newly created concrete instance.
27: * @throws Horde_Text_Filter_Exception
28: */
29: static public function factory($driver, $params = array())
30: {
31: /* Base drivers (in Filter/ directory). */
32: $class = __CLASS__ . '_' . ucfirst(basename($driver));
33: if (class_exists($class)) {
34: return new $class($params);
35: }
36:
37: /* Explicit class name, */
38: $class = $driver;
39: if (class_exists($class)) {
40: return new $class($params);
41: }
42:
43: throw new Horde_Text_Filter_Exception(__CLASS__ . ': Class definition of ' . $driver . ' not found.');
44: }
45:
46: /**
47: * Applies a set of patterns to a block of text.
48: *
49: * @param string $text The text to filter.
50: * @param mixed $filters The list of filters (or a single filter).
51: * @param mixed $params The list of params to use with each filter.
52: *
53: * @return string The transformed text.
54: * @throws Horde_Text_Filter_Exception
55: */
56: static public function filter($text, $filters = array(), $params = array())
57: {
58: if (!is_array($filters)) {
59: $filters = array($filters);
60: $params = array($params);
61: }
62:
63: $params = array_values($params);
64:
65: foreach (array_values($filters) as $num => $filter) {
66: $filterOb = self::factory($filter, isset($params[$num]) ? $params[$num] : array());
67: $patterns = $filterOb->getPatterns();
68:
69: /* Pre-processing. */
70: $text = $filterOb->preProcess($text);
71:
72: /* str_replace() simple patterns. */
73: if (isset($patterns['replace'])) {
74: $text = str_replace(array_keys($patterns['replace']), array_values($patterns['replace']), $text);
75: }
76:
77: /* preg_replace complex patterns. */
78: if (isset($patterns['regexp'])) {
79: $text = preg_replace(array_keys($patterns['regexp']), array_values($patterns['regexp']), $text);
80: }
81:
82: /* preg_replace_callback complex patterns. */
83: if (isset($patterns['regexp_callback'])) {
84: foreach ($patterns['regexp_callback'] as $key => $val) {
85: $text = preg_replace_callback($key, $val, $text);
86: }
87: }
88:
89: /* Post-processing. */
90: $text = $filterOb->postProcess($text);
91: }
92:
93: return $text;
94: }
95:
96: }
97: