ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
Range.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
3 namespace ILIAS\Data;
4 
10 class Range
11 {
15  protected $start;
16 
20  protected $length;
21 
22 
23  public function __construct(int $start, int $length)
24  {
25  $this->checkStart($start);
26  $this->checkLength($length);
27  $this->start = $start;
28  $this->length = $length;
29  }
30 
31  protected function checkStart(int $start)
32  {
33  if ($start < 0) {
34  throw new \InvalidArgumentException("Start must be a positive number (or 0)", 1);
35  }
36  }
37 
38  protected function checkLength(int $length)
39  {
40  if ($length < 1) {
41  throw new \InvalidArgumentException("Length must be larger than 1", 1);
42  }
43  }
44 
45  public function unpack() : array
46  {
47  return [$this->start, $this->length];
48  }
49 
50  public function getStart() : int
51  {
52  return $this->start;
53  }
54 
55  public function getLength() : int
56  {
57  return $this->length;
58  }
59 
60  public function getEnd() : int
61  {
62  return $this->start + $this->length;
63  }
64 
65  public function withStart(int $start) : Range
66  {
67  $this->checkStart($start);
68  $clone = clone $this;
69  $clone->start = $start;
70  return $clone;
71  }
72 
73  public function withLength(int $length) : Range
74  {
75  $this->checkLength($length);
76  $clone = clone $this;
77  $clone->length = $length;
78  return $clone;
79  }
80 }
checkLength(int $length)
Definition: Range.php:38
withLength(int $length)
Definition: Range.php:73
__construct(int $start, int $length)
Definition: Range.php:23
withStart(int $start)
Definition: Range.php:65
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:10
checkStart(int $start)
Definition: Range.php:31