1: <?php
2:
3: 4: 5:
6: class Quform_Validator_Digits extends Quform_Validator_Abstract
7: {
8: const INVALID = 'digitsInvalid';
9: const NOT_DIGITS = 'notDigits';
10:
11: 12: 13: 14: 15:
16: protected static $filter = null;
17:
18: 19: 20: 21: 22: 23: 24:
25: public function isValid($value)
26: {
27: $this->reset();
28:
29: if ( ! is_string($value)) {
30: $this->error(self::INVALID);
31: return false;
32: }
33:
34: if (self::$filter === null) {
35: self::$filter = new Quform_Filter_Digits;
36: }
37:
38: self::$filter->setConfig('allowWhiteSpace', $this->config('allowWhiteSpace'));
39:
40: if ($value !== self::$filter->filter($value)) {
41: $this->error(self::NOT_DIGITS, compact('value'));
42: return false;
43: }
44:
45: return true;
46: }
47:
48: 49: 50: 51: 52: 53:
54: public static function getMessageTemplates($key = null)
55: {
56: $messageTemplates = array(
57: self::INVALID => __('Invalid data type, string expected', 'quform'),
58: self::NOT_DIGITS => __('Only digits are allowed', 'quform')
59: );
60:
61: if (is_string($key)) {
62: return array_key_exists($key, $messageTemplates) ? $messageTemplates[$key] : null;
63: }
64:
65: return $messageTemplates;
66: }
67:
68: 69: 70: 71: 72: 73:
74: public static function getDefaultConfig($key = null)
75: {
76: $config = apply_filters('quform_default_config_validator_digits', array(
77: 'allowWhiteSpace' => false,
78: 'messages' => array(
79: self::INVALID => '',
80: self::NOT_DIGITS => ''
81: )
82: ));
83:
84: $config['type'] = 'digits';
85:
86: if (Quform::isNonEmptyString($key)) {
87: return Quform::get($config, $key);
88: }
89:
90: return $config;
91: }
92: }
93: