Ejemplo n.º 1
0
 public function normalize($input, $property, model_editor $editor)
 {
     if ($this->isReadOnly) {
         return null;
     }
     // don't normalize input provided by editor, but separately read input
     // from multiple included fields
     $field = $editor->propertyToField($property);
     $amount = trim(input::vget("{$field}_amount"));
     $currency = trim(input::vget("{$field}_currency"));
     // consider missing monetary input if amount is empty
     if ($amount === '') {
         return null;
     }
     if (array_key_exists($currency, static::$currencies)) {
         // normalize entered amount according to provided set of supported notations
         foreach (static::$amountNotations as $amountPattern) {
             if (preg_match($amountPattern, $amount, $matches)) {
                 return sprintf('%s%s.%02d %s', $matches[1] == '-' ? '-' : '', strtr($matches[2], array('.' => '', ',' => '')), $matches[3], $currency);
             }
         }
     }
     // provide amount w/o currency as fallback (causing validation failure)
     return $amount;
 }