|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Captcha |
|
17 * @subpackage Adapter |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 */ |
|
21 |
|
22 /** @see Zend_Captcha_Adapter */ |
|
23 require_once 'Zend/Captcha/Adapter.php'; |
|
24 |
|
25 /** @see Zend_Validate_Abstract */ |
|
26 require_once 'Zend/Validate/Abstract.php'; |
|
27 |
|
28 /** |
|
29 * Base class for Captcha adapters |
|
30 * |
|
31 * Provides some utility functionality to build on |
|
32 * |
|
33 * @category Zend |
|
34 * @package Zend_Captcha |
|
35 * @subpackage Adapter |
|
36 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
37 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
38 * @version $Id: Base.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
39 */ |
|
40 abstract class Zend_Captcha_Base extends Zend_Validate_Abstract implements Zend_Captcha_Adapter |
|
41 { |
|
42 /** |
|
43 * Element name |
|
44 * |
|
45 * Useful to generate/check form fields |
|
46 * |
|
47 * @var string |
|
48 */ |
|
49 protected $_name; |
|
50 |
|
51 /** |
|
52 * Captcha options |
|
53 * |
|
54 * @var array |
|
55 */ |
|
56 protected $_options = array(); |
|
57 |
|
58 /** |
|
59 * Options to skip when processing options |
|
60 * @var array |
|
61 */ |
|
62 protected $_skipOptions = array( |
|
63 'options', |
|
64 'config', |
|
65 ); |
|
66 |
|
67 /** |
|
68 * Get name |
|
69 * |
|
70 * @return string |
|
71 */ |
|
72 public function getName() |
|
73 { |
|
74 return $this->_name; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Set name |
|
79 * |
|
80 * @param string $name |
|
81 */ |
|
82 public function setName($name) |
|
83 { |
|
84 $this->_name = $name; |
|
85 return $this; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Constructor |
|
90 * |
|
91 * @param array|Zend_Config $options |
|
92 * @return void |
|
93 */ |
|
94 public function __construct($options = null) |
|
95 { |
|
96 // Set options |
|
97 if (is_array($options)) { |
|
98 $this->setOptions($options); |
|
99 } else if ($options instanceof Zend_Config) { |
|
100 $this->setConfig($options); |
|
101 } |
|
102 } |
|
103 |
|
104 /** |
|
105 * Set single option for the object |
|
106 * |
|
107 * @param string $key |
|
108 * @param string $value |
|
109 * @return Zend_Form_Element |
|
110 */ |
|
111 public function setOption($key, $value) |
|
112 { |
|
113 if (in_array(strtolower($key), $this->_skipOptions)) { |
|
114 return $this; |
|
115 } |
|
116 |
|
117 $method = 'set' . ucfirst ($key); |
|
118 if (method_exists ($this, $method)) { |
|
119 // Setter exists; use it |
|
120 $this->$method ($value); |
|
121 $this->_options[$key] = $value; |
|
122 } elseif (property_exists($this, $key)) { |
|
123 // Assume it's metadata |
|
124 $this->$key = $value; |
|
125 $this->_options[$key] = $value; |
|
126 } |
|
127 return $this; |
|
128 } |
|
129 |
|
130 /** |
|
131 * Set object state from options array |
|
132 * |
|
133 * @param array $options |
|
134 * @return Zend_Form_Element |
|
135 */ |
|
136 public function setOptions($options = null) |
|
137 { |
|
138 foreach ($options as $key => $value) { |
|
139 $this->setOption($key, $value); |
|
140 } |
|
141 return $this; |
|
142 } |
|
143 |
|
144 /** |
|
145 * Retrieve options representing object state |
|
146 * |
|
147 * @return array |
|
148 */ |
|
149 public function getOptions() |
|
150 { |
|
151 return $this->_options; |
|
152 } |
|
153 |
|
154 /** |
|
155 * Set object state from config object |
|
156 * |
|
157 * @param Zend_Config $config |
|
158 * @return Zend_Captcha_Base |
|
159 */ |
|
160 public function setConfig(Zend_Config $config) |
|
161 { |
|
162 return $this->setOptions($config->toArray()); |
|
163 } |
|
164 |
|
165 /** |
|
166 * Get optional decorator |
|
167 * |
|
168 * By default, return null, indicating no extra decorator needed. |
|
169 * |
|
170 * @return null |
|
171 */ |
|
172 public function getDecorator() |
|
173 { |
|
174 return null; |
|
175 } |
|
176 } |