1: <?php
2:
3: namespace Cron;
4:
5: /**
6: * CRON field factory implementing a flyweight factory
7: * @link http://en.wikipedia.org/wiki/Cron
8: */
9: class FieldFactory
10: {
11: /**
12: * @var array Cache of instantiated fields
13: */
14: private $fields = array();
15:
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: * @return FieldInterface
22: * @throws InvalidArgumentException if a position is not valid
23: */
24: public function getField($position)
25: {
26: if (!isset($this->fields[$position])) {
27: switch ($position) {
28: case 0:
29: $this->fields[$position] = new MinutesField();
30: break;
31: case 1:
32: $this->fields[$position] = new HoursField();
33: break;
34: case 2:
35: $this->fields[$position] = new DayOfMonthField();
36: break;
37: case 3:
38: $this->fields[$position] = new MonthField();
39: break;
40: case 4:
41: $this->fields[$position] = new DayOfWeekField();
42: break;
43: case 5:
44: $this->fields[$position] = new YearField();
45: break;
46: default:
47: throw new \InvalidArgumentException(
48: $position . ' is not a valid position'
49: );
50: }
51: }
52:
53: return $this->fields[$position];
54: }
55: }
56: