AbstractField.php
1 year ago
CronExpression.php
8 months ago
DayOfMonthField.php
1 year ago
DayOfWeekField.php
1 year ago
FieldFactory.php
1 year ago
FieldFactoryInterface.php
1 year ago
FieldInterface.php
1 year ago
HoursField.php
1 year ago
MinutesField.php
1 year ago
MonthField.php
1 year ago
index.php
1 year ago
FieldFactory.php
32 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | namespace Cron; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use InvalidArgumentException; |
| 6 | class FieldFactory implements FieldFactoryInterface |
| 7 | { |
| 8 | private $fields = []; |
| 9 | public function getField(int $position): FieldInterface |
| 10 | { |
| 11 | return $this->fields[$position] ?? $this->fields[$position] = $this->instantiateField($position); |
| 12 | } |
| 13 | private function instantiateField(int $position): FieldInterface |
| 14 | { |
| 15 | switch ($position) { |
| 16 | case CronExpression::MINUTE: |
| 17 | return new MinutesField(); |
| 18 | case CronExpression::HOUR: |
| 19 | return new HoursField(); |
| 20 | case CronExpression::DAY: |
| 21 | return new DayOfMonthField(); |
| 22 | case CronExpression::MONTH: |
| 23 | return new MonthField(); |
| 24 | case CronExpression::WEEKDAY: |
| 25 | return new DayOfWeekField(); |
| 26 | } |
| 27 | throw new InvalidArgumentException( |
| 28 | ($position + 1) . ' is not a valid position' |
| 29 | ); |
| 30 | } |
| 31 | } |
| 32 |