source: trunk/www.guidonia.net/wp/wp-content/plugins/webtv/Drivers/Zend/Validate/Abstract.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 10.9 KB
Line 
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_Validate
17 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id: Abstract.php 14317 2009-03-13 20:39:31Z thomas $
20 */
21
22/**
23 * @see Zend_Validate_Interface
24 */
25require_once 'Zend/Validate/Interface.php';
26
27/**
28 * @category Zend
29 * @package Zend_Validate
30 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license http://framework.zend.com/license/new-bsd New BSD License
32 */
33abstract class Zend_Validate_Abstract implements Zend_Validate_Interface
34{
35 /**
36 * The value to be validated
37 *
38 * @var mixed
39 */
40 protected $_value;
41
42 /**
43 * Additional variables available for validation failure messages
44 *
45 * @var array
46 */
47 protected $_messageVariables = array();
48
49 /**
50 * Validation failure message template definitions
51 *
52 * @var array
53 */
54 protected $_messageTemplates = array();
55
56 /**
57 * Array of validation failure messages
58 *
59 * @var array
60 */
61 protected $_messages = array();
62
63 /**
64 * Flag indidcating whether or not value should be obfuscated in error
65 * messages
66 * @var bool
67 */
68 protected $_obscureValue = false;
69
70 /**
71 * Array of validation failure message codes
72 *
73 * @var array
74 * @deprecated Since 1.5.0
75 */
76 protected $_errors = array();
77
78 /**
79 * Translation object
80 * @var Zend_Translate
81 */
82 protected $_translator;
83
84 /**
85 * Default translation object for all validate objects
86 * @var Zend_Translate
87 */
88 protected static $_defaultTranslator;
89
90 /**
91 * Is translation disabled?
92 * @var Boolean
93 */
94 protected $_translatorDisabled = false;
95
96 /**
97 * Returns array of validation failure messages
98 *
99 * @return array
100 */
101 public function getMessages()
102 {
103 return $this->_messages;
104 }
105
106 /**
107 * Returns an array of the names of variables that are used in constructing validation failure messages
108 *
109 * @return array
110 */
111 public function getMessageVariables()
112 {
113 return array_keys($this->_messageVariables);
114 }
115
116 /**
117 * Returns the message templates from the validator
118 *
119 * @return array
120 */
121 public function getMessageTemplates()
122 {
123 return $this->_messageTemplates;
124 }
125
126 /**
127 * Sets the validation failure message template for a particular key
128 *
129 * @param string $messageString
130 * @param string $messageKey OPTIONAL
131 * @return Zend_Validate_Abstract Provides a fluent interface
132 * @throws Zend_Validate_Exception
133 */
134 public function setMessage($messageString, $messageKey = null)
135 {
136 if ($messageKey === null) {
137 $keys = array_keys($this->_messageTemplates);
138 $messageKey = current($keys);
139 }
140 if (!isset($this->_messageTemplates[$messageKey])) {
141 require_once 'Zend/Validate/Exception.php';
142 throw new Zend_Validate_Exception("No message template exists for key '$messageKey'");
143 }
144 $this->_messageTemplates[$messageKey] = $messageString;
145 return $this;
146 }
147
148 /**
149 * Sets validation failure message templates given as an array, where the array keys are the message keys,
150 * and the array values are the message template strings.
151 *
152 * @param array $messages
153 * @return Zend_Validate_Abstract
154 */
155 public function setMessages(array $messages)
156 {
157 foreach ($messages as $key => $message) {
158 $this->setMessage($message, $key);
159 }
160 return $this;
161 }
162
163 /**
164 * Magic function returns the value of the requested property, if and only if it is the value or a
165 * message variable.
166 *
167 * @param string $property
168 * @return mixed
169 * @throws Zend_Validate_Exception
170 */
171 public function __get($property)
172 {
173 if ($property == 'value') {
174 return $this->_value;
175 }
176 if (array_key_exists($property, $this->_messageVariables)) {
177 return $this->{$this->_messageVariables[$property]};
178 }
179 /**
180 * @see Zend_Validate_Exception
181 */
182 require_once 'Zend/Validate/Exception.php';
183 throw new Zend_Validate_Exception("No property exists by the name '$property'");
184 }
185
186 /**
187 * Constructs and returns a validation failure message with the given message key and value.
188 *
189 * Returns null if and only if $messageKey does not correspond to an existing template.
190 *
191 * If a translator is available and a translation exists for $messageKey,
192 * the translation will be used.
193 *
194 * @param string $messageKey
195 * @param string $value
196 * @return string
197 */
198 protected function _createMessage($messageKey, $value)
199 {
200 if (!isset($this->_messageTemplates[$messageKey])) {
201 return null;
202 }
203
204 $message = $this->_messageTemplates[$messageKey];
205
206 if (null !== ($translator = $this->getTranslator())) {
207 if ($translator->isTranslated($message)) {
208 $message = $translator->translate($message);
209 } elseif ($translator->isTranslated($messageKey)) {
210 $message = $translator->translate($messageKey);
211 }
212 }
213
214 if (is_object($value)) {
215 if (!in_array('__toString', get_class_methods($value))) {
216 $value = get_class($value) . ' object';
217 } else {
218 $value = $value->__toString();
219 }
220 } else {
221 $value = (string)$value;
222 }
223
224 if ($this->getObscureValue()) {
225 $value = str_repeat('*', strlen($value));
226 }
227
228 $message = str_replace('%value%', (string) $value, $message);
229 foreach ($this->_messageVariables as $ident => $property) {
230 $message = str_replace("%$ident%", (string) $this->$property, $message);
231 }
232 return $message;
233 }
234
235 /**
236 * @param string $messageKey OPTIONAL
237 * @param string $value OPTIONAL
238 * @return void
239 */
240 protected function _error($messageKey = null, $value = null)
241 {
242 if ($messageKey === null) {
243 $keys = array_keys($this->_messageTemplates);
244 $messageKey = current($keys);
245 }
246 if ($value === null) {
247 $value = $this->_value;
248 }
249 $this->_errors[] = $messageKey;
250 $this->_messages[$messageKey] = $this->_createMessage($messageKey, $value);
251 }
252
253 /**
254 * Sets the value to be validated and clears the messages and errors arrays
255 *
256 * @param mixed $value
257 * @return void
258 */
259 protected function _setValue($value)
260 {
261 $this->_value = $value;
262 $this->_messages = array();
263 $this->_errors = array();
264 }
265
266 /**
267 * Returns array of validation failure message codes
268 *
269 * @return array
270 * @deprecated Since 1.5.0
271 */
272 public function getErrors()
273 {
274 return $this->_errors;
275 }
276
277 /**
278 * Set flag indicating whether or not value should be obfuscated in messages
279 *
280 * @param bool $flag
281 * @return Zend_Validate_Abstract
282 */
283 public function setObscureValue($flag)
284 {
285 $this->_obscureValue = (bool) $flag;
286 return $this;
287 }
288
289 /**
290 * Retrieve flag indicating whether or not value should be obfuscated in
291 * messages
292 *
293 * @return bool
294 */
295 public function getObscureValue()
296 {
297 return $this->_obscureValue;
298 }
299
300 /**
301 * Set translation object
302 *
303 * @param Zend_Translate|Zend_Translate_Adapter|null $translator
304 * @return Zend_Validate_Abstract
305 */
306 public function setTranslator($translator = null)
307 {
308 if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
309 $this->_translator = $translator;
310 } elseif ($translator instanceof Zend_Translate) {
311 $this->_translator = $translator->getAdapter();
312 } else {
313 require_once 'Zend/Validate/Exception.php';
314 throw new Zend_Validate_Exception('Invalid translator specified');
315 }
316 return $this;
317 }
318
319 /**
320 * Return translation object
321 *
322 * @return Zend_Translate_Adapter|null
323 */
324 public function getTranslator()
325 {
326 if ($this->translatorIsDisabled()) {
327 return null;
328 }
329
330 if (null === $this->_translator) {
331 return self::getDefaultTranslator();
332 }
333
334 return $this->_translator;
335 }
336
337 /**
338 * Set default translation object for all validate objects
339 *
340 * @param Zend_Translate|Zend_Translate_Adapter|null $translator
341 * @return void
342 */
343 public static function setDefaultTranslator($translator = null)
344 {
345 if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
346 self::$_defaultTranslator = $translator;
347 } elseif ($translator instanceof Zend_Translate) {
348 self::$_defaultTranslator = $translator->getAdapter();
349 } else {
350 require_once 'Zend/Validate/Exception.php';
351 throw new Zend_Validate_Exception('Invalid translator specified');
352 }
353 }
354
355 /**
356 * Get default translation object for all validate objects
357 *
358 * @return Zend_Translate_Adapter|null
359 */
360 public static function getDefaultTranslator()
361 {
362 if (null === self::$_defaultTranslator) {
363 require_once 'Zend/Registry.php';
364 if (Zend_Registry::isRegistered('Zend_Translate')) {
365 $translator = Zend_Registry::get('Zend_Translate');
366 if ($translator instanceof Zend_Translate_Adapter) {
367 return $translator;
368 } elseif ($translator instanceof Zend_Translate) {
369 return $translator->getAdapter();
370 }
371 }
372 }
373
374 return self::$_defaultTranslator;
375 }
376
377 /**
378 * Indicate whether or not translation should be disabled
379 *
380 * @param bool $flag
381 * @return Zend_Validate_Abstract
382 */
383 public function setDisableTranslator($flag)
384 {
385 $this->_translatorDisabled = (bool) $flag;
386 return $this;
387 }
388
389 /**
390 * Is translation disabled?
391 *
392 * @return bool
393 */
394 public function translatorIsDisabled()
395 {
396 return $this->_translatorDisabled;
397 }
398}
Note: See TracBrowser for help on using the repository browser.