1: <?php
  2: 
  3:   4:   5: 
  6: class Quform_Validator_Array extends Quform_Validator_Abstract
  7: {
  8:     const INVALID = 'arrayInvalid';
  9: 
 10:      11:  12: 
 13:     protected $validator;
 14: 
 15:      16:  17: 
 18:     public function __construct(array $options = array())
 19:     {
 20:         $this->setValidator($options['validator']);
 21:         unset($options['validator']);
 22: 
 23:         parent::__construct($options);
 24:     }
 25: 
 26:      27:  28: 
 29:     public function setValidator(Quform_Validator_Abstract $validator)
 30:     {
 31:         $this->validator = $validator;
 32:     }
 33: 
 34:      35:  36: 
 37:     public function getValidator()
 38:     {
 39:         return $this->validator;
 40:     }
 41: 
 42:      43:  44:  45:  46:  47: 
 48:     public function isValid($values)
 49:     {
 50:         $this->reset();
 51: 
 52:         if ( ! is_array($values)) {
 53:             $this->error(self::INVALID);
 54:             return false;
 55:         }
 56: 
 57:         foreach ($values as $value) {
 58:             if ( ! $this->validator->isValid($value)) {
 59:                 $this->setMessage($this->validator->getMessage());
 60:                 return false;
 61:             }
 62:         }
 63: 
 64:         return true;
 65:     }
 66: 
 67:      68:  69:  70:  71:  72: 
 73:     public static function getMessageTemplates($key = null)
 74:     {
 75:         $messageTemplates = array(
 76:             self::INVALID => __('Invalid data type, array expected',  'quform')
 77:         );
 78: 
 79:         if (is_string($key)) {
 80:             return array_key_exists($key, $messageTemplates) ? $messageTemplates[$key] : null;
 81:         }
 82: 
 83:         return $messageTemplates;
 84:     }
 85: 
 86:      87:  88:  89:  90:  91: 
 92:     public static function getDefaultConfig($key = null)
 93:     {
 94:         $config = apply_filters('quform_default_config_validator_array', array(
 95:             'messages' => array(
 96:                 self::INVALID => ''
 97:             )
 98:         ));
 99: 
100:         $config['type'] = 'array';
101: 
102:         if (Quform::isNonEmptyString($key)) {
103:             return Quform::get($config, $key);
104:         }
105: 
106:         return $config;
107:     }
108: }
109: