1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2016 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupRa\RaBundle\Value; |
20
|
|
|
|
21
|
|
|
use DateInterval; |
22
|
|
|
use Surfnet\StepupRa\RaBundle\Exception\InvalidArgumentException; |
23
|
|
|
|
24
|
|
|
final class TimeFrame |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var DateInterval |
28
|
|
|
*/ |
29
|
|
|
private $timeFrame; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param DateInterval $timeFrame |
33
|
|
|
*/ |
34
|
|
|
final private function __construct(DateInterval $timeFrame) |
35
|
|
|
{ |
36
|
|
|
$this->timeFrame = $timeFrame; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param int $seconds |
41
|
|
|
* @return TimeFrame |
42
|
|
|
*/ |
43
|
|
|
public static function ofSeconds($seconds) |
44
|
|
|
{ |
45
|
|
|
if (!is_int($seconds) || $seconds < 1) { |
46
|
|
|
throw InvalidArgumentException::invalidType('positive integer', 'seconds', $seconds); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return new TimeFrame(new DateInterval('PT' . $seconds . 'S')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param DateTime $dateTime |
54
|
|
|
* @return DateTime |
55
|
|
|
*/ |
56
|
|
|
public function getEndWhenStartingAt(DateTime $dateTime) |
57
|
|
|
{ |
58
|
|
|
return $dateTime->add($this->timeFrame); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param TimeFrame $other |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function equals(TimeFrame $other) |
66
|
|
|
{ |
67
|
|
|
return $this->timeFrame->s === $other->timeFrame->s; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function __toString() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
return $this->timeFrame->format('%S'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.