1: <?php
2:
3: 4: 5:
6: class Quform_Validator_InArray extends Quform_Validator_Abstract
7: {
8: const INVALID = 'inArrayInvalid';
9: const NOT_IN_ARRAY = 'notInArray';
10:
11: 12: 13: 14: 15: 16: 17:
18: public function isValid($value)
19: {
20: $this->reset();
21:
22: if ( ! is_string($value)) {
23: $this->error(self::INVALID);
24: return false;
25: }
26:
27: $inArray = in_array($value, $this->config('haystack'), true);
28:
29: if (( ! $inArray && ! $this->config('invert')) || ($inArray && $this->config('invert'))) {
30: $this->error(self::NOT_IN_ARRAY, compact('value'));
31: return false;
32: }
33:
34: return true;
35: }
36:
37: 38: 39: 40: 41: 42:
43: public static function getMessageTemplates($key = null)
44: {
45: $messageTemplates = array(
46: self::INVALID => __('Invalid data type, string expected', 'quform'),
47: self::NOT_IN_ARRAY => __('This value is not valid', 'quform')
48: );
49:
50: if (is_string($key)) {
51: return array_key_exists($key, $messageTemplates) ? $messageTemplates[$key] : null;
52: }
53:
54: return $messageTemplates;
55: }
56:
57: 58: 59: 60: 61: 62:
63: public static function getDefaultConfig($key = null)
64: {
65: $config = apply_filters('quform_default_config_validator_in_array', array(
66: 'haystack' => array(),
67: 'invert' => false,
68: 'messages' => array(
69: self::INVALID => '',
70: self::NOT_IN_ARRAY => ''
71: )
72: ));
73:
74: $config['type'] = 'inArray';
75:
76: if (Quform::isNonEmptyString($key)) {
77: return Quform::get($config, $key);
78: }
79:
80: return $config;
81: }
82: }
83: