forked from ILIAS-eLearning/ILIAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRange.php
80 lines (67 loc) · 1.59 KB
/
Range.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php declare(strict_types=1);
namespace ILIAS\Data;
/**
* A simple class to express a naive range of whole positive numbers.
*
* @author Nils Haagen <[email protected]>
*/
class Range
{
/**
* @var integer
*/
protected $start;
/**
* @var integer
*/
protected $length;
public function __construct(int $start, int $length)
{
$this->checkStart($start);
$this->checkLength($length);
$this->start = $start;
$this->length = $length;
}
protected function checkStart(int $start)
{
if ($start < 0) {
throw new \InvalidArgumentException("Start must be a positive number (or 0)", 1);
}
}
protected function checkLength(int $length)
{
if ($length < 1) {
throw new \InvalidArgumentException("Length must be larger than 1", 1);
}
}
public function unpack() : array
{
return [$this->start, $this->length];
}
public function getStart() : int
{
return $this->start;
}
public function getLength() : int
{
return $this->length;
}
public function getEnd() : int
{
return $this->start + $this->length;
}
public function withStart(int $start) : Range
{
$this->checkStart($start);
$clone = clone $this;
$clone->start = $start;
return $clone;
}
public function withLength(int $length) : Range
{
$this->checkLength($length);
$clone = clone $this;
$clone->length = $length;
return $clone;
}
}