1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/dbal project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Dbal\Migration; |
10
|
|
|
|
11
|
|
|
use Daikon\DataStructure\TypedList; |
12
|
|
|
use Daikon\Interop\ToNativeInterface; |
13
|
|
|
|
14
|
|
|
final class MigrationList extends TypedList implements ToNativeInterface |
15
|
|
|
{ |
16
|
|
|
public function __construct(iterable $migrations = []) |
17
|
|
|
{ |
18
|
|
|
$this->init($migrations, [MigrationInterface::class]); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function exclude(self $migrationList): self |
22
|
|
|
{ |
23
|
|
|
return $this->filter( |
24
|
|
|
fn(MigrationInterface $migration): bool => !$migrationList->contains($migration) |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getPendingMigrations(): self |
29
|
|
|
{ |
30
|
|
|
return $this->filter( |
31
|
|
|
fn(MigrationInterface $migration): bool => !$migration->hasExecuted() |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getExecutedMigrations(): self |
36
|
|
|
{ |
37
|
|
|
return $this->filter( |
38
|
|
|
fn(MigrationInterface $migration): bool => $migration->hasExecuted() |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function contains(MigrationInterface $migration): bool |
43
|
|
|
{ |
44
|
|
|
return $this->reduce( |
45
|
|
|
function (bool $carry, MigrationInterface $item) use ($migration): bool { |
46
|
|
|
return $carry |
47
|
|
|
|| $item->getName() === $migration->getName() |
48
|
|
|
&& $item->getVersion() === $migration->getVersion(); |
49
|
|
|
}, |
50
|
|
|
false |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function sortByVersion(): self |
55
|
|
|
{ |
56
|
|
|
return $this->sort( |
57
|
|
|
fn(MigrationInterface $a, MigrationInterface $b): int => $a->getVersion() - $b->getVersion() |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function findBeforeVersion(int $version = null): self |
62
|
|
|
{ |
63
|
|
|
return $this->filter( |
64
|
|
|
fn(MigrationInterface $migration): bool => !$version || $migration->getVersion() <= $version |
|
|
|
|
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function findAfterVersion(int $version = null): self |
69
|
|
|
{ |
70
|
|
|
return $this->filter( |
71
|
|
|
fn(MigrationInterface $migration): bool => !$version || $migration->getVersion() > $version |
|
|
|
|
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function toNative(): array |
76
|
|
|
{ |
77
|
|
|
return $this->map( |
78
|
|
|
fn(MigrationInterface $migration): array => $migration->toNative() |
79
|
|
|
)->unwrap(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: