-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: unit registry class to store and retrieve supported units to/from
- Loading branch information
1 parent
73f9ee4
commit 4e241a1
Showing
3 changed files
with
509 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the jordanbrauer/unit-converter PHP package. | ||
* | ||
* @copyright 2017 Jordan Brauer <[email protected]> | ||
* @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 <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the jordanbrauer/unit-converter PHP package. | ||
* | ||
* @copyright 2017 Jordan Brauer <[email protected]> | ||
* @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 <[email protected]> | ||
*/ | ||
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; | ||
} |
Oops, something went wrong.