Пример #1
0
 public function concat(Converter $converter)
 {
     if ($converter instanceof self) {
         $offset = $this->offset + $converter->getOffset();
         return $offset == 0.0 ? Converter::IDENTITY() : new self($offset);
     }
     return parent::concat($converter);
 }
 public function concat(Converter $converter)
 {
     if ($converter instanceof self) {
         $factor = $this->factor * $converter->getFactor();
         return $factor == 1.0 ? Converter::IDENTITY() : new self($factor);
     }
     return parent::concat($converter);
 }
 public function concat(Converter $converter)
 {
     if (!$converter instanceof self) {
         return parent::concat($converter);
     }
     $dividend = $this->dividend * $converter->getDividend();
     $divisor = $this->divisor * $converter->getDivisor();
     $gcd = $this->gcd($dividend, $divisor);
     $dividend = $dividend / $gcd;
     $divisor = $divisor / $gcd;
     if ($dividend == 1 && $divisor == 1) {
         return Converter::IDENTITY();
     }
     return new self($dividend, $divisor);
 }
Пример #4
0
 public function getConverterTo(Unit $that)
 {
     if ($this->equals($that)) {
         return Converter::IDENTITY();
     }
     $thisSystemUnit = $this->getStandardUnit();
     $thatSystemUnit = $that->getStandardUnit();
     if ($thisSystemUnit->equals($thatSystemUnit)) {
         return $that->toStandardUnit()->inverse()->concat($this->toStandardUnit());
     }
     if (!$thisSystemUnit->getDimension()->equals($thatSystemUnit->getDimension())) {
         throw new \RuntimeException(sprintf('Dimension %s is not compatible with dimension %s.', $thisSystemUnit->getDimension()->getSymbol(), $thatSystemUnit->getDimension()->getSymbol()));
     }
     $thisTransform = $this->toStandardUnit()->concat($thisSystemUnit->toBaseUnits());
     $thatTransform = $that->toStandardUnit()->concat($thatSystemUnit->toBaseUnits());
     return $thatTransform->inverse()->concat($thisTransform);
 }
Пример #5
0
 public function toBaseUnits()
 {
     $result = Converter::IDENTITY();
     foreach ($this->elements as $element) {
         $unit = $element->unit;
         $converter = $unit->toBaseUnits();
         if (!$converter->isLinear()) {
             throw new \RuntimeException($unit->getSymbol() . ' is non-linear.');
         }
         if ($element->root != 1) {
             throw new \RuntimeException($unit->getSymbol() . ' holds a base unit with fractional exponent');
         }
         $pow = $element->pow;
         if ($pow < 0) {
             $pow = -$pow;
             $converter = $converter->inverse();
         }
         for ($j = 0; $j < $pow; ++$j) {
             $result = $result->concat($converter);
         }
     }
     return $result;
 }
Пример #6
0
 public function toBaseUnits()
 {
     return Converter::IDENTITY();
 }
Пример #7
0
 public function toStandardUnit()
 {
     return Converter::IDENTITY();
 }