]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - Zend/Validate/Abstract.php
Release 6.5.0
[Github/sugarcrm.git] / Zend / Validate / Abstract.php
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-2010 Zend Technologies USA Inc. (http://www.zend.com)
18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
19
20  */
21
22 /**
23  * @see Zend_Validate_Interface
24  */
25 require_once 'Zend/Validate/Interface.php';
26
27 /**
28  * @category   Zend
29  * @package    Zend_Validate
30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
32  */
33 abstract 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      * Limits the maximum returned length of a error message
98      *
99      * @var Integer
100      */
101     protected static $_messageLength = -1;
102
103     /**
104      * Returns array of validation failure messages
105      *
106      * @return array
107      */
108     public function getMessages()
109     {
110         return $this->_messages;
111     }
112
113     /**
114      * Returns an array of the names of variables that are used in constructing validation failure messages
115      *
116      * @return array
117      */
118     public function getMessageVariables()
119     {
120         return array_keys($this->_messageVariables);
121     }
122
123     /**
124      * Returns the message templates from the validator
125      *
126      * @return array
127      */
128     public function getMessageTemplates()
129     {
130         return $this->_messageTemplates;
131     }
132
133     /**
134      * Sets the validation failure message template for a particular key
135      *
136      * @param  string $messageString
137      * @param  string $messageKey     OPTIONAL
138      * @return Zend_Validate_Abstract Provides a fluent interface
139      * @throws Zend_Validate_Exception
140      */
141     public function setMessage($messageString, $messageKey = null)
142     {
143         if ($messageKey === null) {
144             $keys = array_keys($this->_messageTemplates);
145             foreach($keys as $key) {
146                 $this->setMessage($messageString, $key);
147             }
148             return $this;
149         }
150
151         if (!isset($this->_messageTemplates[$messageKey])) {
152             require_once 'Zend/Validate/Exception.php';
153             throw new Zend_Validate_Exception("No message template exists for key '$messageKey'");
154         }
155
156         $this->_messageTemplates[$messageKey] = $messageString;
157         return $this;
158     }
159
160     /**
161      * Sets validation failure message templates given as an array, where the array keys are the message keys,
162      * and the array values are the message template strings.
163      *
164      * @param  array $messages
165      * @return Zend_Validate_Abstract
166      */
167     public function setMessages(array $messages)
168     {
169         foreach ($messages as $key => $message) {
170             $this->setMessage($message, $key);
171         }
172         return $this;
173     }
174
175     /**
176      * Magic function returns the value of the requested property, if and only if it is the value or a
177      * message variable.
178      *
179      * @param  string $property
180      * @return mixed
181      * @throws Zend_Validate_Exception
182      */
183     public function __get($property)
184     {
185         if ($property == 'value') {
186             return $this->_value;
187         }
188         if (array_key_exists($property, $this->_messageVariables)) {
189             return $this->{$this->_messageVariables[$property]};
190         }
191         /**
192          * @see Zend_Validate_Exception
193          */
194         require_once 'Zend/Validate/Exception.php';
195         throw new Zend_Validate_Exception("No property exists by the name '$property'");
196     }
197
198     /**
199      * Constructs and returns a validation failure message with the given message key and value.
200      *
201      * Returns null if and only if $messageKey does not correspond to an existing template.
202      *
203      * If a translator is available and a translation exists for $messageKey,
204      * the translation will be used.
205      *
206      * @param  string $messageKey
207      * @param  string $value
208      * @return string
209      */
210     protected function _createMessage($messageKey, $value)
211     {
212         if (!isset($this->_messageTemplates[$messageKey])) {
213             return null;
214         }
215
216         $message = $this->_messageTemplates[$messageKey];
217
218         if (null !== ($translator = $this->getTranslator())) {
219             if ($translator->isTranslated($messageKey)) {
220                 $message = $translator->translate($messageKey);
221             } else {
222                 $message = $translator->translate($message);
223             }
224         }
225
226         if (is_object($value)) {
227             if (!in_array('__toString', get_class_methods($value))) {
228                 $value = get_class($value) . ' object';
229             } else {
230                 $value = $value->__toString();
231             }
232         } else {
233             $value = (string)$value;
234         }
235
236         if ($this->getObscureValue()) {
237             $value = str_repeat('*', strlen($value));
238         }
239
240         $message = str_replace('%value%', (string) $value, $message);
241         foreach ($this->_messageVariables as $ident => $property) {
242             $message = str_replace("%$ident%", (string) $this->$property, $message);
243         }
244
245         $length = self::getMessageLength();
246         if (($length > -1) && (strlen($message) > $length)) {
247             $message = substr($message, 0, (self::getMessageLength() - 3)) . '...';
248         }
249
250         return $message;
251     }
252
253     /**
254      * @param  string $messageKey
255      * @param  string $value      OPTIONAL
256      * @return void
257      */
258     protected function _error($messageKey, $value = null)
259     {
260         if ($messageKey === null) {
261             $keys = array_keys($this->_messageTemplates);
262             $messageKey = current($keys);
263         }
264         if ($value === null) {
265             $value = $this->_value;
266         }
267         $this->_errors[]              = $messageKey;
268         $this->_messages[$messageKey] = $this->_createMessage($messageKey, $value);
269     }
270
271     /**
272      * Sets the value to be validated and clears the messages and errors arrays
273      *
274      * @param  mixed $value
275      * @return void
276      */
277     protected function _setValue($value)
278     {
279         $this->_value    = $value;
280         $this->_messages = array();
281         $this->_errors   = array();
282     }
283
284     /**
285      * Returns array of validation failure message codes
286      *
287      * @return array
288      * @deprecated Since 1.5.0
289      */
290     public function getErrors()
291     {
292         return $this->_errors;
293     }
294
295     /**
296      * Set flag indicating whether or not value should be obfuscated in messages
297      *
298      * @param  bool $flag
299      * @return Zend_Validate_Abstract
300      */
301     public function setObscureValue($flag)
302     {
303         $this->_obscureValue = (bool) $flag;
304         return $this;
305     }
306
307     /**
308      * Retrieve flag indicating whether or not value should be obfuscated in
309      * messages
310      *
311      * @return bool
312      */
313     public function getObscureValue()
314     {
315         return $this->_obscureValue;
316     }
317
318     /**
319      * Set translation object
320      *
321      * @param  Zend_Translate|Zend_Translate_Adapter|null $translator
322      * @return Zend_Validate_Abstract
323      */
324     public function setTranslator($translator = null)
325     {
326         if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
327             $this->_translator = $translator;
328         } elseif ($translator instanceof Zend_Translate) {
329             $this->_translator = $translator->getAdapter();
330         } else {
331             require_once 'Zend/Validate/Exception.php';
332             throw new Zend_Validate_Exception('Invalid translator specified');
333         }
334         return $this;
335     }
336
337     /**
338      * Return translation object
339      *
340      * @return Zend_Translate_Adapter|null
341      */
342     public function getTranslator()
343     {
344         if ($this->translatorIsDisabled()) {
345             return null;
346         }
347
348         if (null === $this->_translator) {
349             return self::getDefaultTranslator();
350         }
351
352         return $this->_translator;
353     }
354
355     /**
356      * Does this validator have its own specific translator?
357      *
358      * @return bool
359      */
360     public function hasTranslator()
361     {
362         return (bool)$this->_translator;
363     }
364
365     /**
366      * Set default translation object for all validate objects
367      *
368      * @param  Zend_Translate|Zend_Translate_Adapter|null $translator
369      * @return void
370      */
371     public static function setDefaultTranslator($translator = null)
372     {
373         if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
374             self::$_defaultTranslator = $translator;
375         } elseif ($translator instanceof Zend_Translate) {
376             self::$_defaultTranslator = $translator->getAdapter();
377         } else {
378             require_once 'Zend/Validate/Exception.php';
379             throw new Zend_Validate_Exception('Invalid translator specified');
380         }
381     }
382
383     /**
384      * Get default translation object for all validate objects
385      *
386      * @return Zend_Translate_Adapter|null
387      */
388     public static function getDefaultTranslator()
389     {
390         if (null === self::$_defaultTranslator) {
391             require_once 'Zend/Registry.php';
392             if (Zend_Registry::isRegistered('Zend_Translate')) {
393                 $translator = Zend_Registry::get('Zend_Translate');
394                 if ($translator instanceof Zend_Translate_Adapter) {
395                     return $translator;
396                 } elseif ($translator instanceof Zend_Translate) {
397                     return $translator->getAdapter();
398                 }
399             }
400         }
401
402         return self::$_defaultTranslator;
403     }
404
405     /**
406      * Is there a default translation object set?
407      *
408      * @return boolean
409      */
410     public static function hasDefaultTranslator()
411     {
412         return (bool)self::$_defaultTranslator;
413     }
414
415     /**
416      * Indicate whether or not translation should be disabled
417      *
418      * @param  bool $flag
419      * @return Zend_Validate_Abstract
420      */
421     public function setDisableTranslator($flag)
422     {
423         $this->_translatorDisabled = (bool) $flag;
424         return $this;
425     }
426
427     /**
428      * Is translation disabled?
429      *
430      * @return bool
431      */
432     public function translatorIsDisabled()
433     {
434         return $this->_translatorDisabled;
435     }
436
437     /**
438      * Returns the maximum allowed message length
439      *
440      * @return integer
441      */
442     public static function getMessageLength()
443     {
444         return self::$_messageLength;
445     }
446
447     /**
448      * Sets the maximum allowed message length
449      *
450      * @param integer $length
451      */
452     public static function setMessageLength($length = -1)
453     {
454         self::$_messageLength = $length;
455     }
456 }