FieldFactory.php 1.5 KB

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