-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from brewerwall/feature/rates
Start of Rates structure with Flow
- Loading branch information
Showing
20 changed files
with
389 additions
and
61 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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,130 @@ | ||
<?php | ||
|
||
namespace Unitz\Rate; | ||
|
||
use Doctrine\Inflector\Inflector; | ||
use Doctrine\Inflector\InflectorFactory; | ||
use ReflectionClass; | ||
use RuntimeException; | ||
use Unitz\BaseUnitz; | ||
|
||
abstract class AbstractRate | ||
{ | ||
private const UNIT_NAME_NUMERATOR = 0; | ||
private const UNIT_NAME_DENOMINATOR = 1; | ||
|
||
private Inflector $inflector; | ||
private BaseUnitz $numerator; | ||
private BaseUnitz $denominator; | ||
|
||
public function __construct() | ||
{ | ||
$this->inflector = InflectorFactory::create()->build(); | ||
} | ||
|
||
/** | ||
* @param \Unitz\BaseUnitz $numerator | ||
* @return void | ||
*/ | ||
protected function setNumerator(BaseUnitz $numerator): void | ||
{ | ||
$this->numerator = $numerator; | ||
} | ||
|
||
/** | ||
* @param \Unitz\BaseUnitz $denominator | ||
* @return void | ||
*/ | ||
protected function setDenominator(BaseUnitz $denominator): void | ||
{ | ||
$this->denominator = $denominator; | ||
} | ||
|
||
/** | ||
* @param string $methodName | ||
* @param array $arguments | ||
* @return \Unitz\BaseUnitz | ||
* @throws \RuntimeException | ||
*/ | ||
public function __call(string $methodName, array $arguments): BaseUnitz | ||
{ | ||
$unitNames = $this->getUnitNames($this->checkAndRemoveGetPrefix($methodName)); | ||
|
||
$numeratorName = $this->processUnitName($unitNames[self::UNIT_NAME_NUMERATOR]); | ||
$denominatorName = $this->processUnitName($unitNames[self::UNIT_NAME_DENOMINATOR]); | ||
|
||
$numerator = $this->getUnitValue($this->numerator, $numeratorName); | ||
$denominator = $this->getUnitValue($this->denominator, $denominatorName); | ||
|
||
$numeratorReflection = new ReflectionClass($this->numerator); | ||
$unitsClassName = $numeratorReflection->getName(); | ||
$unitsClassShortName = $numeratorReflection->getShortName(); | ||
|
||
return (new $unitsClassName(preferences: [$unitsClassShortName => $numeratorName]))->{"set$numeratorName"}( | ||
$numerator / $denominator | ||
); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return string | ||
* @throws \RuntimeException | ||
*/ | ||
private function checkAndRemoveGetPrefix(string $name): string | ||
{ | ||
$getPosition = strpos($name, 'get'); | ||
if ($getPosition === false || $getPosition !== 0) { | ||
throw new RuntimeException('All methods must start with "get"'); | ||
} | ||
|
||
return str_replace('get', '', $name); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return array | ||
* @throws \RuntimeException | ||
*/ | ||
private function getUnitNames(string $name): array | ||
{ | ||
$perPosition = strpos($name, 'Per'); | ||
if ($perPosition === false) { | ||
throw new RuntimeException('All methods must contain "Per" between the two units'); | ||
} | ||
|
||
$units = array_filter(explode('Per', $name)); | ||
if (count($units) !== 2) { | ||
throw new RuntimeException( | ||
'All methods must contain two units separated by "Per", get' . $name . '() has ' . count( | ||
$units | ||
) | ||
); | ||
} | ||
|
||
return $units; | ||
} | ||
|
||
/** | ||
* @param string $unitName | ||
* @return string | ||
*/ | ||
private function processUnitName(string $unitName): string | ||
{ | ||
return $this->inflector->singularize(ucfirst(strtolower($unitName))); | ||
} | ||
|
||
/** | ||
* @param \Unitz\BaseUnitz $unit | ||
* @param string $unitName | ||
* @return float | ||
* @throws \RuntimeException | ||
*/ | ||
private function getUnitValue(BaseUnitz $unit, string $unitName): float | ||
{ | ||
if (!method_exists($unit, "get$unitName")) { | ||
throw new RuntimeException("Method get$unitName does not exist on " . get_class($unit)); | ||
} | ||
|
||
return $unit->{"get$unitName"}(); | ||
} | ||
} |
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,81 @@ | ||
<?php | ||
|
||
namespace Unitz\Rate; | ||
|
||
use Unitz\Time; | ||
use Unitz\Volume; | ||
|
||
/** Magic methods available through AbstractRate::__call() | ||
* @method Volume getOuncesPerMillisecond() | ||
* @method Volume getOuncesPerSecond() | ||
* @method Volume getOuncesPerMinute() | ||
* @method Volume getOuncesPerHour() | ||
* @method Volume getOuncesPerDay() | ||
* @method Volume getOuncesPerWeek() | ||
* @method Volume getOuncesPerMonth() | ||
* @method Volume getOuncesPerYear() | ||
* @method Volume getGallonsPerMillisecond | ||
* @method Volume getGallonsPerSecond() | ||
* @method Volume getGallonsPerMinute() | ||
* @method Volume getGallonsPerHour() | ||
* @method Volume getGallonsPerDay() | ||
* @method Volume getGallonsPerWeek() | ||
* @method Volume getGallonsPerMonth() | ||
* @method Volume getGallonsPerYear() | ||
* @method Volume getLitersPerMillisecond() | ||
* @method Volume getLitersPerSecond() | ||
* @method Volume getLitersPerMinute() | ||
* @method Volume getLitersPerHour() | ||
* @method Volume getLitersPerDay() | ||
* @method Volume getLitersPerWeek() | ||
* @method Volume getLitersPerMonth() | ||
* @method Volume getLitersPerYear() | ||
* @method Volume getMillilitersPerMillisecond() | ||
* @method Volume getMillilitersPerSecond() | ||
* @method Volume getMillilitersPerMinute() | ||
* @method Volume getMillilitersPerHour() | ||
* @method Volume getMillilitersPerDay() | ||
* @method Volume getMillilitersPerWeek() | ||
* @method Volume getMillilitersPerMonth() | ||
* @method Volume getMillilitersPerYear() | ||
* @method Volume getHectolitersPerMillisecond | ||
* @method Volume getHectolitersPerSecond() | ||
* @method Volume getHectolitersPerMinute() | ||
* @method Volume getHectolitersPerHour() | ||
* @method Volume getHectolitersPerDay() | ||
* @method Volume getHectolitersPerWeek() | ||
* @method Volume getHectolitersPerMonth() | ||
* @method Volume getHectolitersPerYear() | ||
* @method Volume getBarrelsPerSecond() | ||
* @method Volume getBarrelsPerMinute() | ||
* @method Volume getBarrelsPerHour() | ||
* @method Volume getBarrelsPerDay() | ||
* @method Volume getBarrelsPerWeek() | ||
* @method Volume getBarrelsPerMonth() | ||
* @method Volume getBarrelsPerYear() | ||
*/ | ||
class Flow extends AbstractRate | ||
{ | ||
public function __construct(private readonly Volume $volume, private readonly Time $time) | ||
{ | ||
parent::__construct(); | ||
$this->setNumerator($volume); | ||
$this->setDenominator($time); | ||
} | ||
|
||
/** | ||
* @return \Unitz\Volume | ||
*/ | ||
public function getVolume(): Volume | ||
{ | ||
return $this->volume; | ||
} | ||
|
||
/** | ||
* @return \Unitz\Time | ||
*/ | ||
public function getTime(): Time | ||
{ | ||
return $this->time; | ||
} | ||
} |
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
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
Oops, something went wrong.