Esempio n. 1
0
 /**
  * Adds a new default namespace
  *
  * @param array|string $namespace
  * @return null
  */
 public static function addDefaultNamespaces($namespace)
 {
     if (!is_array($namespace)) {
         $namespace = array((string) $namespace);
     }
     self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace));
 }
Esempio n. 2
0
 /**
  * @param array $validatorRule
  * @return void
  */
 protected function _validateRule(array $validatorRule)
 {
     /**
      * Get one or more data values from input, and check for missing fields.
      * Apply defaults if fields are missing.
      */
     $data = array();
     foreach ((array) $validatorRule[self::FIELDS] as $key => $field) {
         if (array_key_exists($field, $this->_data)) {
             $data[$field] = $this->_data[$field];
         } else {
             if (isset($validatorRule[self::DEFAULT_VALUE])) {
                 /** @todo according to this code default value can't be an array. It has to be reviewed */
                 if (!is_array($validatorRule[self::DEFAULT_VALUE])) {
                     // Default value is a scalar
                     $data[$field] = $validatorRule[self::DEFAULT_VALUE];
                 } else {
                     // Default value is an array. Search for corresponding key
                     if (isset($validatorRule[self::DEFAULT_VALUE][$key])) {
                         $data[$field] = $validatorRule[self::DEFAULT_VALUE][$key];
                     } else {
                         if ($validatorRule[self::PRESENCE] == self::PRESENCE_REQUIRED) {
                             // Default value array is provided, but it doesn't have an entry for current field
                             // and presence is required
                             $this->_missingFields[$validatorRule[self::RULE]][] = $this->_getMissingMessage($validatorRule[self::RULE], $field);
                         }
                     }
                 }
             } else {
                 if ($validatorRule[self::PRESENCE] == self::PRESENCE_REQUIRED) {
                     $this->_missingFields[$validatorRule[self::RULE]][] = $this->_getMissingMessage($validatorRule[self::RULE], $field);
                 }
             }
         }
     }
     /**
      * If any required fields are missing, break the loop.
      */
     if (isset($this->_missingFields[$validatorRule[self::RULE]]) && count($this->_missingFields[$validatorRule[self::RULE]]) > 0) {
         return;
     }
     /**
      * Evaluate the inputs against the validator chain.
      */
     if (count((array) $validatorRule[self::FIELDS]) > 1) {
         if (!$validatorRule[self::ALLOW_EMPTY]) {
             $emptyFieldsFound = false;
             $errorsList = array();
             $messages = array();
             foreach ($data as $fieldKey => $field) {
                 // if there is no IfwPsn_Vendor_Zend_Validate_NotEmpty instance in the rules, we will use the default
                 if (!($notEmptyValidator = $this->_getNotEmptyValidatorInstance($validatorRule))) {
                     $notEmptyValidator = $this->_getValidator('NotEmpty');
                     $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldKey));
                 }
                 if (!$notEmptyValidator->isValid($field)) {
                     foreach ($notEmptyValidator->getMessages() as $messageKey => $message) {
                         if (!isset($messages[$messageKey])) {
                             $messages[$messageKey] = $message;
                         } else {
                             $messages[] = $message;
                         }
                     }
                     $errorsList[] = $notEmptyValidator->getErrors();
                     $emptyFieldsFound = true;
                 }
             }
             if ($emptyFieldsFound) {
                 $this->_invalidMessages[$validatorRule[self::RULE]] = $messages;
                 $this->_invalidErrors[$validatorRule[self::RULE]] = array_unique(call_user_func_array('array_merge', $errorsList));
                 return;
             }
         }
         if (!$validatorRule[self::VALIDATOR_CHAIN]->isValid($data)) {
             $this->_invalidMessages[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getMessages();
             $this->_invalidErrors[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getErrors();
             return;
         }
     } else {
         if (count($data) > 0) {
             // $data is actually a one element array
             $fieldNames = array_keys($data);
             $fieldName = reset($fieldNames);
             $field = reset($data);
             $failed = false;
             if (!is_array($field)) {
                 $field = array($field);
             }
             // if there is no IfwPsn_Vendor_Zend_Validate_NotEmpty instance in the rules, we will use the default
             if (!($notEmptyValidator = $this->_getNotEmptyValidatorInstance($validatorRule))) {
                 $notEmptyValidator = $this->_getValidator('NotEmpty');
                 $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldName));
             }
             if ($validatorRule[self::ALLOW_EMPTY]) {
                 $validatorChain = $validatorRule[self::VALIDATOR_CHAIN];
             } else {
                 $validatorChain = new IfwPsn_Vendor_Zend_Validate();
                 $validatorChain->addValidator($notEmptyValidator, true);
                 $validatorChain->addValidator($validatorRule[self::VALIDATOR_CHAIN]);
             }
             foreach ($field as $key => $value) {
                 if ($validatorRule[self::ALLOW_EMPTY] && !$notEmptyValidator->isValid($value)) {
                     // Field is empty AND it's allowed. Do nothing.
                     continue;
                 }
                 if (!$validatorChain->isValid($value)) {
                     if (isset($this->_invalidMessages[$validatorRule[self::RULE]])) {
                         $collectedMessages = $this->_invalidMessages[$validatorRule[self::RULE]];
                     } else {
                         $collectedMessages = array();
                     }
                     foreach ($validatorChain->getMessages() as $messageKey => $message) {
                         if (!isset($collectedMessages[$messageKey])) {
                             $collectedMessages[$messageKey] = $message;
                         } else {
                             $collectedMessages[] = $message;
                         }
                     }
                     $this->_invalidMessages[$validatorRule[self::RULE]] = $collectedMessages;
                     if (isset($this->_invalidErrors[$validatorRule[self::RULE]])) {
                         $this->_invalidErrors[$validatorRule[self::RULE]] = array_merge($this->_invalidErrors[$validatorRule[self::RULE]], $validatorChain->getErrors());
                     } else {
                         $this->_invalidErrors[$validatorRule[self::RULE]] = $validatorChain->getErrors();
                     }
                     unset($this->_validFields[$fieldName]);
                     $failed = true;
                     if ($validatorRule[self::BREAK_CHAIN]) {
                         return;
                     }
                 }
             }
             if ($failed) {
                 return;
             }
         }
     }
     /**
      * If we got this far, the inputs for this rule pass validation.
      */
     foreach ((array) $validatorRule[self::FIELDS] as $field) {
         if (array_key_exists($field, $data)) {
             $this->_validFields[$field] = $data[$field];
         }
     }
 }