PeriodAbstract::includes()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 2
1
<?php
2
3
/*
4
 * This file is part of CalendR, a Fréquence web project.
5
 *
6
 * (c) 2012 Fréquence web
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CalendR\Period;
13
14
use CalendR\Event\EventInterface;
15
16
/**
17
 * An abstract class that represent a date period and provide some base helpers.
18
 *
19
 * @author Yohan Giarelli <[email protected]>
20
 */
21
abstract class PeriodAbstract implements PeriodInterface
22
{
23
    /**
24
     * @var \DateTime
25
     */
26
    protected $begin;
27
28
    /**
29
     * @var \DateTime
30
     */
31
    protected $end;
32
33
    /**
34
     * @var FactoryInterface
35
     */
36
    protected $factory;
37
38
    /**
39
     * @param \DateTime        $begin
40
     * @param FactoryInterface $factory
41
     *
42
     * @throws \CalendR\Exception
43
     */
44
    public function __construct(\DateTime $begin, FactoryInterface $factory)
45
    {
46
        $this->factory = $factory;
47
        if (!static::isValid($begin)) {
48
            throw $this->createInvalidException();
49
        }
50
51
        $this->begin = clone $begin;
52
        $this->end   = clone $begin;
53
        $this->end->add($this->getDateInterval());
54
    }
55
56
    /**
57
     * Checks if the given period is contained in the current period.
58
     *
59
     * @param \DateTime $date
60
     *
61
     * @return bool true if the period contains this date
62
     */
63
    public function contains(\DateTime $date)
64
    {
65
        return $this->begin <= $date && $date < $this->end;
66
    }
67
68
    /**
69
     * Checks if a period is equals to an other.
70
     *
71
     * @param PeriodInterface $period
72
     *
73
     * @return bool
74
     */
75
    public function equals(PeriodInterface $period)
76
    {
77
        return
78
            $period instanceof static &&
79
            $this->begin->format('Y-m-d-H-i-s') === $period->getBegin()->format('Y-m-d-H-i-s')
80
        ;
81
    }
82
83
    /**
84
     * Returns true if the period include the other period
85
     * given as argument.
86
     *
87
     * @param PeriodInterface $period
88
     * @param bool            $strict
89
     *
90
     * @return bool
91
     */
92
    public function includes(PeriodInterface $period, $strict = true)
93
    {
94
        if (true === $strict) {
95
            return $this->getBegin() <= $period->getBegin() && $this->getEnd() >= $period->getEnd();
96
        }
97
98
        return
99
            $this->includes($period, true) ||
100
            $period->includes($this, true) ||
101
            $this->contains($period->getBegin()) ||
102
            $this->contains($period->getEnd())
103
        ;
104
    }
105
106
    /**
107
     * Returns if $event is during this period.
108
     * Non strict. Must return true if :
109
     *  * Event is during period
110
     *  * Period is during event
111
     *  * Event begin is during Period
112
     *  * Event end is during Period.
113
     *
114
     * @param EventInterface $event
115
     *
116
     * @return bool
117
     */
118
    public function containsEvent(EventInterface $event)
119
    {
120
        return
121
            $event->containsPeriod($this) ||
122
            $event->isDuring($this) ||
123
            $this->contains($event->getBegin()) ||
124
            ($event->getEnd() && $this->contains($event->getEnd())  && $event->getEnd()->format('c') !== $this->begin->format('c'))
125
        ;
126
    }
127
128
    /**
129
     * Format the period to a string.
130
     *
131
     * @param string $format
132
     *
133
     * @return string
134
     */
135
    public function format($format)
136
    {
137
        return $this->begin->format($format);
138
    }
139
140
    /**
141
     * Returns if the current period is the current one.
142
     *
143
     * @return bool
144
     */
145
    public function isCurrent()
146
    {
147
        return $this->contains(new \DateTime());
148
    }
149
150
    /**
151
     * Gets the next period of the same type.
152
     *
153
     * @return PeriodInterface
154
     */
155
    public function getNext()
156
    {
157
        return new static($this->end, $this->factory);
158
    }
159
160
    /**
161
     * Gets the previous period of the same type.
162
     *
163
     * @return PeriodInterface
164
     */
165
    public function getPrevious()
166
    {
167
        $start = clone $this->begin;
168
        $start->sub(static::getDateInterval());
169
170
        return new static($start, $this->factory);
171
    }
172
173
    /**
174
     * @return \DateTime
175
     */
176
    public function getBegin()
177
    {
178
        return clone $this->begin;
179
    }
180
181
    /**
182
     * @return \DateTime
183
     */
184
    public function getEnd()
185
    {
186
        return clone $this->end;
187
    }
188
189
    /**
190
     * @return FactoryInterface
191
     */
192
    public function getFactory()
193
    {
194
        if (null === $this->factory) {
195
            $this->factory = new Factory();
196
        }
197
198
        return $this->factory;
199
    }
200
201
    /**
202
     * @return \CalendR\Exception
203
     */
204
    protected function createInvalidException()
205
    {
206
        $class = 'CalendR\Period\Exception\NotA' . (new \ReflectionClass($this))->getShortName();
207
208
        return new $class;
209
    }
210
}
211