Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert Measure class to enum #181

Open
wants to merge 1 commit into
base: php8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions src/ConverterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use UnitConverter\Calculator\BinaryCalculator;
use UnitConverter\Calculator\CalculatorInterface;
use UnitConverter\Calculator\SimpleCalculator;
use UnitConverter\Exception\BadMeasurement;
use UnitConverter\Registry\UnitRegistry;
use UnitConverter\Unit\UnitInterface;

class ConverterBuilder
{
Expand All @@ -34,7 +34,7 @@ class ConverterBuilder
*/
public function __construct()
{
$this->defaultMeasurements = Measure::getDefaultMeasurements();
$this->defaultMeasurements = Measure::cases();
}

/**
Expand Down Expand Up @@ -69,10 +69,10 @@ public function addDefaultRegistry()
* type that are provided with this package.
*
* @api
* @param string $measurement The type of measurement to seed units for.
* @param Measure $measurement The type of measurement to seed units for.
* @return self
*/
public function addRegistryFor(string $measurement)
public function addRegistryFor(Measure $measurement)
{
$this->registry = new UnitRegistry($this->instantiateAllUnitsFor($measurement));

Expand Down Expand Up @@ -123,13 +123,14 @@ public function build()
* Retrieve instances of all units.
*
* @internal
* @return array
* @return UnitInterface[]
*/
private function instantiateAllUnits()
{
$measurements = array_map(function (string $measurement) {
return $this->instantiateAllUnitsFor($measurement);
}, $this->defaultMeasurements);
$measurements = array_map(
fn (Measure $measurement): array => $this->instantiateAllUnitsFor($measurement),
$this->defaultMeasurements,
);

return array_merge(...$measurements);
}
Expand All @@ -138,17 +139,13 @@ private function instantiateAllUnits()
* Retrieve instances of all units for a given measurement type.
*
* @internal
* @param string $measurement
* @return array
* @return UnitInterface[]
*/
private function instantiateAllUnitsFor(string $measurement): array
private function instantiateAllUnitsFor(Measure $measurement): array
{
if (!in_array($measurement, $this->defaultMeasurements)) {
throw BadMeasurement::unknown($measurement);
}

return array_map(function ($class) {
return new $class();
}, Measure::getDefaultUnitsFor($measurement));
return array_map(
static fn (string $fqcn): UnitInterface => new $fqcn(),
$measurement->units()
);
}
}
Loading