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

53 lines
1.4 KiB
PHP
Raw Normal View History

2024-05-07 12:17:25 +02:00
<?php
2024-08-13 13:44:16 +00:00
declare(strict_types=1);
2024-05-07 12:17:25 +02:00
namespace Cron;
use InvalidArgumentException;
/**
2024-08-13 13:44:16 +00:00
* CRON field factory implementing a flyweight factory.
*
* @see http://en.wikipedia.org/wiki/Cron
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
class FieldFactory implements FieldFactoryInterface
2024-05-07 12:17:25 +02:00
{
/**
* @var array Cache of instantiated fields
*/
2024-08-13 13:44:16 +00:00
private $fields = [];
2024-05-07 12:17:25 +02:00
/**
2024-08-13 13:44:16 +00:00
* Get an instance of a field object for a cron expression position.
2024-05-07 12:17:25 +02:00
*
* @param int $position CRON expression position value to retrieve
*
* @throws InvalidArgumentException if a position is not valid
*/
2024-08-13 13:44:16 +00:00
public function getField(int $position): FieldInterface
{
return $this->fields[$position] ?? $this->fields[$position] = $this->instantiateField($position);
}
private function instantiateField(int $position): FieldInterface
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
switch ($position) {
case CronExpression::MINUTE:
return new MinutesField();
case CronExpression::HOUR:
return new HoursField();
case CronExpression::DAY:
return new DayOfMonthField();
case CronExpression::MONTH:
return new MonthField();
case CronExpression::WEEKDAY:
return new DayOfWeekField();
2024-05-07 12:17:25 +02:00
}
2024-08-13 13:44:16 +00:00
throw new InvalidArgumentException(
($position + 1) . ' is not a valid position'
);
2024-05-07 12:17:25 +02:00
}
}