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

Week::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 16
rs 9.4286
cc 3
eloc 9
nc 3
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Week::getDatePeriod() 0 4 1
1
<?php
2
3
namespace CalendR\Period;
4
5
/**
6
 * Represents a week.
7
 *
8
 * @author Yohan Giarelli <[email protected]>
9
 */
10
class Week extends PeriodAbstract implements \Iterator
11
{
12
    /**
13
     * @var null|PeriodInterface
14
     */
15
    private $current = null;
16
17
    /**
18
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
19
     */
20
    public function getNumber()
21
    {
22
        return $this->begin->format('W');
23
    }
24
25
    /**
26
     * Returns the period as a DatePeriod.
27
     *
28
     * @return \DatePeriod
29
     */
30
    public function getDatePeriod()
31
    {
32
        return new \DatePeriod($this->begin, new \DateInterval('P1D'), $this->end);
33
    }
34
35
    /**
36
     * @param \DateTime $start
37
     *
38
     * @return bool
39
     */
40
    public static function isValid(\DateTime $start)
41
    {
42
        if ($start->format('H:i:s') !== '00:00:00') {
43
            return false;
44
        }
45
46
        return true;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function current()
53
    {
54
        return $this->current;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function next()
61
    {
62
        if (!$this->valid()) {
63
            $this->current = $this->getFactory()->createDay($this->begin);
64
        } else {
65
            $this->current = $this->current->getNext();
66
            if (!$this->contains($this->current->getBegin())) {
67
                $this->current = null;
68
            }
69
        }
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function key()
76
    {
77
        return $this->current->getBegin()->format('d-m-Y');
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function valid()
84
    {
85
        return null !== $this->current;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function rewind()
92
    {
93
        $this->current = null;
94
        $this->next();
95
    }
96
97
    /**
98
     * Returns the week number.
99
     *
100
     * @return string
101
     */
102
    public function __toString()
103
    {
104
        return $this->format('W');
105
    }
106
107
    /**
108
     * Returns a \DateInterval equivalent to the period.
109
     *
110
     * @return \DateInterval
111
     */
112
    public static function getDateInterval()
113
    {
114
        return new \DateInterval('P1W');
115
    }
116
}
117