Completed
Push — master ( 363bb4...91ed68 )
by Yohan
02:04
created

Hour::createInvalidException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace CalendR\Period;
4
use CalendR\Period\Exception\NotAnHour;
5
6
/**
7
 * Represents an hour.
8
 *
9
 * @author Zander Baldwin <[email protected]>
10
 */
11
class Hour extends PeriodAbstract implements \Iterator
12
{
13
    /**
14
     * @var PeriodInterface
15
     */
16
    private $current;
17
18
    /**
19
     * Returns the period as a DatePeriod.
20
     *
21
     * @return \DatePeriod
22
     */
23
    public function getDatePeriod()
24
    {
25
        return new \DatePeriod($this->begin, new \DateInterval('PT1M'), $this->end);
26
    }
27
28
    /**
29
     * @param \DateTime $start
30
     *
31
     * @return bool
32
     */
33
    public static function isValid(\DateTime $start)
34
    {
35
        return $start->format('i:s') == '00:00';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function current()
42
    {
43
        return $this->current;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function next()
50
    {
51
        if (null === $this->current) {
52
            $this->current = $this->getFactory()->createMinute($this->begin);
53
        } else {
54
            $this->current = $this->current->getNext();
55
            if (!$this->contains($this->current->getBegin())) {
56
                $this->current = null;
57
            }
58
        }
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function key()
65
    {
66
        return (int) $this->current->getBegin()->format('G');
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function valid()
73
    {
74
        return null !== $this->current;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function rewind()
81
    {
82
        $this->current = null;
83
        $this->next();
84
    }
85
86
    /**
87
     * Returns the hour.
88
     *
89
     * @return string
90
     */
91
    public function __toString()
92
    {
93
        return $this->format('G');
94
    }
95
96
    /**
97
     * Returns a \DateInterval equivalent to the period.
98
     *
99
     * @return \DateInterval
100
     */
101
    public static function getDateInterval()
102
    {
103
        return new \DateInterval('PT1H');
104
    }
105
106
    /**
107
     * @return NotAnHour
108
     */
109
    protected function createInvalidException()
110
    {
111
        return new NotAnHour;
112
    }
113
}
114