示例#1
0
 /**
  * Convert localized string representations to integer, float or date values
  *
  * Subclasses can support localized input formats by calling this method
  * from a filter.
  *
  * Non-string values get trimmed and converted to integer, float or
  * \DateTime, depending on $type. Invalid values are returned as string. The
  * input filter should validate filtered data by checking the datatype via
  * validateType().
  *
  * @param string $value Localized input string
  * @param string $type Data type (integer, float, date). Any other value will be ignored.
  * @return mixed Normalized value or input string
  */
 public function normalize($value, $type)
 {
     // Integers and floats are validated first to prevent successful parsing
     // of strings containing invalid characters with the invalid part simply
     // cut off.
     switch ($type) {
         case 'integer':
             $value = trim($value);
             if (\Zend\Validator\StaticValidator::execute($value, 'Zend\\I18n\\Validator\\IsInt')) {
                 $value = \Zend\Filter\StaticFilter::execute($value, 'Zend\\I18n\\Filter\\NumberParse', array('type' => \NumberFormatter::TYPE_INT32));
             }
             break;
         case 'float':
             $value = trim($value);
             if (\Zend\Validator\StaticValidator::execute($value, 'Zend\\I18n\\Validator\\IsFloat')) {
                 $value = \Zend\Filter\StaticFilter::execute($value, 'Zend\\I18n\\Filter\\NumberParse', array('type' => \NumberFormatter::TYPE_DOUBLE));
             }
             break;
         case 'date':
             $value = trim($value);
             $validator = new \Zend\I18n\Validator\DateTime();
             $validator->setDateType(\IntlDateFormatter::SHORT);
             if ($validator->isValid($value)) {
                 // Some systems accept invalid date separators, like '/'
                 // with a de_DE locale which should accept only '.'.
                 // An extra comparision of the locale-specific pattern and
                 // the input string is necessary.
                 // This also enforces 4-digit years to avoid any confusion
                 // with 2-digit year input.
                 $pattern = preg_quote($validator->getPattern(), '#');
                 // Get the year part out of the way first.
                 $pattern = preg_replace('/y+/', '§', $pattern);
                 // Remaining letters are placeholders for digits.
                 $pattern = preg_replace('/[a-zA-Z]+/', '\\d+', $pattern);
                 // Set the year pattern.
                 $pattern = str_replace('§', '\\d{4}', $pattern);
                 if (preg_match("#^{$pattern}\$#", $value)) {
                     $formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE, 'UTC');
                     $value = \DateTime::createFromFormat('U', $formatter->parse($value));
                 }
             }
             break;
     }
     return $value;
 }
