Skip to content

Commit

Permalink
Merge pull request #1 from brewerwall/feature/unit-service
Browse files Browse the repository at this point in the history
New UnitzService
  • Loading branch information
griffithben authored Aug 6, 2023
2 parents dede5fc + 5fd0b26 commit 5aff67a
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 32 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/feature.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on:
push:
branches:
- "feature/**"
permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: composer run-script test
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ that helps convert a unit to all types and not specifically addressing specific
composer require brewerwall/unitz
```

### Use
### Single Type Use

```php
// Create a new Gravity Object
Expand All @@ -34,6 +34,26 @@ $plato = $gravity->getValue();

```

### Service Provider Use

You can inject the UnitzService class into your application. Setting the user's preferences as an argument in the
constructor
will allow you to use the `getValue()` method to get the user's preferred unit of measure.

```php
// Instantiate a new UnitzService in a Service Provider Pattern
$unitService = new UnitzService(preferences: ['Temperature' => 'Celsius']);

// Dependency injection of UnitzService within the application
$temperature = $unitService->makeTemperature(fahrenheit: 72);

// Output of getValue() based on the user's preferences
$temperature->getValue(); // 22.222222222222

// Output of getValue() based on the user's preferences with rounding
$temperature->getValue(1); // 22.2
````

### Available Units

| Unit | Types |
Expand Down
22 changes: 3 additions & 19 deletions src/AbstractUnit.php → src/AbstractUnitz.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,8 @@
use ReflectionClass;
use RuntimeException;

