1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Metadata; |
6
|
|
|
|
7
|
|
|
use Countable; |
8
|
|
|
use Doctrine\Migrations\Exception\MigrationNotExecuted; |
9
|
|
|
use Doctrine\Migrations\Exception\NoMigrationsFoundWithCriteria; |
10
|
|
|
use Doctrine\Migrations\Version\Version; |
11
|
|
|
use function array_values; |
12
|
|
|
use function count; |
13
|
|
|
|
14
|
|
|
final class ExecutedMigrationsList implements Countable |
15
|
|
|
{ |
16
|
|
|
/** @var ExecutedMigration[] */ |
17
|
|
|
private $items = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param ExecutedMigration[] $items |
21
|
|
|
*/ |
22
|
77 |
|
public function __construct(array $items) |
23
|
|
|
{ |
24
|
77 |
|
$this->items = array_values($items); |
25
|
77 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return ExecutedMigration[] |
29
|
|
|
*/ |
30
|
25 |
|
public function getItems() : array |
31
|
|
|
{ |
32
|
25 |
|
return $this->items; |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
public function getFirst(int $offset = 0) : ExecutedMigration |
36
|
|
|
{ |
37
|
2 |
|
if (! isset($this->items[$offset])) { |
38
|
1 |
|
throw NoMigrationsFoundWithCriteria::new('first' . ($offset > 0 ? '+' . $offset : '')); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return $this->items[$offset]; |
42
|
|
|
} |
43
|
|
|
|
44
|
17 |
|
public function getLast(int $offset = 0) : ExecutedMigration |
45
|
|
|
{ |
46
|
17 |
|
$offset = count($this->items) - 1 - (-1 * $offset); |
47
|
17 |
|
if (! isset($this->items[$offset])) { |
48
|
5 |
|
throw NoMigrationsFoundWithCriteria::new('last' . ($offset > 0 ? '+' . $offset : '')); |
49
|
|
|
} |
50
|
|
|
|
51
|
12 |
|
return $this->items[$offset]; |
52
|
|
|
} |
53
|
|
|
|
54
|
22 |
|
public function count() : int |
55
|
|
|
{ |
56
|
22 |
|
return count($this->items); |
57
|
|
|
} |
58
|
|
|
|
59
|
37 |
|
public function hasMigration(Version $version) : bool |
60
|
|
|
{ |
61
|
37 |
|
foreach ($this->items as $migration) { |
62
|
24 |
|
if ($migration->getVersion()->equals($version)) { |
63
|
24 |
|
return true; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
30 |
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
5 |
|
public function getMigration(Version $version) : ExecutedMigration |
71
|
|
|
{ |
72
|
5 |
|
foreach ($this->items as $migration) { |
73
|
5 |
|
if ($migration->getVersion()->equals($version)) { |
74
|
4 |
|
return $migration; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
throw MigrationNotExecuted::new((string) $version); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|