Changeset 1463

Show
Ignore:
Timestamp:
01/07/07 04:00:26 (2 years ago)
Author:
dominik
Message:

add in_locale, no_locale and cast_to parameters to the number validator

refs #359

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/0.11/src/validator/AgaviNumberValidator.class.php

    r1245 r1463  
    4747  protected function validate() 
    4848  { 
    49     $value = $this->getData($this->getArgument()); 
     49    $value =& $this->getData($this->getArgument()); 
     50 
     51    $hasExtraChars = false; 
     52 
     53    if(!$this->getParameter('no_locale', false)) { 
     54      if($locale = $this->getParameter('in_locale')) { 
     55        $locale = $this->getContext()->getTranslationManager()->getLocaleFromIdentifier($locale); 
     56      } else { 
     57        $locale = $this->getContext()->getTranslationManager()->getCurrentLocale(); 
     58      } 
     59 
     60      $value = AgaviDecimalFormatter::parse($value, $locale, $hasExtraChars); 
     61    } 
    5062 
    5163    switch(strtolower($this->getParameter('type'))) { 
    5264      case 'int': 
    5365      case 'integer': 
    54         $temp = (int) $value; 
    55         if(((string)$temp) !== ((string)$value) ) { 
     66        if(!is_int($value) || $hasExtraChars) { 
    5667          $this->throwError('type'); 
    5768          return false; 
     
    6172       
    6273      case 'float': 
    63         if(!is_numeric($value)) { 
     74        if(!is_float($value) || $hasExtraChars) { 
    6475          $this->throwError('type'); 
    6576          return false; 
     
    6778         
    6879        break; 
     80    } 
     81 
     82    switch(strtolower($this->getParameter('cast_to'))) { 
     83      case 'int': 
     84      case 'integer': 
     85        $value = (int) $value; 
     86        break; 
     87 
     88      case 'float': 
     89      case 'double': 
     90        $value = (float) $value; 
     91        break; 
     92 
    6993    } 
    7094