abstract class AbstractUnit
abstract class AbstractUnitz extends BaseUnitz
{
public const DEFAULT_PREFERENCES = [
'Gravity' => 'Plato',
'Temperature' => 'Fahrenheit',
'Volume' => 'Gallon',
'Pressure' => 'Psi',
'Weight' => 'Pound',
'Color' => 'Srm',
];

private array $preferences;

public function __construct(array $preferences = [])
{
$this->preferences = array_merge($preferences, self::DEFAULT_PREFERENCES);
}

/**
* @param int|null $round
* @return float
Expand Down Expand Up @@ -64,8 +48,8 @@ private function makeGatherMethod(): string
$reflection = new ReflectionClass($this);
$classname = $reflection->getShortName();

if (array_key_exists($classname, $this->preferences)) {
return 'get' . $this->preferences[$classname];
if (array_key_exists($classname, $this->getPreferences())) {
return 'get' . $this->getPreferences()[$classname];
}

throw new RuntimeException("Preference for '$classname' has not been set.");
Expand Down
27 changes: 27 additions & 0 deletions src/BaseUnitz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Unitz;

class BaseUnitz
{
public const DEFAULT_PREFERENCES = [
'Gravity' => 'Plato',
'Temperature' => 'Fahrenheit',
'Volume' => 'Gallon',
'Pressure' => 'Psi',
'Weight' => 'Pound',
'Color' => 'Srm',
];

private array $preferences;

public function __construct(array $preferences = [])
{
$this->preferences = array_merge(self::DEFAULT_PREFERENCES, $preferences);
}

public function getPreferences(): array
{
return $this->preferences;
}
}
6 changes: 4 additions & 2 deletions src/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Unitz;

class Color extends AbstractUnit
use InvalidArgumentException;

class Color extends AbstractUnitz
{
private float $srm;
private float $ebc;
Expand All @@ -15,7 +17,7 @@ public function __construct(
array $preferences = []
) {
if (!$this->hasOnlyOneValue([$srm, $ebc, $lovibond])) {
throw new \InvalidArgumentException('Only one Color type can be set at a time.');
throw new InvalidArgumentException('Only one Color type can be set at a time.');
}

parent::__construct($preferences);
Expand Down
6 changes: 4 additions & 2 deletions src/Gravity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Unitz;

class Gravity extends AbstractUnit
use InvalidArgumentException;

class Gravity extends AbstractUnitz
{
private float $plato;

Expand All @@ -17,7 +19,7 @@ public function __construct(
array $preferences = []
) {
if (!$this->hasOnlyOneValue([$plato, $specificGravity, $brix])) {
throw new \InvalidArgumentException('Only one Gravity type can be set at a time.');
throw new InvalidArgumentException('Only one Gravity type can be set at a time.');
}

parent::__construct($preferences);
Expand Down
6 changes: 4 additions & 2 deletions src/Pressure.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace Unitz;

class Pressure extends AbstractUnit
use InvalidArgumentException;

class Pressure extends AbstractUnitz
{
private float $bar;
private float $psi;

public function __construct(?float $bar = null, ?float $psi = null, array $preferences = [])
{
if (!$this->hasOnlyOneValue([$bar, $psi])) {
throw new \InvalidArgumentException('Only one Pressure type can be set at a time.');
throw new InvalidArgumentException('Only one Pressure type can be set at a time.');
}

parent::__construct($preferences);
Expand Down
6 changes: 4 additions & 2 deletions src/Temperature.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace Unitz;

class Temperature extends AbstractUnit
use InvalidArgumentException;

class Temperature extends AbstractUnitz
{
private float $fahrenheit;
private float $celsius;

public function __construct(float $fahrenheit = null, float $celsius = null, array $preferences = [])
{
if (!$this->hasOnlyOneValue([$fahrenheit, $celsius])) {
throw new \InvalidArgumentException('Only one Temperature type can be set at a time.');
throw new InvalidArgumentException('Only one Temperature type can be set at a time.');
}

parent::__construct($preferences);
Expand Down
84 changes: 84 additions & 0 deletions src/UnitzService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Unitz;

final class UnitzService extends BaseUnitz
{
/**
* @param float|null $srm
* @param float|null $ebc
* @param float|null $lovibond
* @return \Unitz\Color
*/
public function makeColor(?float $srm = null, ?float $ebc = null, ?float $lovibond = null): Color
{
return new Color($srm, $ebc, $lovibond, $this->getPreferences());
}

/**
* @param float|null $plato
* @param float|null $specificGravity
* @param float|null $brix
* @return \Unitz\Gravity
*/
public function makeGravity(?float $plato = null, ?float $specificGravity = null, ?float $brix = null): Gravity
{
return new Gravity($plato, $specificGravity, $brix, $this->getPreferences());
}

/**
* @param float|null $bar
* @param float|null $psi
* @return \Unitz\Pressure
*/
public function makePressure(?float $bar = null, ?float $psi = null): Pressure
{
return new Pressure($bar, $psi, $this->getPreferences());
}

/**
* @param float|null $fahrenheit
* * @param float|null $celsius
* @return \Unitz\Temperature
*/
public function makeTemperature(?float $fahrenheit = null, ?float $celsius = null): Temperature
{
return new Temperature($fahrenheit, $celsius, $this->getPreferences());
}

/**
* @param float|null $ounce
* @param float|null $gallon
* @param float|null $barrel
* @param float|null $milliliter
* @param float|null $liter
* @param float|null $hectoliter
* @return \Unitz\Volume
*/
public function makeVolume(
float $ounce = null,
float $gallon = null,
float $barrel = null,
float $milliliter = null,
float $liter = null,
float $hectoliter = null
): Volume {
return new Volume($ounce, $gallon, $barrel, $milliliter, $liter, $hectoliter, $this->getPreferences());
}

/**
* @param float|null $ounce
* @param float|null $pound
* @param float|null $gram
* @param float|null $kilogram
* @return \Unitz\Weight
*/
public function makeWeight(
?float $ounce = null,
?float $pound = null,
?float $gram = null,
?float $kilogram = null,
): Weight {
return new Weight($ounce, $pound, $gram, $kilogram, $this->getPreferences());
}
}
6 changes: 4 additions & 2 deletions src/Volume.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Unitz;

class Volume extends AbstractUnit
use InvalidArgumentException;

class Volume extends AbstractUnitz
{
private float $ounce;
private float $gallon;
Expand All @@ -21,7 +23,7 @@ public function __construct(
array $preferences = []
) {
if (!$this->hasOnlyOneValue([$ounce, $gallon, $barrel, $milliliter, $liter, $hectoliter])) {
throw new \InvalidArgumentException('Only one Volume type can be set at a time.');
throw new InvalidArgumentException('Only one Volume type can be set at a time.');
}

parent::__construct($preferences);
Expand Down
6 changes: 4 additions & 2 deletions src/Weight.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Unitz;

class Weight extends AbstractUnit
use InvalidArgumentException;

class Weight extends AbstractUnitz
{
private float $ounce;
private float $pound;
Expand All @@ -17,7 +19,7 @@ public function __construct(
array $preferences = []
) {
if (!$this->hasOnlyOneValue([$ounce, $pound, $gram, $kilogram])) {
throw new \InvalidArgumentException('Only one Weight type can be set at a time.');
throw new InvalidArgumentException('Only one Weight type can be set at a time.');
}

parent::__construct($preferences);
Expand Down
Loading

0 comments on commit 5aff67a

Please sign in to comment.