Skip to content

Commit 47fe726

Browse files
committed
add Bounds class
1 parent 855273f commit 47fe726

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/Bounds.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Permafrost\CodeSnippets;
4+
5+
class Bounds
6+
{
7+
/** @var int */
8+
public $start = 0;
9+
10+
/** @var int */
11+
public $end = 0;
12+
13+
public function __construct(int $start, int $end)
14+
{
15+
$this->start = $start;
16+
$this->end = $end;
17+
}
18+
19+
public static function create(int $start, int $end): self
20+
{
21+
return new static(...func_get_args());
22+
}
23+
24+
public static function createFromArray(array $lineNumbers): self
25+
{
26+
sort($lineNumbers, SORT_NUMERIC);
27+
28+
return static::create($lineNumbers[0], $lineNumbers[count($lineNumbers) - 1]);
29+
}
30+
31+
public function toArray(): array
32+
{
33+
return [$this->start, $this->end];
34+
}
35+
36+
public function mergeWith(self $bounds): self
37+
{
38+
$data = array_merge($this->toArray(), $bounds->toArray());
39+
40+
return static::createFromArray($data);
41+
}
42+
43+
public function copy(self $bounds): self
44+
{
45+
$this->start = $bounds->start;
46+
$this->end = $bounds->end;
47+
48+
return $this;
49+
}
50+
}

0 commit comments

Comments
 (0)