Beispiel #1
0
 public function to(Unit $unit)
 {
     if ($unit == $this->unit || $unit->equals($this->unit)) {
         return $this;
     }
     return self::valueOf($this->unit->getConverterTo($unit)->convert($this->value), $unit);
 }
 public function equals(Unit $that)
 {
     if ($this == $that) {
         return true;
     }
     if (!$that instanceof self) {
         return false;
     }
     return $this->symbol == $that->getSymbol();
 }
 /**
  * @param Unit   $unit
  * @param int    $root
  * @param string $quantity
  * @param string $symbol
  *
  * @return CompoundUnit
  */
 public static function getRootInstance(Unit $unit, $root, $quantity = '', $symbol = '')
 {
     if ($unit instanceof CompoundUnit) {
         $elements = [];
         foreach ($unit->getElements() as $element) {
             $gcd = self::gcd(abs($element->pow), $element->root * $root);
             $elements[] = new CompoundElement($element->unit, $element->pow / $gcd, $element->root * $root / $gcd);
         }
     } else {
         $elements = [new CompoundElement($unit, 1, $root)];
     }
     return self::getInstance($elements, [], $quantity, $symbol);
 }
 /**
  * Build a measure with the given value and the given unit.
  *
  * @param float $value
  * @param Unit  $unit
  *
  * @return Measure
  */
 public static function build($value, Unit $unit)
 {
     switch ($unit->getQuantity()) {
         case Acceleration::class:
             return new AccelerationMeasure($value, $unit);
         case AmountOfSubstance::class:
             return new AmountOfSubstanceMeasure($value, $unit);
         case Angle::class:
             return new AngleMeasure($value, $unit);
         case Area::class:
             return new AreaMeasure($value, $unit);
         case CatalyticActivity::class:
             return new CatalyticActivityMeasure($value, $unit);
         case Constant::class:
             return new ConstantMeasure($value, $unit);
         case DataAmount::class:
             return new DataAmountMeasure($value, $unit);
         case DataRate::class:
             return new DataRateMeasure($value, $unit);
         case Dimensionless::class:
             return new DimensionlessMeasure($value, $unit);
         case Duration::class:
             return new DurationMeasure($value, $unit);
         case DynamicViscosity::class:
             return new DynamicViscosityMeasure($value, $unit);
         case ElectricCapacitance::class:
             return new ElectricCapacitanceMeasure($value, $unit);
         case ElectricCharge::class:
             return new ElectricChargeMeasure($value, $unit);
         case ElectricConductance::class:
             return new ElectricConductanceMeasure($value, $unit);
         case ElectricCurrent::class:
             return new ElectricCurrentMeasure($value, $unit);
         case ElectricInductance::class:
             return new ElectricInductanceMeasure($value, $unit);
         case ElectricPotential::class:
             return new ElectricPotentialMeasure($value, $unit);
         case ElectricResistance::class:
             return new ElectricResistanceMeasure($value, $unit);
         case Energy::class:
             return new EnergyMeasure($value, $unit);
         case Force::class:
             return new ForceMeasure($value, $unit);
         case Frequency::class:
             return new FrequencyMeasure($value, $unit);
         case Illuminance::class:
             return new IlluminanceMeasure($value, $unit);
         case KinematicViscosity::class:
             return new KinematicViscosityMeasure($value, $unit);
         case Length::class:
             return new LengthMeasure($value, $unit);
         case Level::class:
             return new LevelMeasure($value, $unit);
         case LuminousFlux::class:
             return new LuminousFluxMeasure($value, $unit);
         case LuminousIntensity::class:
             return new LuminousIntensityMeasure($value, $unit);
         case MagneticFlux::class:
             return new MagneticFluxMeasure($value, $unit);
         case MagneticFluxDensity::class:
             return new MagneticFluxDensityMeasure($value, $unit);
         case Mass::class:
             return new MassMeasure($value, $unit);
         case Power::class:
             return new PowerMeasure($value, $unit);
         case Pressure::class:
             return new PressureMeasure($value, $unit);
         case RadiationDoseAbsorbed::class:
             return new RadiationDoseAbsorbedMeasure($value, $unit);
         case RadiationDoseEffective::class:
             return new RadiationDoseEffectiveMeasure($value, $unit);
         case RadioactiveActivity::class:
             return new RadioactiveActivityMeasure($value, $unit);
         case Ratio::class:
             return new RatioMeasure($value, $unit);
         case SolidAngle::class:
             return new SolidAngleMeasure($value, $unit);
         case Temperature::class:
             return new TemperatureMeasure($value, $unit);
         case Velocity::class:
             return new VelocityMeasure($value, $unit);
         case Volume::class:
             return new VolumeMeasure($value, $unit);
         default:
             throw new \InvalidArgumentException(sprintf('Unknown quantity "%s" for unit "%s".', get_class($unit->getQuantity()), $unit->getSymbol()));
     }
 }
 public function root($n)
 {
     return new self($this->unit->root($n));
 }