@@ 13-81 (lines=69) @@ | ||
10 | * @author Kristjan Siimson <[email protected]> |
|
11 | * @package Selector\Domain |
|
12 | */ |
|
13 | class FirstOf implements Selector |
|
14 | { |
|
15 | /** |
|
16 | * @var Direction |
|
17 | */ |
|
18 | private $direction; |
|
19 | ||
20 | /** |
|
21 | * @var Evaluator |
|
22 | */ |
|
23 | private $evaluator; |
|
24 | ||
25 | /** |
|
26 | * @param Direction $direction |
|
27 | * @param Evaluator $evaluator |
|
28 | */ |
|
29 | public function __construct(Direction $direction, Evaluator $evaluator) |
|
30 | { |
|
31 | $this->direction = $direction; |
|
32 | $this->evaluator = $evaluator; |
|
33 | } |
|
34 | ||
35 | /** |
|
36 | * @param string $date |
|
37 | * @param Period $period |
|
38 | * @return string |
|
39 | */ |
|
40 | public function select($date, Period $period) |
|
41 | { |
|
42 | $match = $this->getFirstInPeriod( |
|
43 | new \DateTime($period->getStartDate()), |
|
44 | new \DateTime($period->getEndDate()) |
|
45 | ); |
|
46 | ||
47 | if ($this->direction->compare($date, $match) !== -1) { |
|
48 | $this->direction->next($period); |
|
49 | $match = $this->getFirstInPeriod( |
|
50 | new \DateTime($period->getStartDate()), |
|
51 | new \DateTime($period->getEndDate()) |
|
52 | ); |
|
53 | } |
|
54 | ||
55 | return $match; |
|
56 | } |
|
57 | ||
58 | /** |
|
59 | * @param \DateTime $startDate |
|
60 | * @param \DateTime $endDate |
|
61 | * @return null|string |
|
62 | * @throws \LogicException |
|
63 | */ |
|
64 | private function getFirstInPeriod(\DateTime $startDate, \DateTime $endDate) |
|
65 | { |
|
66 | $match = null; |
|
67 | ||
68 | for ($i = clone $startDate; $i < $endDate; $i->add(new \DateInterval('P1D'))) { |
|
69 | if ($this->evaluator->is($i->format('Y-m-d'))) { |
|
70 | $match = $i->format('Y-m-d'); |
|
71 | break; |
|
72 | } |
|
73 | } |
|
74 | ||
75 | if ($match === null) { |
|
76 | throw new \LogicException('No match in period'); |
|
77 | } |
|
78 | ||
79 | return $match; |
|
80 | } |
|
81 | } |
|
82 |
@@ 13-81 (lines=69) @@ | ||
10 | * @author Kristjan Siimson <[email protected]> |
|
11 | * @package Selector\Domain |
|
12 | */ |
|
13 | class LastOf implements Selector |
|
14 | { |
|
15 | /** |
|
16 | * @var Direction |
|
17 | */ |
|
18 | private $direction; |
|
19 | ||
20 | /** |
|
21 | * @var Evaluator |
|
22 | */ |
|
23 | private $evaluator; |
|
24 | ||
25 | /** |
|
26 | * @param Direction $direction |
|
27 | * @param Evaluator $evaluator |
|
28 | */ |
|
29 | public function __construct(Direction $direction, Evaluator $evaluator) |
|
30 | { |
|
31 | $this->direction = $direction; |
|
32 | $this->evaluator = $evaluator; |
|
33 | } |
|
34 | ||
35 | /** |
|
36 | * @param string $date |
|
37 | * @param Period $period |
|
38 | * @return string |
|
39 | */ |
|
40 | public function select($date, Period $period) |
|
41 | { |
|
42 | $match = $this->getLastInPeriod( |
|
43 | new \DateTime($period->getStartDate()), |
|
44 | new \DateTime($period->getEndDate()) |
|
45 | ); |
|
46 | ||
47 | if ($this->direction->compare($date, $match) !== -1) { |
|
48 | $this->direction->next($period); |
|
49 | $match = $this->getLastInPeriod( |
|
50 | new \DateTime($period->getStartDate()), |
|
51 | new \DateTime($period->getEndDate()) |
|
52 | ); |
|
53 | } |
|
54 | ||
55 | return $match; |
|
56 | } |
|
57 | ||
58 | /** |
|
59 | * @param \DateTime $startDate |
|
60 | * @param \DateTime $endDate |
|
61 | * @return null|string |
|
62 | * @throws \LogicException |
|
63 | */ |
|
64 | private function getLastInPeriod(\DateTime $startDate, \DateTime $endDate) |
|
65 | { |
|
66 | $match = null; |
|
67 | ||
68 | for ($i = clone $endDate; $i > $startDate; $i->sub(new \DateInterval('P1D'))) { |
|
69 | if ($this->evaluator->is($i->format('Y-m-d'))) { |
|
70 | $match = $i->format('Y-m-d'); |
|
71 | break; |
|
72 | } |
|
73 | } |
|
74 | ||
75 | if ($match === null) { |
|
76 | throw new \LogicException('No match in period'); |
|
77 | } |
|
78 | ||
79 | return $match; |
|
80 | } |
|
81 | } |
|
82 |