-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavigation.php
More file actions
65 lines (56 loc) · 1.88 KB
/
Copy pathNavigation.php
File metadata and controls
65 lines (56 loc) · 1.88 KB
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
<?php
declare(strict_types=1);
namespace TinyBlocks\HttpQuery;
use TinyBlocks\Collection\Collection;
use TinyBlocks\Http\LinkRelation;
/**
* Ordered set of navigation targets a result exposes, each pairing a relation with its paging.
*
* <p>A result lists its own targets, so {@see Links} renders every result uniformly without
* branching on the concrete result type. The insertion order is preserved.</p>
*/
final readonly class Navigation
{
/**
* @param Collection<NavigationTarget> $targets
*/
private function __construct(private Collection $targets)
{
}
/**
* Creates an empty Navigation that carries no target.
*
* @return Navigation The empty navigation.
*/
public static function empty(): Navigation
{
/** @var Collection<NavigationTarget> $targets */
$targets = Collection::createFromEmpty();
return new Navigation(targets: $targets);
}
/**
* Returns a copy of the Navigation with the target added under the relation.
*
* <p>A null target is ignored, so the copy carries the same targets as the original.</p>
*
* @param Pagination|null $target The paging the target points to, or null to ignore.
* @param LinkRelation $relation The relation the target is reached through.
* @return Navigation A copy carrying the original targets plus the added target.
*/
public function with(?Pagination $target, LinkRelation $relation): Navigation
{
if (is_null($target)) {
return $this;
}
return new Navigation(targets: $this->targets->add(NavigationTarget::to(target: $target, relation: $relation)));
}
/**
* Returns the navigation targets in insertion order.
*
* @return Collection<NavigationTarget> The navigation targets.
*/
public function targets(): Collection
{
return $this->targets;
}
}