Commit 39ede4af authored by Bojan Živanović's avatar Bojan Živanović
Browse files

Issue #3016669 by bojanz, viappidu: Add "kilometer" and "nautical mile" length units

parent 11415a30
Loading
Loading
Loading
Loading
+8 −1
Changes for src/LengthUnit.php: 8 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -10,8 +10,10 @@ final class LengthUnit implements UnitInterface {
  const MILLIMETER = 'mm';
  const CENTIMETER = 'cm';
  const METER = 'm';
  const KILOMETER = 'km';
  const INCH = 'in';
  const FOOT = 'ft';
  const NAUTICAL_MILE = 'M';

  /**
   * {@inheritdoc}
@@ -21,8 +23,10 @@ final class LengthUnit implements UnitInterface {
      self::MILLIMETER => t('mm'),
      self::CENTIMETER => t('cm'),
      self::METER => t('m'),
      self::KILOMETER => t('km'),
      self::INCH => t('in'),
      self::FOOT => t('ft'),
      self::NAUTICAL_MILE => t('M'),
    ];
  }

@@ -42,8 +46,10 @@ final class LengthUnit implements UnitInterface {
      self::MILLIMETER => '0.001',
      self::CENTIMETER => '0.01',
      self::METER => '1',
      self::KILOMETER => '1000',
      self::INCH => '0.0254',
      self::FOOT => '0.3048',
      self::NAUTICAL_MILE => '1852',
    ];

    return $factors[$unit];
@@ -54,7 +60,8 @@ final class LengthUnit implements UnitInterface {
   */
  public static function assertExists($unit) {
    $allowed_units = [
      self::MILLIMETER, self::CENTIMETER, self::METER, self::INCH, self::FOOT,
      self::MILLIMETER, self::CENTIMETER, self::METER, self::KILOMETER,
      self::INCH, self::FOOT, self::NAUTICAL_MILE,
    ];
    if (!in_array($unit, $allowed_units)) {
      throw new \InvalidArgumentException(sprintf('Invalid length unit "%s" provided.', $unit));
+2 −0
Changes for tests/src/Unit/LengthTest.php: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -45,8 +45,10 @@ class LengthTest extends UnitTestCase {
  public function testConvert() {
    $this->assertEquals(new Length('3000', 'mm'), $this->length->convert('mm')->round());
    $this->assertEquals(new Length('300', 'cm'), $this->length->convert('cm')->round());
    $this->assertEquals(new Length('0.003', 'km'), $this->length->convert('km')->round(3));
    $this->assertEquals(new Length('118.110', 'in'), $this->length->convert('in')->round(3));
    $this->assertEquals(new Length('9.843', 'ft'), $this->length->convert('ft')->round(3));
    $this->assertEquals(new Length('0.00162', 'M'), $this->length->convert('M')->round(5));
  }

}