-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartialPath.php
More file actions
41 lines (32 loc) · 858 Bytes
/
Copy pathPartialPath.php
File metadata and controls
41 lines (32 loc) · 858 Bytes
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
<?php
declare(strict_types=1);
namespace Paths;
use Paths\Path;
use Paths\GraphNode;
use Paths\GraphNode\Terminal;
use Paths\GraphNode\NonTerminal;
class PartialPath
{
private Terminal $source;
private array $path;
public function __construct(Terminal $source, array $path = [])
{
$this->source = $source;
$this->path = $path;
}
public function withTerminal(Terminal $target): Path
{
return new Path($this->source, $this->path, $target);
}
public function withNonTerminal(NonTerminal $node): PartialPath
{
return new PartialPath($this->source, array_merge($this->path, [$node]));
}
public function lastNode(): GraphNode
{
if (empty($this->path)) {
return $this->source;
}
return $this->path[array_key_last($this->path)];
}
}