Completed
Push — master ( 5096ba...da0857 )
by Peter
07:17
created

WeekIntervalPoint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2016, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Interval\Week;
12
13
use GpsLab\Component\Interval\BaseIntervalPoint;
14
15
class WeekIntervalPoint extends BaseIntervalPoint
16
{
17
    /**
18
     * @var \DateTime
19
     */
20
    private $week;
21
22
    /**
23
     * @param \DateTime $date
24
     */
25 4
    public function __construct(\DateTime $date)
26
    {
27 4
        $this->week = $this->getMondayThisWeek($date)->setTime(0, 0, 0);
28 4
    }
29
30
    /**
31
     * Bugfix for get monday of this week.
32
     *
33
     * @see https://bugs.php.net/bug.php?id=63740
34
     *
35
     * @param \DateTime $date
36
     *
37
     * @return \DateTime
38
     */
39 4
    private function getMondayThisWeek(\DateTime $date)
40
    {
41 4
        $monday = clone $date;
42
43 4
        if ($monday->format('N') != 1) {
44 2
            $monday->modify('Monday this week');
45
46 2
            if ($date->format('Y W') != $monday->format('Y W')) {
47
                $monday->modify('-7 day');
48
            }
49 2
        }
50
51 4
        return $monday;
52
    }
53
54
    /**
55
     * @return \DateTime
56
     */
57 4
    public function value()
58
    {
59 4
        return clone $this->week;
60
    }
61
62
    /**
63
     * @return string
64
     */
65 1
    public function __toString()
66
    {
67 1
        return $this->value()->format('Y-m-d');
68
    }
69
}
70