Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface Strategy {
- public function calculate(array $criteria): array;
- }
- class TwoThousandNineteenStrategy implements Stragety {
- public function calculate(array $criteria): array
- {
- // query and calculation for 2019.
- }
- }
- class TwoThousandEighteenStrategy implements Stragety {
- public function calculate(array $criteria): array
- {
- // query and calculation for 2018.
- }
- }
- class StrategyFactory
- {
- public function create(int $year): Strategy
- {
- if ($year === 2018) {
- return new TwoThousandEighteenStrategy();
- }
- if ($year === 2019) {
- return new TwoThousandNineteenStrategy();
- }
- throw new InvalidArgumentException('Missing strategy for given year');
- }
- }
- $strategyFactory = new StrategyFactory();
- foreach ($years as $year) {
- $stategy = $strategyFactory->create($year);
- resultOfTheYear = $strategy->calcule([]);
- }
Advertisement
Add Comment
Please, Sign In to add comment