/** * Test __callStatic * * @author Tom Haskins-Vaughan <*****@*****.**> * @since 0.3.0 */ public function testCallStatic() { $weight = Weight::LB(10, 20); $this->assertSame('LB', $weight->getUom()->getName()); $this->assertSame('1/2', (string) $weight->getAmount()); $newWeight = $weight->to(Uom::OZ()); $this->assertSame('8', (string) $newWeight->getAmount()); }
/** * Whether this Uom is equivalent to another * * @author Tom Haskins-Vaughan <*****@*****.**> * @since 0.10.0 * * @param Uom $other * * @return bool */ public function isSameValueAs(Uom $other) { return $this->getName() === $other->getName(); }
/** * isSameValueAsProvider * * @author Tom Haskins-Vaughan <*****@*****.**> * @since 0.10.0 * * @return array */ public static function isSameValueAsProvider() { $oz = Uom::OZ(); return [[$oz, $oz, true], [Uom::OZ(), Uom::OZ(), true], [Uom::LB(), Uom::LB(), true], [Uom::KG(), Uom::KG(), true], [Uom::LB(), Uom::KG(), false], [Uom::OZ(), Uom::LB(), false]]; }
/** * Create Quantity from a string, e.g. * * * 1 LB * * 0.5 KG * * 1/2 OZ * * @author Tom Haskins-Vaughan <*****@*****.**> * @since 0.12.0 * * @param string $string * * @return mixed */ public static function fromString($string) { // trim white space $string = trim($string); // look for the first Uom at the end of the string foreach (Uom::getUoms() as $uomGroup) { foreach ($uomGroup as $uomName => $description) { $expectedPostion = strlen($string) - strlen($uomName); $actualPosition = strpos($string, $uomName); if ($expectedPostion === $actualPosition) { // ok, we've found a Uom, remove it, leaving the amount $amountAsString = trim(str_replace($uomName, '', $string)); // now see if the rest is a fraction try { return new static(Fraction::fromString($amountAsString), new Uom($uomName)); } catch (InvalidArgumentException $e) { } // no, so see if it is float return new static(Fraction::fromFloat($amountAsString), new Uom($uomName)); } } } throw new InvalidArgumentException(sprintf('Cannot parse "%s" as a %s', $string, get_called_class())); }