1: <?php
2: /**
3: * A Horde_Injector:: based Horde_Text_Filter_Base:: factory.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Core
9: * @author Michael Slusarz <slusarz@horde.org>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Core
12: */
13:
14: /**
15: * A Horde_Injector:: based Horde_Text_Filter_Base:: factory.
16: *
17: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Horde
23: * @package Core
24: * @author Michael Slusarz <slusarz@horde.org>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://pear.horde.org/index.php?package=Core
27: */
28: class Horde_Core_Factory_TextFilter extends Horde_Core_Factory_Base
29: {
30: /**
31: * Return the Horde_Text_Filter_Base:: instance.
32: *
33: * @param string $driver Either a driver name, or the full class name to
34: * use.
35: * @param array $params A hash containing any additional configuration
36: * parameters a subclass might need.
37: *
38: * @return Horde_Text_Filter_Base The singleton instance.
39: * @throws Horde_Text_Filter_Exception
40: */
41: public function create($driver, array $params = array())
42: {
43: list($driver, $params) = $this->_getDriver($driver, $params);
44: return Horde_Text_Filter::factory($driver, $params);
45: }
46:
47: /**
48: * Applies a set of patterns to a block of text.
49: *
50: * @param string $text The text to filter.
51: * @param mixed $filters The list of filters (or a single filter).
52: * @param mixed $params The list of params to use with each filter.
53: *
54: * @return string The transformed text.
55: */
56: 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: $filter_list = array();
64: $params = array_values($params);
65:
66: foreach (array_values($filters) as $num => $filter) {
67: list($driver, $driv_param) = $this->_getDriver($filter, isset($params[$num]) ? $params[$num] : array());
68: $filter_list[$driver] = $driv_param;
69: }
70:
71: return Horde_Text_Filter::filter($text, array_keys($filter_list), array_values($filter_list));
72: }
73:
74: /**
75: * Gets the driver/params for a given base Horde_Text_Filter driver.
76: *
77: * @param string $driver Either a driver name, or the full class name to
78: * use.
79: * @param array $params A hash containing any additional configuration
80: * parameters a subclass might need.
81: *
82: * @return array Driver as the first value, params list as the second.
83: */
84: protected function _getDriver($driver, $params)
85: {
86: $lc_driver = Horde_String::lower($driver);
87:
88: switch ($lc_driver) {
89: case 'bbcode':
90: $driver = 'Horde_Core_Text_Filter_Bbcode';
91: break;
92:
93: case 'emails':
94: $driver = 'Horde_Core_Text_Filter_Emails';
95: break;
96:
97: case 'emoticons':
98: $driver = 'Horde_Core_Text_Filter_Emoticons';
99: break;
100:
101: case 'highlightquotes':
102: $driver = 'Horde_Core_Text_Filter_Highlightquotes';
103: break;
104:
105: case 'linkurls':
106: if (!isset($params['callback'])) {
107: $params['callback'] = 'Horde::externalUrl';
108: }
109: break;
110:
111: case 'text2html':
112: $param_copy = $params;
113: foreach (array('emails', 'linkurls', 'space2html') as $val) {
114: if (!isset($params[$val])) {
115: $tmp = $this->_getDriver($val, $param_copy);
116: $params[$val] = array(
117: $tmp[0] => $tmp[1]
118: );
119: }
120: }
121: break;
122: }
123:
124: return array($driver, $params);
125: }
126:
127: }
128: