|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Fixtures\Innmind\Immutable; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\BlackBox\Set; |
|
|
|
|
|
|
7
|
|
|
use Innmind\Immutable\Sequence as Structure; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* {@inheritdoc} |
|
11
|
|
|
* @template I |
|
12
|
|
|
*/ |
|
13
|
|
|
final class Sequence implements Set |
|
14
|
|
|
{ |
|
15
|
|
|
private $type; |
|
16
|
|
|
private $set; |
|
17
|
|
|
private $sizes; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(string $type, Set $set, Set\Integers $sizes = null) |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
$this->type = $type; |
|
22
|
|
|
$this->set = $set; |
|
23
|
|
|
$this->sizes = ($sizes ?? Set\Integers::between(0, 100))->take(100); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return Set<Structure<I>> |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function of(string $type, Set $set, Set\Integers $sizes = null): self |
|
30
|
|
|
{ |
|
31
|
|
|
return new self($type, $set, $sizes); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function take(int $size): Set |
|
35
|
|
|
{ |
|
36
|
|
|
$self = clone $this; |
|
37
|
|
|
$self->sizes = $this->sizes->take($size); |
|
38
|
|
|
|
|
39
|
|
|
return $self; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function filter(callable $predicate): Set |
|
46
|
|
|
{ |
|
47
|
|
|
throw new \LogicException('Sequence set can\'t be filtered, underlying set must be filtered beforehand'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return \Generator<Set\Value<Structure<I>>> |
|
52
|
|
|
*/ |
|
53
|
|
|
public function values(): \Generator |
|
54
|
|
|
{ |
|
55
|
|
|
$immutable = $this->set->values()->current()->isImmutable(); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($this->sizes->values() as $size) { |
|
58
|
|
|
if ($immutable) { |
|
59
|
|
|
yield Set\Value::immutable($this->generate($size->unwrap())); |
|
|
|
|
|
|
60
|
|
|
} else { |
|
61
|
|
|
yield Set\Value::mutable(fn() => $this->generate($size->unwrap())); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function generate(int $size): Structure |
|
67
|
|
|
{ |
|
68
|
|
|
$sequence = Structure::of($this->type); |
|
69
|
|
|
$values = $this->set->take($size)->values(); |
|
70
|
|
|
|
|
71
|
|
|
while ($sequence->size() < $size) { |
|
72
|
|
|
$sequence = ($sequence)($values->current()->unwrap()); |
|
73
|
|
|
$values->next(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $sequence; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: