From f83ddbc41d4bbea030e21b1351a136931c83028e Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Fri, 6 Oct 2017 21:38:15 -0500 Subject: [PATCH 01/22] Feat: add symfony/var-dumper dev dependency --- composer.json | 3 ++- composer.lock | 72 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index a389e6b9..0fc44434 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "require": {}, "require-dev": { "phpdocumentor/phpdocumentor": "^2.9", - "phpunit/phpunit": "^6.3" + "phpunit/phpunit": "^6.3", + "symfony/var-dumper": "^3.3" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 8745a833..b081b398 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "532e5a91b038ecf0d80bcf2237e5320b", - "content-hash": "2af6f818d023630e8ab459274fe709e7", + "hash": "4c4bdccb729468d9427a803397d110e5", + "content-hash": "efc945298d2d52904654305be7f1c67f", "packages": [], "packages-dev": [ { @@ -3245,6 +3245,74 @@ "homepage": "https://symfony.com", "time": "2017-10-01 21:00:16" }, + { + "name": "symfony/var-dumper", + "version": "v3.3.10", + "source": { + "type": "git", + "url": "https://github.com/symfony/var-dumper.git", + "reference": "03e3693a36701f1c581dd24a6d6eea2eba2113f6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/var-dumper/zipball/03e3693a36701f1c581dd24a6d6eea2eba2113f6", + "reference": "03e3693a36701f1c581dd24a6d6eea2eba2113f6", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0" + }, + "require-dev": { + "ext-iconv": "*", + "twig/twig": "~1.34|~2.4" + }, + "suggest": { + "ext-iconv": "To convert non-UTF-8 strings to UTF-8 (or symfony/polyfill-iconv in case ext-iconv cannot be used).", + "ext-symfony_debug": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "files": [ + "Resources/functions/dump.php" + ], + "psr-4": { + "Symfony\\Component\\VarDumper\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony mechanism for exploring and dumping PHP variables", + "homepage": "https://symfony.com", + "keywords": [ + "debug", + "dump" + ], + "time": "2017-10-02 06:42:24" + }, { "name": "theseer/tokenizer", "version": "1.1.0", From 7de15180668d380646ff4e06555871953f296501 Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Sat, 7 Oct 2017 22:49:47 -0500 Subject: [PATCH 02/22] Update: test namespace & directory hierarchy --- composer.json | 4 ++-- composer.lock | 2 +- phpunit.xml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 0fc44434..ff6ed53c 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ } ], "scripts": { - "test": "phpunit --configuration='./phpunit.xml' --test-suffix='.spec.php' --color=always ./dev/tests/" + "test": "phpunit --configuration='./phpunit.xml' --test-suffix='.spec.php' --color=always ./tests/" }, "require": {}, "require-dev": { @@ -26,7 +26,7 @@ }, "autoload-dev": { "psr-4": { - "UnitConverter\\Tests\\Unit\\": "dev/tests/unit/UnitConverter/" + "UnitConverter\\Tests\\Unit\\": "tests/unit/UnitConverter/" } } } diff --git a/composer.lock b/composer.lock index b081b398..19ecdd86 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "4c4bdccb729468d9427a803397d110e5", + "hash": "9901e795bf35ddf4769f687f0d580f8f", "content-hash": "efc945298d2d52904654305be7f1c67f", "packages": [], "packages-dev": [ diff --git a/phpunit.xml b/phpunit.xml index 47360231..d324740f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -14,7 +14,7 @@ > - dev/tests/ + tests/ From ed0ffe6e69ad35a9a29b9b5ce2664e8694b8558c Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Sun, 8 Oct 2017 23:28:41 -0500 Subject: [PATCH 03/22] Update: add unit testsuite --- phpunit.xml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index d324740f..f30c4d2c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -12,15 +12,21 @@ verbose="true" colors="true" > + + + src/ + + + - + tests/ - - - src/ - - + + + tests/unit/ + + From a6f63950c990445df571df70092c6988eedabfea Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:37:53 -0500 Subject: [PATCH 04/22] Feat: list of default measurements via class constants --- src/UnitConverter/Measure.php | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/UnitConverter/Measure.php diff --git a/src/UnitConverter/Measure.php b/src/UnitConverter/Measure.php new file mode 100644 index 00000000..43c06dc2 --- /dev/null +++ b/src/UnitConverter/Measure.php @@ -0,0 +1,46 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter; + +/** + * A static class containing constants that define the available + * default types of measurement. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Measure +{ + const LENGTH = "length"; + + const AREA = "area"; + + const VOLUME = "volume"; + + const WEIGHT = "weight"; + + const SPEED = "speed"; + + const ROTATION = "rotation"; + + const TEMPERATURE = "temperature"; + + const PRESSURE = "pressure"; + + const TIME = "time"; + + const ENERGY = "energy"; +} From 41a2b923afb833017156371b074207097435438c Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:40:07 -0500 Subject: [PATCH 05/22] Feat: interface and unit tests for the abstract Unit of measure class --- src/UnitConverter/Unit/AbstractUnit.php | 122 ++++++++++++++++++ src/UnitConverter/Unit/UnitInterface.php | 95 ++++++++++++++ .../UnitConverter/Unit/AbstractUnit.spec.php | 118 +++++++++++++++++ 3 files changed, 335 insertions(+) create mode 100644 src/UnitConverter/Unit/AbstractUnit.php create mode 100644 src/UnitConverter/Unit/UnitInterface.php create mode 100644 tests/unit/UnitConverter/Unit/AbstractUnit.spec.php diff --git a/src/UnitConverter/Unit/AbstractUnit.php b/src/UnitConverter/Unit/AbstractUnit.php new file mode 100644 index 00000000..df9baa42 --- /dev/null +++ b/src/UnitConverter/Unit/AbstractUnit.php @@ -0,0 +1,122 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit; + +/** + * This class is the base class for all unit of measurement classes. When creating + * a new/custom unit of measure, extend from this class. The Bare minimum + * requirements for usage are defining all properties. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +abstract class AbstractUnit implements UnitInterface +{ + /** + * @var string $name The units' full name. + */ + protected $name; + + /** + * @var string $symbol The units' symbol notation (e.g., meter = m). + */ + protected $symbol; + + /** + * @var string $unit_of What is this unit measuring? Length, temperatutre, etc. + */ + protected $unitOf; + + /** + * @var UnitInterface $base The units' base unit. + */ + protected $base; + + /** + * @var float $units The amount of base units needed to make up 1 unit. + */ + protected $units; + + public function __construct () + { + $this->units = $this->calculate() ?? $this->units; + } + + /** + * Calculate the amount of required base units to make up 1 unit. + * + * @return null|float + */ + protected function calculate () : ?float + { + return null; + } + + public function setName (string $name) : UnitInterface + { + $this->name = $name; + return $this; + } + + public function getName () : string + { + return $this->name; + } + + public function setSymbol (string $symbol) : UnitInterface + { + $this->symbol = $symbol; + return $this; + } + + public function getSymbol () : string + { + return $this->symbol; + } + + public function setUnitOf (string $unitOf) : UnitInterface + { + $this->unitOf = $unitOf; + return $this; + } + + public function getUnitOf () : string + { + return $this->unitOf; + } + + public function setBase (UnitInterface $base) : UnitInterface + { + $this->base = $base; + return $this; + } + + public function getBase () : UnitInterface + { + return new $this->base; + } + + public function setUnits (float $units) : UnitInterface + { + $this->units = $units; + return $this; + } + + public function getUnits () : float + { + return $this->units; + } +} diff --git a/src/UnitConverter/Unit/UnitInterface.php b/src/UnitConverter/Unit/UnitInterface.php new file mode 100644 index 00000000..bd18c079 --- /dev/null +++ b/src/UnitConverter/Unit/UnitInterface.php @@ -0,0 +1,95 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit; + +/** + * Interface for the unit of measurement abstract class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +interface UnitInterface +{ + /** + * Sets the units full symantic name. + * + * @return UnitInterface + */ + public function setName (string $name) : UnitInterface; + + /** + * Returns the full symantic name of the unit. + * + * @return string + */ + public function getName () : string; + + /** + * Sets the symbol notation used for the unit. + * + * @return UnitInterface + */ + public function setSymbol (string $symbol) : UnitInterface; + + /** + * Returns the symbol notation of the unit. + * + * @return string + */ + public function getSymbol () : string; + + /** + * Sets the type of measurement that this unit is measuring. + * + * @return UnitInterface + */ + public function setUnitOf (string $unitOf) : UnitInterface; + + /** + * Returns the type of measurement that this unit is measuring. + * + * @return string + */ + public function getUnitOf () : string; + + /** + * Sets the unit class that this unit is based off of. + * + * @return UnitInterface + */ + public function setBase (UnitInterface $base) : UnitInterface; + + /** + * Returns the unit class that this unit is based off of. + * + * @return UnitInterface + */ + public function getBase () : UnitInterface; + + /** + * Sets the amount of base units required to make up 1 of the unit. + * + * @return UnitInterface + */ + public function setUnits (float $units) : UnitInterface; + + /** + * Returns the amount of base units required to make up 1 of the unit. + * + * @return float + */ + public function getUnits () : float; +} diff --git a/tests/unit/UnitConverter/Unit/AbstractUnit.spec.php b/tests/unit/UnitConverter/Unit/AbstractUnit.spec.php new file mode 100644 index 00000000..6f4b885f --- /dev/null +++ b/tests/unit/UnitConverter/Unit/AbstractUnit.spec.php @@ -0,0 +1,118 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Tests\Unit\Unit; + +use PHPUnit\Framework\TestCase; +use UnitConverter\Measure; +use UnitConverter\Unit\AbstractUnit; +use UnitConverter\Unit\Length\{ + Centimeter, + Inch, + Meter +}; + +/** + * @coversDefaultClass UnitConverter\UnitConverter + * @author Jordan Brauer + */ +class AbstractUnitSpec extends TestCase +{ + protected function setUp () + { + $this->unit = new class extends AbstractUnit + { + protected $name = "saiyan power"; + protected $symbol = "sP"; + protected $unitOf = Measure::LENGTH; + protected $base = Meter::class; + protected $units = 9001; + }; + } + + protected function tearDown () + { + unset($this->unit); + } + + /** + * @test + * @covers ::setName + * @covers ::getName + */ + public function assertGetNameSetNameMethodsCanReadAndWriteToUnitName () + { + $this->unit->setName("test set"); + $actual = $this->unit->getName(); + + $this->assertEquals("test set", $actual); + $this->assertInternalType("string", $actual); + } + + /** + * @test + * @covers ::setSymbol + * @covers ::getSymbol + */ + public function assertGetSymbolSetSymbolMethodsCanReadAndWriteToUnitSymbol () + { + $this->unit->setSymbol("tS"); + $actual = $this->unit->getSymbol(); + + $this->assertEquals("tS", $actual); + $this->assertInternalType("string", $actual); + } + + /** + * @test + * @covers ::setUnitOf + * @covers ::getUnitOf + */ + public function assertGetUnitOfSetUnitOfMethodsCanReadAndWriteToUnitUnitOf () + { + $this->unit->setUnitOf(Measure::ENERGY); + $actual = $this->unit->getUnitOf(); + + $this->assertEquals(Measure::ENERGY, $actual); + $this->assertInternalType("string", $actual); + } + + /** + * @test + * @covers ::setBase + * @covers ::getBase + */ + public function assertGetBaseSetBaseMethodsCanReadAndWriteToUnitBase () + { + $this->unit->setBase(new Inch); + $actual = $this->unit->getBase(); + + $this->assertInstanceOf(Inch::class, $actual); + $this->assertInternalType("object", $actual); + } + + /** + * @test + * @covers ::setUnits + * @covers ::getUnits + */ + public function assertGetUnitsSetUnitsMethodsCanReadAndWriteToUnitUnits () + { + $this->unit->setUnits(69); + $actual = $this->unit->getUnits(); + + $this->assertEquals(69, $actual); + $this->assertInternalType("float", $actual); + } +} From 73f9ee4e2a006a2ef9764a7943c437372bd531d7 Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:41:48 -0500 Subject: [PATCH 06/22] Feat: add a few default length units. meter is base for all length units --- src/UnitConverter/Unit/Length/Centimeter.php | 34 ++++++++++++++++++++ src/UnitConverter/Unit/Length/Inch.php | 34 ++++++++++++++++++++ src/UnitConverter/Unit/Length/Meter.php | 34 ++++++++++++++++++++ src/UnitConverter/Unit/Length/Milimeter.php | 34 ++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Centimeter.php create mode 100644 src/UnitConverter/Unit/Length/Inch.php create mode 100644 src/UnitConverter/Unit/Length/Meter.php create mode 100644 src/UnitConverter/Unit/Length/Milimeter.php diff --git a/src/UnitConverter/Unit/Length/Centimeter.php b/src/UnitConverter/Unit/Length/Centimeter.php new file mode 100644 index 00000000..9c10bdcd --- /dev/null +++ b/src/UnitConverter/Unit/Length/Centimeter.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Centimeter data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Centimeter extends LengthUnit +{ + protected $name = "centimeter"; + + protected $symbol = "cm"; + + protected $units = 0.01; +} diff --git a/src/UnitConverter/Unit/Length/Inch.php b/src/UnitConverter/Unit/Length/Inch.php new file mode 100644 index 00000000..adff4509 --- /dev/null +++ b/src/UnitConverter/Unit/Length/Inch.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Inch data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Inch extends LengthUnit +{ + protected $name = "inch"; + + protected $symbol = "in"; + + protected $units = 0.0254; +} diff --git a/src/UnitConverter/Unit/Length/Meter.php b/src/UnitConverter/Unit/Length/Meter.php new file mode 100644 index 00000000..c72bf1eb --- /dev/null +++ b/src/UnitConverter/Unit/Length/Meter.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Meter data class. All units of Length are based off of Meter. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Meter extends LengthUnit +{ + protected $name = "meter"; + + protected $symbol = "m"; + + protected $units = 1; +} diff --git a/src/UnitConverter/Unit/Length/Milimeter.php b/src/UnitConverter/Unit/Length/Milimeter.php new file mode 100644 index 00000000..7cf2dd54 --- /dev/null +++ b/src/UnitConverter/Unit/Length/Milimeter.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Milimeter data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Milimeter extends LengthUnit +{ + protected $name = "milimeter"; + + protected $symbol = "mm"; + + protected $units = 0.001; +} From 4e241a1b5b0787b88ea8f55da378c1818225c1de Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:43:31 -0500 Subject: [PATCH 07/22] Feat: unit registry class to store and retrieve supported units to/from --- src/UnitConverter/Registry/UnitRegistry.php | 165 ++++++++++++++ .../Registry/UnitRegistryInterface.php | 140 ++++++++++++ .../Registry/UnitRegistry.spec.php | 204 ++++++++++++++++++ 3 files changed, 509 insertions(+) create mode 100644 src/UnitConverter/Registry/UnitRegistry.php create mode 100644 src/UnitConverter/Registry/UnitRegistryInterface.php create mode 100644 tests/unit/UnitConverter/Registry/UnitRegistry.spec.php diff --git a/src/UnitConverter/Registry/UnitRegistry.php b/src/UnitConverter/Registry/UnitRegistry.php new file mode 100644 index 00000000..43bcc82d --- /dev/null +++ b/src/UnitConverter/Registry/UnitRegistry.php @@ -0,0 +1,165 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Registry; + +use UnitConverter\Measure; +use UnitConverter\Unit\UnitInterface; + +/** + * The unit converter registry object. This object is used + * to store and retrieve instances of the UnitInterface. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class UnitRegistry implements UnitRegistryInterface +{ + /** + * @var array $store A two-dimensional array containing available types of measuerment that each contain their available units of measure. + */ + protected $store = array(); + + /** + * Public constructor function for the unit registry. + * + * @param UnitInterface[] A one-dimensional array of UnitInterface objects to be registered upon construction. + * @return self + */ + public function __construct (array $units = array()) + { + $this->store = array( + Measure::LENGTH => array(), + Measure::AREA => array(), + Measure::VOLUME => array(), + Measure::WEIGHT => array(), + Measure::SPEED => array(), + Measure::ROTATION => array(), + Measure::TEMPERATURE => array(), + Measure::PRESSURE => array(), + Measure::TIME => array(), + Measure::ENERGY => array(), + ); + + if (count($units) > 0) + $this->registerUnits($units); + } + + public function isMeasurementRegistered (string $measurement) : bool + { + return array_key_exists($measurement, $this->store); + } + + public function isUnitRegistered (string $symbol) : bool + { + foreach ($this->store as $measurement => $units) { + if (array_key_exists($symbol, $units)) + return true; + } + + return false; + } + + public function loadUnit (string $symbol) : ?UnitInterface + { + foreach ($this->store as $measurement => $units) { + if (array_key_exists($symbol, $units)) + return $this->store[$measurement][$symbol]; + } + + return null; + } + + public function listMeasurements () : array + { + return array_keys($this->store); + } + + public function listUnits (string $measurement = null) : array + { + if (!$measurement) { + $registeredUnits = array(); + + foreach ($this->store as $measurements) { + foreach ($measurements as $unit) { + array_push($registeredUnits, $unit->getSymbol()); + } + } + + return $registeredUnits; + } + + return array_keys($this->store[$measurement]); + } + + public function registerMeasurement (string $measurement) : void + { + if (!$this->isMeasurementRegistered($measurement)) + $this->store[$measurement] = $measurement; + } + + public function registerMeasurements (array $measurements) : void + { + foreach ($measurements as $measurement) { + $this->registerMeasurement($measurement); + } + } + + public function registerUnit (UnitInterface $unit) : void + { + if (!$this->isMeasurementRegistered($unit->getUnitOf())) + throw new \ErrorException("Trying to register unit {$unit->getName()} to an unregisted measurement of {$unit->getUnitOf()}"); + + $this->store[$unit->getUnitOf()][$unit->getSymbol()] = $unit; + } + + public function registerUnits (array $units) : void + { + foreach ($units as $unit) { + $this->registerUnit($unit); + } + } + + public function unregisterMeasurement (string $measurement) : void + { + if (!$this->isMeasurementRegistered($measurement)) + throw new \ErrorException("Trying to unregister a nonexistent measurement type {$measurement}"); + + unset($this->store[$measurement]); + } + + public function unregisterMeasurements(array $measurements) : void + { + foreach ($measurements as $measurement) { + $this->unregisterMeasurement($measurement); + } + } + + public function unregisterUnit (string $symbol) : void + { + if ($this->isUnitRegistered($symbol) === false) + throw new \ErrorException("Trying to unregister a nonexistent unit {$symbol}"); + + $unit = $this->loadUnit($symbol); + unset($this->store[$unit->getUnitOf()][$symbol]); + } + + public function unregisterUnits (array $symbols) : void + { + foreach ($symbols as $unit) { + $this->unregisterUnit($unit); + } + } +} diff --git a/src/UnitConverter/Registry/UnitRegistryInterface.php b/src/UnitConverter/Registry/UnitRegistryInterface.php new file mode 100644 index 00000000..f9ff8fb2 --- /dev/null +++ b/src/UnitConverter/Registry/UnitRegistryInterface.php @@ -0,0 +1,140 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Registry; + +use UnitConverter\Unit\UnitInterface; + +/** + * The interface for the unit converter registry that stores units + * and types of measurement. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +interface UnitRegistryInterface +{ + /** + * Determine if a measurement type is registered to the unit registry or not. + * + * @param string $measurement The name of the measurment being checked, e.g. "length". + */ + public function isMeasurementRegistered (string $measurement) : bool; + + /** + * Determine if a specific unit of measure is registered to the unit registry or not. + * + * @param string $symbol The symbol notation of the unit being checked, e.g., "cm". + */ + public function isUnitRegistered (string $symbol) : bool; + + /** + * Fetch a unit from the unit registry for use elsewhere. + * + * @param string $symbol The symbol notation of the unit being loaded. + * @return null|UnitInterface + */ + public function loadUnit (string $symbol) : ?UnitInterface; + + /** + * Return a one-dimensional array of currently supported measurement types. + * + * @return array + */ + public function listMeasurements () : array; + + /** + * Return a one-dimensional array of currently supported units. Optionally + * pass a string equal to a type of measurement (e.g. "length") to return + * only units of the specifed type. + * + * @param string $measurement + */ + public function listUnits (string $measurement = null) : array; + + /** + * Register a single measurement to the unit registry by passing a + * string as the argument + * + * @param string $measurement + * @return void + */ + public function registerMeasurement (string $measurement) : void; + + /** + * Register many measurements to the unit registry by passing an + * array of measurement strings as the argument + * + * @param array $measurements + * @return void + */ + public function registerMeasurements (array $measurements) : void; + + /** + * Register a single unit to the unit registry by passing an + * instance of a UnitInterface as it's argument + * + * @param UnitInterface $unit + * @throws ErrorException This will be thrown when an attempted unit registration is made on an unexisting measurement. + * @return void + */ + public function registerUnit (UnitInterface $unit) : void; + + /** + * Register many units to the unit registry by passing an array of unit + * classes as it's argument. + * + * @param UnitInterface[] $units + * @return void + */ + public function registerUnits (array $units) : void; + + /** + * Unegister a single measurement from the unit registry. + * + * @NOTE: Invoking this method will also unregister all + * units belonging to the measurement that is being unregistered. + * + * @param string $symbol + * @throws ErrorException An error exception will be thrown if you attempt to unregister a non-existing measurement type. + * @return void + */ + public function unregisterMeasurement (string $symbol) : void; + + /** + * Unegister many units from the unit registry + * + * @param string[] $symbols + * @return void + */ + public function unregisterMeasurements (array $symbols) : void; + + /** + * Unegister a single unit from the unit registry + * + * @param string $symbol + * @throws ErrorException An error exception will be thrown if you attempt to unregister a non-existing unit of measure. + * @return void + */ + public function unregisterUnit (string $symbol) : void; + + /** + * Unegister many units from the unit registry + * + * @param string[] $symbols + * @return void + */ + public function unregisterUnits (array $symbols) : void; +} diff --git a/tests/unit/UnitConverter/Registry/UnitRegistry.spec.php b/tests/unit/UnitConverter/Registry/UnitRegistry.spec.php new file mode 100644 index 00000000..6f62746b --- /dev/null +++ b/tests/unit/UnitConverter/Registry/UnitRegistry.spec.php @@ -0,0 +1,204 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Tests\Unit\Registry; + +use PHPUnit\Framework\TestCase; +use UnitConverter\Registry\UnitRegistry; +use UnitConverter\Unit\UnitInterface; +use UnitConverter\Unit\AbstractUnit; +use UnitConverter\Unit\Length\{ + Centimeter, + Inch, + Meter, + Milimeter +}; + +/** + * @coversDefaultClass UnitConverter\UnitRegistry + * @author Jordan Brauer + */ +class UnitRegistrySpec extends TestCase +{ + protected function setUp () + { + $this->registry = new UnitRegistry(array( + new Centimeter, + new Inch, + )); + } + + protected function tearDown () + { + unset($this->registry); + } + + /** + * @test + * @covers ::isMeasurementRegistered + */ + public function assertMeasurementIsRegistered () + { + $this->assertTrue($this->registry->isMeasurementRegistered("length")); + $this->assertFalse($this->registry->isMeasurementRegistered("saiyanPower")); + } + + /** + * @test + * @covers ::isUnitRegistered + */ + public function assertUnitIsRegistered () + { + $this->assertTrue($this->registry->isUnitRegistered("cm")); + $this->assertFalse($this->registry->isUnitRegistered("yd")); + } + + /** + * @test + * @covers ::loadUnit + */ + public function assertUnitObjectIsLoaded () + { + $actual = $this->registry->loadUnit("cm"); + $expected = UnitInterface::class; + + $this->assertInstanceOf($expected, $actual); + } + + /** + * @test + * @covers ::listMeasurements + */ + public function assertListMeasurementsMethodReturnsArray () + { + $actual = $this->registry->listMeasurements(); + $expected = array( + "length", + "area", + "volume", + "weight", + "speed", + "rotation", + "temperature", + "pressure", + "time", + "energy", + ); + + $this->assertEquals($expected, $actual); + $this->assertInternalType("array", $actual); + $this->assertTrue((count($actual) > 0)); + } + + /** + * @test + * @covers ::listUnits + */ + public function assertListUnitsMethodReturnsArray () + { + $actual = $this->registry->listUnits(); + $expected = array( + "cm", + "in", + ); + + $this->assertEquals($expected, $actual); + $this->assertInternalType("array", $actual); + $this->assertTrue((count($actual) > 0)); + } + + /** + * @test + * @covers ::registerMeasurement + * @covers ::registerMeasurements + * @uses ::isMeasurementRegistered + */ + public function assertRegisterMeasurementMethodsAddItemsToUnitRegistry () + { + $this->assertFalse($this->registry->isMeasurementRegistered("data")); + $this->assertFalse($this->registry->isMeasurementRegistered("saiyanPower")); + $this->assertFalse($this->registry->isMeasurementRegistered("funniness")); + + $this->registry->registerMeasurement("data"); + $this->registry->registerMeasurements(["saiyanPower", "funniness"]); + + $this->assertTrue($this->registry->isMeasurementRegistered("data")); + $this->assertTrue($this->registry->isMeasurementRegistered("saiyanPower")); + $this->assertTrue($this->registry->isMeasurementRegistered("funniness")); + } + + /** + * @test + * @covers ::registerUnit + * @covers ::registerUnits + * @uses ::isUnitRegistered + */ + public function assertRegisterUnitMethodsAddItemsToUnitRegistry () + { + $this->assertFalse($this->registry->isUnitRegistered("sP")); + $this->assertFalse($this->registry->isUnitRegistered("m")); + $this->assertFalse($this->registry->isUnitRegistered("mm")); + + $this->registry->registerUnit(new class extends AbstractUnit { + protected $name = "saiyanPower"; + protected $symbol = "sP"; + protected $unitOf = "energy"; + protected $base = self::class; + protected $units = 9001; + }); + $this->registry->registerUnits([new Meter, new Milimeter]); + + $this->assertTrue($this->registry->isUnitRegistered("sP")); + $this->assertTrue($this->registry->isUnitRegistered("m")); + $this->assertTrue($this->registry->isUnitRegistered("mm")); + } + + /** + * @test + * @covers ::unregisterMeasurement + * @covers ::unregisterMeasurements + * @uses ::isMeasurementRegistered + */ + public function assertUnregisterMeasurementMethodsRemoveItemsFromUnitRegistry () + { + $this->assertTrue($this->registry->isMeasurementRegistered("length")); + $this->assertTrue($this->registry->isMeasurementRegistered("weight")); + $this->assertTrue($this->registry->isMeasurementRegistered("volume")); + + $this->registry->unregisterMeasurement("length"); + $this->registry->unregisterMeasurements(array("weight", "volume")); + + $this->assertFalse($this->registry->isMeasurementRegistered("length")); + $this->assertFalse($this->registry->isMeasurementRegistered("weight")); + $this->assertFalse($this->registry->isMeasurementRegistered("volume")); + } + + /** + * @test + * @covers ::unregisterUnit + * @covers ::unregisterUnits + * @uses ::isUnitRegistered + */ + public function assertUnregisterUnitMethodsRemoveItemsFromUnitRegistry () + { + $this->assertTrue($this->registry->isUnitRegistered("cm")); + $this->assertTrue($this->registry->isUnitRegistered("in")); + + $this->registry->unregisterUnit("cm"); + $this->registry->unregisterUnits(array("in")); + + $this->assertFalse($this->registry->isUnitRegistered("cm")); + $this->assertFalse($this->registry->isUnitRegistered("in")); + } +} From 3a84d07aab1f808a6f82a515228c1903d06e980e Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:56:32 -0500 Subject: [PATCH 08/22] Feat: add unit converter class to actually convert values from/to units --- src/UnitConverter/UnitConverter.php | 129 ++++++++++++++++++ src/UnitConverter/UnitConverterInterface.php | 67 +++++++++ .../unit/UnitConverter/UnitConverter.spec.php | 65 +++++++++ 3 files changed, 261 insertions(+) create mode 100644 src/UnitConverter/UnitConverter.php create mode 100644 src/UnitConverter/UnitConverterInterface.php create mode 100644 tests/unit/UnitConverter/UnitConverter.spec.php diff --git a/src/UnitConverter/UnitConverter.php b/src/UnitConverter/UnitConverter.php new file mode 100644 index 00000000..95d5eada --- /dev/null +++ b/src/UnitConverter/UnitConverter.php @@ -0,0 +1,129 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter; + +use UnitConverter\Measure; +use UnitConverter\Unit\UnitInterface; +use UnitConverter\Registry\UnitRegistryInterface; + +/** + * The actual unit converter object. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class UnitConverter implements UnitConverterInterface +{ + /** + * @var UnitRegistryInterface The registry that the unit converter accesses available units from. + */ + protected $registry; + + /** + * @var float $convert The value being converted. + */ + protected $convert; + + /** + * @var string $from The unit of measure being converted **from**. + */ + protected $from; + + /** + * @var string $to The unit of measure being converted **to**. + */ + protected $to; + + /** + * Public constructor function for the UnitConverter class. + * + * @param UnitInterface[] A two-dimensional array of UnitInterface objects. + * @return self + */ + public function __construct (UnitRegistryInterface $registry = null) + { + $this->setRegistry($registry); + } + + /** + * Determine whether or not the converter has an active registry. + * + * @internal + * @return bool + */ + protected function registryExists () : bool + { + if ($this->registry instanceof UnitRegistryInterface) + return true; + + return false; + } + + /** + * Load a unit from the unit converter registry. + * + * @internal + * @uses UnitConverter\UnitRegistry::loadUnit + * @throws ErrorException An error exceptino will be thrown if no registry exits + * @return UnitInterface + */ + protected function loadUnit(string $symbol) : UnitInterface + { + if ($this->registryExists() === false) + throw new \ErrorException("No unit registry was found to load units from."); + + return $this->registry->loadUnit($symbol); + } + + /** + * Calculate the conversion from one unit to another. + * + * @internal + * @param float $value The initial value being converted. + * @param UnitInterface $from The unit of measure being converted **from**. + * @param UnitInterface $to The unit of measure being converted **to**. + * @return float + */ + protected function calculate (float $value, UnitInterface $from, UnitInterface $to): float + { + return ($value * $from->getUnits()) / $to->getUnits(); + } + + + public function setRegistry ($registry) : UnitConverterInterface + { + $this->registry = $registry; + return $this; + } + + public function convert (float $value) + { + $this->convert = $value; + return $this; + } + + public function from (string $unit) + { + $this->from = $this->loadUnit($unit); + return $this; + } + + public function to (string $unit) + { + $this->to = $this->loadUnit($unit); + return $this->calculate($this->convert, $this->from, $this->to); + } +} diff --git a/src/UnitConverter/UnitConverterInterface.php b/src/UnitConverter/UnitConverterInterface.php new file mode 100644 index 00000000..4a08cb8c --- /dev/null +++ b/src/UnitConverter/UnitConverterInterface.php @@ -0,0 +1,67 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter; + +use UnitConverter\Unit\UnitInterface; + +/** + * The interface for any and all unit converter classes. If you want + * a custom converter, implement this interface and you are good to + * go! + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +interface UnitConverterInterface +{ + /** + * Set the unit converter registry for storing units of measure to convert values with. + * + * @api + * @param UnitRegistryInterface $registry An instance of UnitRegistry. + */ + public function setRegistry ($registry) : UnitConverterInterface; + + /** + * Set the unit converters' value to be converted. This method is the first + * method to be called in the chain of conversion methods. + * + * @api + * @example $converter->convert(1)->from("in")->to("cm"); + * @param float $value The numerical value being converted. + */ + public function convert (float $value); + + /** + * Set the unit converters' unit to be converted **from**. This method is the + * second to be called in the chain of conversion methods. + * + * @api + * @example $converter->convert(1)->from("in")->to("cm"); + * @param string $unit The unit being conerted **from**. The unit must first be registered to the UnitRegistry. + */ + public function from (string $unit); + + /** + * Set the unit converters' unit to be converted **to**. This method is the + * third to be called in the chain of conversion methods. + * + * @api + * @example $converter->convert(1)->from("in")->to("cm"); + * @param string $unit The unit being converted **to**. The unit must first be registered to the UnitRegistry. + */ + public function to (string $unit); +} diff --git a/tests/unit/UnitConverter/UnitConverter.spec.php b/tests/unit/UnitConverter/UnitConverter.spec.php new file mode 100644 index 00000000..ff210301 --- /dev/null +++ b/tests/unit/UnitConverter/UnitConverter.spec.php @@ -0,0 +1,65 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Tests\Unit; + +use PHPUnit\Framework\TestCase; +use UnitConverter\UnitConverter; +use UnitConverter\Registry\UnitRegistry; +use UnitConverter\Unit\Length\{ + Centimeter, + Inch +}; + +/** + * @coversDefaultClass UnitConverter\UnitConverter + * @author Jordan Brauer + */ +class UnitConverterSpec extends TestCase +{ + protected function setUp () + { + $this->converter = new UnitConverter( + new UnitRegistry(array( + new Centimeter, + new Inch, + )) + ); + } + + protected function tearDown () + { + unset($this->converter); + } + + /** + * @test + * @covers ::convert + * @covers ::from + * @covers ::to + * @covers ::calculate + */ + public function assertCalculateMethodReturnsCorrectCalculation () + { + $expected = 2.54; # = (1 * 0.0254) / 0.01 + $actual = $this->converter + ->convert(1) + ->from("in") + ->to("cm") + ; + + $this->assertEquals($expected, $actual); + $this->assertInternalType("float", $actual); + } +} From 92e48ef107ef0f525460456cefbdbf11fb9f74f1 Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:57:01 -0500 Subject: [PATCH 09/22] Update: add examples file --- examples.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples.php diff --git a/examples.php b/examples.php new file mode 100644 index 00000000..f9e1f8b5 --- /dev/null +++ b/examples.php @@ -0,0 +1,48 @@ +registerUnit(new class extends AbstractUnit { + protected $name = "testtt"; + protected $symbol = "Tst"; + protected $unitOf = Measure::VOLUME; + protected $base = self::class; + protected $units = 1; +}); + +# Converting Units +# ================ + +$conversion = +$converter + ->convert(1) + ->from("in") + ->to("cm") + ; From e85984c10ecb07d0fd35fd4a285b8f198995e66e Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:57:52 -0500 Subject: [PATCH 10/22] Feat: support conversion to Astronomical Units --- .../Unit/Length/AstronomicalUnit.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/AstronomicalUnit.php diff --git a/src/UnitConverter/Unit/Length/AstronomicalUnit.php b/src/UnitConverter/Unit/Length/AstronomicalUnit.php new file mode 100644 index 00000000..b414db30 --- /dev/null +++ b/src/UnitConverter/Unit/Length/AstronomicalUnit.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * AstronomicalUnit data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class AstronomicalUnit extends LengthUnit +{ + protected $name = "astronomical unit"; + + protected $symbol = "au"; + + protected $units = 149597870700; +} From 3fc8d057b3cad6eec0f5f46175d245cf590a058b Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:58:00 -0500 Subject: [PATCH 11/22] Feat: support conversion to Decimeters --- src/UnitConverter/Unit/Length/Decimeter.php | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Decimeter.php diff --git a/src/UnitConverter/Unit/Length/Decimeter.php b/src/UnitConverter/Unit/Length/Decimeter.php new file mode 100644 index 00000000..eb222f09 --- /dev/null +++ b/src/UnitConverter/Unit/Length/Decimeter.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Decimeter data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Decimeter extends LengthUnit +{ + protected $name = "decimeter"; + + protected $symbol = "dm"; + + protected $units = 0.1; +} From fa2dc156bf97126eab9a71e18b781664fd1fa2f2 Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:58:11 -0500 Subject: [PATCH 12/22] Feat: support conversion to Feet --- src/UnitConverter/Unit/Length/Foot.php | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Foot.php diff --git a/src/UnitConverter/Unit/Length/Foot.php b/src/UnitConverter/Unit/Length/Foot.php new file mode 100644 index 00000000..9cc88f63 --- /dev/null +++ b/src/UnitConverter/Unit/Length/Foot.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Foot data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Foot extends LengthUnit +{ + protected $name = "foot"; + + protected $symbol = "ft"; + + protected $units = 0.3048; +} From 557d426b95e94fc2eb460b1311ebf23877f163d4 Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:58:18 -0500 Subject: [PATCH 13/22] Feat: support conversion to Hands --- src/UnitConverter/Unit/Length/Hand.php | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Hand.php diff --git a/src/UnitConverter/Unit/Length/Hand.php b/src/UnitConverter/Unit/Length/Hand.php new file mode 100644 index 00000000..b77c5426 --- /dev/null +++ b/src/UnitConverter/Unit/Length/Hand.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Hand data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Hand extends LengthUnit +{ + protected $name = "hand"; + + protected $symbol = "h"; + + protected $units = 0.1016; +} From aec822907747e8f59a291f13a085e92092b2f15a Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:58:24 -0500 Subject: [PATCH 14/22] Feat: support conversion to Kilometers --- src/UnitConverter/Unit/Length/Kilometer.php | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Kilometer.php diff --git a/src/UnitConverter/Unit/Length/Kilometer.php b/src/UnitConverter/Unit/Length/Kilometer.php new file mode 100644 index 00000000..caafb1b2 --- /dev/null +++ b/src/UnitConverter/Unit/Length/Kilometer.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Kilometer data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Kilometer extends LengthUnit +{ + protected $name = "kilometer"; + + protected $symbol = "km"; + + protected $units = 1000; +} From cf453c6c99ea431ab2cf3b049ea6c5268d78d574 Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:58:40 -0500 Subject: [PATCH 15/22] Feat: support conversion to Lightyears --- src/UnitConverter/Unit/Length/Lightyear.php | 36 +++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Lightyear.php diff --git a/src/UnitConverter/Unit/Length/Lightyear.php b/src/UnitConverter/Unit/Length/Lightyear.php new file mode 100644 index 00000000..dffa4705 --- /dev/null +++ b/src/UnitConverter/Unit/Length/Lightyear.php @@ -0,0 +1,36 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Lightyear data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Lightyear extends LengthUnit +{ + protected $name = "lightyear"; + + protected $symbol = "ly"; + + # Metric (SI) Units = 9.4607 × 1015 m + # – OR – 9.4607 Pm + protected $units = 9460730472580800; +} From 0a1295350acd55ee2a59bef746a36ee6674c24ab Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:58:45 -0500 Subject: [PATCH 16/22] Feat: support conversion to Micrometers --- src/UnitConverter/Unit/Length/Micrometer.php | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Micrometer.php diff --git a/src/UnitConverter/Unit/Length/Micrometer.php b/src/UnitConverter/Unit/Length/Micrometer.php new file mode 100644 index 00000000..9e7680fa --- /dev/null +++ b/src/UnitConverter/Unit/Length/Micrometer.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Micrometer data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Micrometer extends LengthUnit +{ + protected $name = "micrometer"; + + protected $symbol = "µm"; + + protected $units = 0.000001; # 1.0E-6 +} From b377122684f9b1717ce535b712ed63e438042f33 Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:58:51 -0500 Subject: [PATCH 17/22] Feat: support conversion to Miles --- src/UnitConverter/Unit/Length/Mile.php | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Mile.php diff --git a/src/UnitConverter/Unit/Length/Mile.php b/src/UnitConverter/Unit/Length/Mile.php new file mode 100644 index 00000000..b7f9a1f2 --- /dev/null +++ b/src/UnitConverter/Unit/Length/Mile.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Mile data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Mile extends LengthUnit +{ + protected $name = "mile"; + + protected $symbol = "mi"; + + protected $units = 1609.344; +} From c9499c872bf92f626dfca723d562c6bab3cf5831 Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:58:57 -0500 Subject: [PATCH 18/22] Feat: support conversion to Nanometers --- src/UnitConverter/Unit/Length/Nanometer.php | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Nanometer.php diff --git a/src/UnitConverter/Unit/Length/Nanometer.php b/src/UnitConverter/Unit/Length/Nanometer.php new file mode 100644 index 00000000..ab7c85dd --- /dev/null +++ b/src/UnitConverter/Unit/Length/Nanometer.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Nanometer data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Nanometer extends LengthUnit +{ + protected $name = "nanometer"; + + protected $symbol = "nm"; + + protected $units = 0.000000001; # 1.0E-9 +} From 9dd7546f083c179ab76d74b857fe1af981ef1d97 Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:59:02 -0500 Subject: [PATCH 19/22] Feat: support conversion to Parsecs --- src/UnitConverter/Unit/Length/Parsec.php | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Parsec.php diff --git a/src/UnitConverter/Unit/Length/Parsec.php b/src/UnitConverter/Unit/Length/Parsec.php new file mode 100644 index 00000000..cc58c6bc --- /dev/null +++ b/src/UnitConverter/Unit/Length/Parsec.php @@ -0,0 +1,36 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Parsec data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Parsec extends LengthUnit +{ + protected $name = "parsec"; + + protected $symbol = "pc"; + + # Metric (SI) Units = 3.0857×1016 m + # – OR – ~31 petametres + protected $units = 30856775814913672.789139379577965; # 3.08567758149E+16 +} From 5ca7f5cdaf53e4675f532351210a5b400ae59604 Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:59:09 -0500 Subject: [PATCH 20/22] Feat: support conversion to Picometers --- src/UnitConverter/Unit/Length/Picometer.php | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Picometer.php diff --git a/src/UnitConverter/Unit/Length/Picometer.php b/src/UnitConverter/Unit/Length/Picometer.php new file mode 100644 index 00000000..f739dc4f --- /dev/null +++ b/src/UnitConverter/Unit/Length/Picometer.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Picometer data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Picometer extends LengthUnit +{ + protected $name = "picometer"; + + protected $symbol = "pm"; + + protected $units = 0.000000000001; # 1.0E-12 +} From d1196f67d80dd8ea6f51c725a45f9b87556a5140 Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 00:59:13 -0500 Subject: [PATCH 21/22] Feat: support conversion to Yards --- src/UnitConverter/Unit/Length/Yard.php | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/Yard.php diff --git a/src/UnitConverter/Unit/Length/Yard.php b/src/UnitConverter/Unit/Length/Yard.php new file mode 100644 index 00000000..603783a5 --- /dev/null +++ b/src/UnitConverter/Unit/Length/Yard.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Yard data class. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class Yard extends LengthUnit +{ + protected $name = "yard"; + + protected $symbol = "yd"; + + protected $units = 0.9144; +} From 75c53423f6a7d040653f9107a07519ebb04f9b5c Mon Sep 17 00:00:00 2001 From: jordanbrauer Date: Mon, 9 Oct 2017 01:01:08 -0500 Subject: [PATCH 22/22] Fix: included missing base LengthUnit file --- src/UnitConverter/Unit/Length/LengthUnit.php | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/UnitConverter/Unit/Length/LengthUnit.php diff --git a/src/UnitConverter/Unit/Length/LengthUnit.php b/src/UnitConverter/Unit/Length/LengthUnit.php new file mode 100644 index 00000000..b3297820 --- /dev/null +++ b/src/UnitConverter/Unit/Length/LengthUnit.php @@ -0,0 +1,34 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace UnitConverter\Unit\Length; + +use UnitConverter\Measure; +use UnitConverter\Unit\{ AbstractUnit, UnitInterface }; + +/** + * Length unit base data class. Any new length units should + * extend this class and implement their name, symbol and units; + * overriding the $unitOf and $base properties only if necessary. + * + * @version 1.0.0 + * @since 1.0.0 + * @author Jordan Brauer + */ +class LengthUnit extends AbstractUnit +{ + protected $unitOf = Measure::LENGTH; + + protected $base = Meter::class; +}