示例#2
0
 /**
  * Função de definição de validações de campos
  * @param  array  $params
  * @param  object $mainFilter
  * @return object
  */
 private function defineValidation(array $params, $mainFilter)
 {
     $factory = new ZendInputFilterFactory();
     $validatorChain = new ZendValidatorChain();
     $validatorsField = array();
     $iCount = 0;
     if (isset($params['validation']) and strtolower(trim($params['validation'])) == 'required') {
         $validatorsField['name'] = $params['name'];
         $validatorsField['required'] = true;
         $validatorsField['validators'][$iCount] = array('name' => 'NotEmpty');
         $validatorsField['validators'][$iCount]['options']['messages'] = array(\Zend\Validator\NotEmpty::IS_EMPTY => 'Campo de preenchimento obrigatório');
         $iCount++;
     } else {
         $validatorsField['name'] = $params['name'];
         $validatorsField['required'] = false;
     }
     if (isset($params['validationtype'])) {
         /* Verifica o tipo de validação utilizada e formata para continuar */
         if (!is_array($params['validationtype'])) {
             $params['validationtype'] = array($params['validationtype']);
         }
         /* Procura o tipo de validação e define o mesmo */
         foreach ($params['validationtype'] as $value) {
             $options = array();
             //$options['translator'] = $this->translator;
             switch (strtolower($value)) {
                 /* Zend Validator */
                 case 'alphanum':
                     if (isset($params['permiteespaco']) and !empty($params['permiteespaco'])) {
                         $validatorsField['validators'][$iCount] = array('name' => 'Alnum', 'options' => array($options, 'allowWhiteSpace' => true));
                     } else {
                         $validatorsField['validators'][$iCount] = array('name' => 'Alnum');
                     }
                     break;
                 case 'alpha':
                     if (isset($params['permiteespaco']) and !empty($params['permiteespaco'])) {
                         $validatorsField['validators'][$iCount] = array('name' => 'Alpha', 'options' => array($options, 'allowWhiteSpace' => true));
                     } else {
                         $validatorsField['validators'][$iCount] = array('name' => 'Alpha');
                     }
                     break;
                 case 'barcode':
                     $validatorsField['validators'][$iCount] = array('name' => 'Barcode', 'options' => array($options, 'adapter' => $params['barcodeadapter']));
                     break;
                 case 'between':
                     if (isset($params['validationmax']) and !empty($params['validationmax'])) {
                         $options['max'] = $params['validationmax'];
                     }
                     if (isset($params['validationmin']) and !empty($params['validationmin'])) {
                         $options['min'] = $params['validationmin'];
                     }
                     $options['inclusive'] = true;
                     $validatorsField['validators'][$iCount] = array('name' => 'Between', 'options' => $options);
                     break;
                 case 'callback':
                     $validatorsField['validators'][$iCount] = array('name' => 'Callback', 'options' => $options);
                     break;
                 case 'creditcard':
                     $validatorsField['validators'][$iCount] = array('name' => 'CreditCard', 'options' => $options);
                     break;
                 case 'date':
                     $options['format'] = 'Y-m-d';
                     $validatorsField['validators'][$iCount] = array('name' => 'Date', 'options' => $options);
                     break;
                 case 'time':
                     $options['format'] = 'H:i:s';
                     $validatorsField['validators'][$iCount] = array('name' => 'Date', 'options' => $options);
                     break;
                 case 'datetime':
                     $validatorObj = new \Zend\I18n\Validator\DateTime();
                     $validatorObj->setDateType(\IntlDateFormatter::SHORT);
                     $validatorObj->setTimeType(\IntlDateFormatter::SHORT);
                     $validatorsField['validators'][$iCount] = $validatorChain->attach($validatorObj);
                     break;
                 case 'recordexists':
                     $options['adapter'] = \Cityware\Db\Factory::factory('zend')->getAdapter();
                     if (isset($params['recordColumn']) and !empty($params['recordColumn'])) {
                         $options['field'] = $params['recordColumn'];
                     } else {
                         $options['field'] = $params['name'];
                     }
                     if (isset($params['recordTable']) and !empty($params['recordTable'])) {
                         $options['table'] = $params['recordTable'];
                     } else {
                         $options['table'] = $this->formDefaultConfig['table'];
                     }
                     if (isset($params['recordSchema']) and !empty($params['recordSchema'])) {
                         $options['schema'] = $params['recordSchema'];
                     } else {
                         $options['schema'] = $this->formDefaultConfig['schema'];
                     }
                     if (isset($params['exclude']) and $params['exclude'] == 'true') {
                         if (isset($params['excludeCol']) and !empty($params['excludeCol'])) {
                             $options['exclude']['field'] = $params['excludeCol'];
                         } else {
                             throw new \Exception('Não foi definido nenhuma coluna de exclusão!', 500);
                         }
                         if (isset($params['excludeColValue']) and !empty($params['excludeColValue'])) {
                             $options['exclude']['value'] = \Cityware\Format\Str::preparePhpTag($params['excludeColValue'], false);
                         } else {
                             if (isset($params['excludeUrlParam']) and !empty($params['excludeUrlParam'])) {
                                 $options['exclude']['value'] = $this->getUrlParam($params['excludeCol']);
                             } else {
                                 throw new \Exception('Não foi definido nenhum valor de exclusão!', 500);
                             }
                         }
                     }
                     $validatorsField['validators'][$iCount] = array('name' => 'Db\\RecordExists', 'options' => $options);
                     break;
                 case 'norecordexists':
                     $options['adapter'] = \Cityware\Db\Factory::factory('zend')->getAdapter();
                     if (isset($params['recordColumn']) and !empty($params['recordColumn'])) {
                         $options['field'] = $params['recordColumn'];
                     } else {
                         $options['field'] = $params['name'];
                     }
                     if (isset($params['recordTable']) and !empty($params['recordTable'])) {
                         $options['table'] = $params['recordTable'];
                     } else {
                         $options['table'] = $this->formDefaultConfig['table'];
                     }
                     if (isset($params['recordSchema']) and !empty($params['recordSchema'])) {
                         $options['schema'] = $params['recordSchema'];
                     } else {
                         $options['schema'] = $this->formDefaultConfig['schema'];
                     }
                     if (isset($params['exclude']) and $params['exclude'] == 'true') {
                         if (isset($params['excludeCol']) and !empty($params['excludeCol'])) {
                             $options['exclude']['field'] = $params['excludeCol'];
                         } else {
                             throw new \Exception('Não foi definido nenhuma coluna de exclusão!', 500);
                         }
                         if (isset($params['excludeColValue']) and !empty($params['excludeColValue'])) {
                             $options['exclude']['value'] = \Cityware\Format\Str::preparePhpTag($params['excludeColValue'], false);
                         } else {
                             if (isset($params['excludeUrlParam']) and !empty($params['excludeUrlParam'])) {
                                 $options['exclude']['value'] = $this->getUrlParam($params['excludeCol']);
                             } else {
                                 throw new \Exception('Não foi definido nenhum valor de exclusão!', 500);
                             }
                         }
                     }
                     $validatorsField['validators'][$iCount] = array('name' => 'Db\\NoRecordExists', 'options' => $options);
                     break;
                 case 'digits':
                 case 'int':
                 case 'integer':
                     $validatorsField['validators'][$iCount] = array('name' => 'Digits', 'options' => $options);
                     break;
                 case 'email':
                     $validatorsField['validators'][$iCount] = array('name' => 'EmailAddress', 'options' => array($options, 'allow' => \Zend\Validator\Hostname::ALLOW_DNS, 'mx' => false, 'domain' => true));
                     break;
                 case 'greaterthan':
                     if (isset($params['validationmin']) and !empty($params['validationmin'])) {
                         $options['min'] = $params['validationmin'];
                     }
                     $validatorsField['validators'][$iCount] = array('name' => 'GreaterThan', 'options' => $options);
                     break;
                 case 'hex':
                     $validatorsField['validators'][$iCount] = array('name' => 'Hex', 'options' => $options);
                     break;
                 case 'hostname':
                     $validatorsField['validators'][$iCount] = array('name' => 'Hostname', 'options' => $options);
                     break;
                 case 'iban':
                     $validatorsField['validators'][$iCount] = array('name' => 'Iban', 'options' => $options);
                     break;
                 case 'identical':
                     $validatorsField['validators'][$iCount] = array('name' => 'Identical', 'options' => array($options, 'token' => $params['validationcompare']));
                     break;
                 case 'ip':
                     $validatorsField['validators'][$iCount] = array('name' => 'Ip', 'options' => $options);
                     break;
                 case 'isbn':
                     $validatorsField['validators'][$iCount] = array('name' => 'Isbn', 'options' => $options);
                     break;
                 case 'postcode':
                     $validatorsField['validators'][$iCount] = array('name' => 'PostCode', 'options' => $options);
                     break;
                 case 'lessthan':
                     if (isset($params['validationmax']) and !empty($params['validationmax'])) {
                         $options['max'] = $params['validationmax'];
                     }
                     $validatorsField['validators'][$iCount] = array('name' => 'LessThan', 'options' => $options);
                     break;
                 case 'regex':
                     $validatorsField['validators'][$iCount] = array('name' => 'Regex', 'options' => array($options, 'pattern' => $params['regexrule']));
                     break;
                 case 'stringlength':
                     if (isset($params['validationmax']) and !empty($params['validationmax'])) {
                         $options['max'] = $params['validationmax'];
                     }
                     if (isset($params['validationmin']) and !empty($params['validationmin'])) {
                         $options['min'] = $params['validationmin'];
                     }
                     $validatorsField['validators'][$iCount] = array('name' => 'StringLength', 'options' => $options);
                     break;
                     /* Customizados */
                 /* Customizados */
                 case 'custom':
                     $validatorsField['validators'][$iCount] = $validatorChain->attach(new $params['customclass']());
                     break;
                 case 'cpf':
                     $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\Cpf());
                     break;
                 case 'cnpj':
                     $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\Cnpj());
                     break;
                 case 'renavam':
                     $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\Renavam());
                     break;
                 case 'rg':
                     $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\Rg());
                     break;
                 case 'strongpassword':
                     $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\StrongPassword());
                     break;
                 case 'mediumpassword':
                     $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\MediumPassword());
                     break;
                 case 'easypassword':
                     $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\EasyPassword());
                     break;
                 case 'float':
                     $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Zend\I18n\Validator\Float());
                     break;
                 case 'seterrorcustom':
                     $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\SetErrorCustom());
                     break;
             }
             if (isset($params['messages']) and !empty($params['messages'])) {
                 $validatorsField['validators'][$iCount]['options']['messages'] = $params['messages'];
             }
             $iCount++;
         }
     }
     $mainFilter->add($factory->createInput($validatorsField));
     return $mainFilter;
 }