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

Year::isValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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