1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Fixtures\Innmind\Immutable; |
5
|
|
|
|
6
|
|
|
use Innmind\BlackBox\Set; |
|
|
|
|
7
|
|
|
use Innmind\Immutable\Map as Structure; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* {@inheritdoc} |
11
|
|
|
* @template I |
12
|
|
|
* @template J |
13
|
|
|
*/ |
14
|
|
|
final class Map implements Set |
15
|
|
|
{ |
16
|
|
|
private $keyType; |
17
|
|
|
private $valueType; |
18
|
|
|
private $keys; |
19
|
|
|
private $values; |
20
|
|
|
private $sizes; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
string $keyType, |
24
|
|
|
string $valueType, |
25
|
|
|
Set $keys, |
26
|
|
|
Set $values, |
27
|
|
|
Set\Integers $sizes = null |
|
|
|
|
28
|
|
|
) { |
29
|
|
|
$this->keyType = $keyType; |
30
|
|
|
$this->valueType = $valueType; |
31
|
|
|
$this->keys = $keys; |
32
|
|
|
$this->values = $values; |
33
|
|
|
$this->sizes = ($sizes ?? Set\Integers::between(0, 100))->take(100); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return Set<Structure<I, J>> |
38
|
|
|
*/ |
39
|
|
|
public static function of( |
40
|
|
|
string $keyType, |
41
|
|
|
string $valueType, |
42
|
|
|
Set $keys, |
43
|
|
|
Set $values, |
44
|
|
|
Set\Integers $sizes = null |
45
|
|
|
): self { |
46
|
|
|
return new self($keyType, $valueType, $keys, $values, $sizes); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function take(int $size): Set |
50
|
|
|
{ |
51
|
|
|
$self = clone $this; |
52
|
|
|
$self->sizes = $this->sizes->take($size); |
53
|
|
|
|
54
|
|
|
return $self; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function filter(callable $predicate): Set |
61
|
|
|
{ |
62
|
|
|
throw new \LogicException('Map set can\'t be filtered, underlying sets must be filtered beforehand'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return \Generator<Set\Value<Structure<I, J>>> |
67
|
|
|
*/ |
68
|
|
|
public function values(): \Generator |
69
|
|
|
{ |
70
|
|
|
$immutable = $this->keys->values()->current()->isImmutable() && |
71
|
|
|
$this->values->values()->current()->isImmutable(); |
72
|
|
|
|
73
|
|
|
foreach ($this->sizes->values() as $size) { |
74
|
|
|
if ($immutable) { |
75
|
|
|
yield Set\Value::immutable($this->generate($size->unwrap())); |
|
|
|
|
76
|
|
|
} else { |
77
|
|
|
yield Set\Value::mutable(fn() => $this->generate($size->unwrap())); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function generate(int $size): Structure |
83
|
|
|
{ |
84
|
|
|
$map = Structure::of($this->keyType, $this->valueType); |
85
|
|
|
$keys = $this->keys->take($size)->values(); |
86
|
|
|
$values = $this->values->take($size)->values(); |
87
|
|
|
|
88
|
|
|
while ($map->size() < $size) { |
89
|
|
|
$map = ($map)( |
90
|
|
|
$keys->current()->unwrap(), |
91
|
|
|
$values->current()->unwrap(), |
92
|
|
|
); |
93
|
|
|
$keys->next(); |
94
|
|
|
$values->next(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $map; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: