-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapData.php
More file actions
92 lines (74 loc) · 1.95 KB
/
MapData.php
File metadata and controls
92 lines (74 loc) · 1.95 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace SyncEngine\Structure\Data;
use SyncEngine\Structure\Collection\AbstractCollection;
use Traversable;
class MapData implements \ArrayAccess, \Countable, \IteratorAggregate
{
private array $map = [];
public function add( string $source, string $target ): static
{
if ( ! isset( $this->map[ $source ] ) ) {
$this->map[ $source ] = [];
}
if ( ! in_array( $target, $this->map[ $source ] ) ) {
$this->map[ $source ][] = $target;
}
return $this;
}
public function getSources(): array
{
return array_keys( $this->getMap() );
}
public function getTargets(): array
{
$targets = [];
foreach ( $this->getMap() as $target ) {
$targets = array_merge( $targets, $target );
}
return $targets;
}
public function getTarget( string $source ): array
{
return $this->map[ $source ] ?? [];
}
public function getFirstTarget( string $source ): mixed
{
return ! empty( $this->map[ $source ] ) ? reset( $this->map[ $source ] ) : null;
}
public function getLastTarget( string $source ): mixed
{
return ! empty( $this->map[ $source ] ) ? end( $this->map[ $source ] ) : null;
}
public function getMap(): array
{
return $this->map;
}
public function getIterator(): Traversable
{
foreach ( $this->map as $source => $targets ) {
foreach ( (array) $targets as $target ) {
yield $source => $target;
}
}
}
public function offsetExists( mixed $offset ): bool
{
return isset( $this->map[ AbstractCollection::fixFloatOffset( $offset ) ] );
}
public function offsetGet( mixed $offset ): array
{
return $this->map[ AbstractCollection::fixFloatOffset( $offset ) ] ?? [];
}
public function offsetSet( mixed $offset, mixed $value ): void
{
$this->map[ AbstractCollection::fixFloatOffset( $offset ) ] = (array) $value;
}
public function offsetUnset( mixed $offset ): void
{
unset( $this->map[ AbstractCollection::fixFloatOffset( $offset ) ] );
}
public function count(): int
{
return count( $this->map );
}
}