截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FieldFactory.php 1.4KB

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