Debian Codegraph @

vendor/dragonmantank/cron-expression/src/Cron/FieldFactory.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Cron;
6
7 use InvalidArgumentException;
8
9 /**
10 * CRON field factory implementing a flyweight factory.
11 *
12 * @see http://en.wikipedia.org/wiki/Cron
13 */
14 class FieldFactory implements FieldFactoryInterface
15 {
16 /**
17 * @var array<int, FieldInterface> Cache of instantiated fields
18 */
19 private $fields = [];
20
21 /**
22 * Get an instance of a field object for a cron expression position.
23 *
24 * @param int $position CRON expression position value to retrieve
25 *
26 * @throws InvalidArgumentException if a position is not valid
27 */
28 public function getField(int $position): FieldInterface
29 {
30 return $this->fields[$position] ?? $this->fields[$position] = $this->instantiateField($position);
31 }
32
33 private function instantiateField(int $position): FieldInterface
34 {
35 switch ($position) {
36 case CronExpression::MINUTE:
37 return new MinutesField();
38 case CronExpression::HOUR:
39 return new HoursField();
40 case CronExpression::DAY:
41 return new DayOfMonthField();
42 case CronExpression::MONTH:
43 return new MonthField();
44 case CronExpression::WEEKDAY:
45 return new DayOfWeekField();
46 }
47
48 throw new InvalidArgumentException(
49 ($position + 1) . ' is not a valid position'
50 );
51 }
52 }