示例#1
0
 /**
  * isSameValueAs
  *
  * ValueObject comparison
  *
  * @author Christopher Tatro <*****@*****.**>
  * @since 1.1.0
  *
  * @param Fraction $fraction
  *
  * @return bool
  */
 public function isSameValueAs(Fraction $fraction)
 {
     if ($this->getNumerator() != $fraction->getNumerator()) {
         return false;
     }
     if ($this->getDenominator() != $fraction->getDenominator()) {
         return false;
     }
     return true;
 }
示例#2
0
 /**
  * Subtract a given fraction from this fraction
  *
  * @author Tom Haskins-Vaughan <*****@*****.**>
  * @since  0.1.0
  *
  * @param Fraction $fraction
  *
  * @return Fraction
  */
 public function subtract(Fraction $fraction)
 {
     $numerator = $this->getNumerator() * $fraction->getDenominator() - $fraction->getNumerator() * $this->getDenominator();
     $denominator = $this->getDenominator() * $fraction->getDenominator();
     return new static($numerator, $denominator);
 }
示例#3
0
 /**
  * Test isSameValueAs
  *
  * @author Christopher Tatro <*****@*****.**>
  * @since 1.1.0
  *
  * @dataProvider isSameValueAsProvider
  */
 public function testIsSameValueAs($numerator1, $denominator1, $numerator2, $denominator2, $result)
 {
     $fraction = new Fraction($numerator1, $denominator1);
     $this->assertSame($result, $fraction->isSameValueAs(new Fraction($numerator2, $denominator2)));
 }
示例#4
0
 /**
  * Test fromString exception
  *
  * @author Tom Haskins-Vaughan <*****@*****.**>
  * @since  0.4.0
  *
  * @expectedException \InvalidArgumentException
  * @dataProvider fromStringExceptionProvider
  */
 public function testFromStringException($string)
 {
     Fraction::fromString($string);
 }
示例#5
0
 /**
  * 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()));
 }