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