diff --git a/config/services.yaml b/config/services.yaml index 8282741..2269883 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -15,7 +15,9 @@ services: App\Gateway\GatewayInterface: tags: ['app.gateway.gateway'] App\Money\Currency\ExchangeInterface: - tags: ['app.lib.economy.currency.exchange'] + tags: ['app.money.currency.exchange'] + App\Money\Totalization\TotalizerInterface: + tags: ['app.money.totalization.totalizer'] App\Matchfunding\Formula\FormulaInterface: tags: ['app.matchfunding.formula'] App\Matchfunding\Rule\RuleInterface: @@ -96,7 +98,11 @@ services: App\Money\Currency\ExchangeLocator: arguments: - - !tagged 'app.lib.economy.currency.exchange' + - !tagged 'app.money.currency.exchange' + + App\Money\Totalization\TotalizerLocator: + arguments: + - !tagged 'app.money.totalization.totalizer' App\Mapping\AutoMapper: arguments: diff --git a/src/Money/Totalization/TotalizedMoney.php b/src/Money/Totalization/TotalizedMoney.php new file mode 100644 index 0000000..c1f46e8 --- /dev/null +++ b/src/Money/Totalization/TotalizedMoney.php @@ -0,0 +1,32 @@ +amount; + } + + public function getCurrency(): string + { + return $this->currency; + } + + /** + * @return int the total number of items that were totalized to produce the Money + */ + public function getLength(): int + { + return $this->length; + } +} diff --git a/src/Money/Totalization/TotalizerInterface.php b/src/Money/Totalization/TotalizerInterface.php new file mode 100644 index 0000000..fe2240a --- /dev/null +++ b/src/Money/Totalization/TotalizerInterface.php @@ -0,0 +1,16 @@ + */ + private array $totalizers; + + public function __construct( + iterable $interfaces, + ) { + /** @var TotalizerInterface[] */ + $totalizers = \iterator_to_array($interfaces); + + foreach ($totalizers as $totalizer) { + $resource = $totalizer::getSupportedResource(); + + if (\array_key_exists($resource, $this->totalizers)) { + throw new \Exception(\sprintf( + "Duplicated TotalizerInterface. The resource '%s' is already supported by %s", + $resource, + $totalizers[$resource]::class + )); + } + + $this->totalizers[$resource] = $totalizer; + } + } + + public function get(string $resource): ?TotalizerInterface + { + return $this->totalizers[$resource] ?? null; + } + + /** + * @return array + */ + public function getAll(): array + { + return $this->totalizers; + } +}