/**
  * Set the value of the field
  *
  * @param integer|array $value The new value of the field
  * @return \FormHandler\Field\Temperature
  * @author Marien den Besten
  */
 public function setValue($value, $forced = false)
 {
     $this->empty->setValue(0, $forced);
     if (is_object($value) && method_exists($value, 'getTemperature') && method_exists($value, 'getUnit')) {
         $unit = $value->getUnit() != 'fahrenheit' || $value->getUnit() != 0 ? self::UNIT_CELSIUS : self::UNIT_FAHRENHEIT;
         parent::setValue(array($value->getTemperature(), $unit), $forced);
         $this->temperature->setValue($value->getTemperature(), $forced);
         $this->unit->setValue($unit, $forced);
     } elseif (is_array($value) && count($value) == 2) {
         parent::setValue($value, $forced);
         $this->temperature->setValue($value[0], $forced);
         $this->unit->setValue($value[1], $forced);
     } elseif (is_numeric($value)) {
         $this->temperature->setValue($value, $forced);
         parent::setValue(array($value, $this->unit->getValue()), $forced);
     } elseif (is_null($value)) {
         $this->empty->setValue(1, $forced);
         parent::setValue(null, $forced);
     }
     $current_unit = $this->unit->getValue();
     $preferred_unit = $this->preferred_unit;
     if (!is_null($value) && !is_null($preferred_unit) && $current_unit != $preferred_unit) {
         $value = $this->convert($this->temperature->getValue(), $current_unit, $preferred_unit);
         $this->temperature->setValue($value, $forced);
         $this->unit->setValue($preferred_unit, $forced);
         parent::setValue(array($value, $preferred_unit), $forced);
     }
     $this->value_set = true;
     return $this;
 }
Beispiel #2
0
 public function setValue($value, $forced = false)
 {
     $this->empty->setValue(0, $forced);
     if (is_null($value)) {
         $this->empty->setValue(1, $forced);
         return parent::setValue(null, $forced);
     }
     return parent::setValue($value, $forced);
 }