AvailableMigrationsSet.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Metadata;
  4. use Countable;
  5. use Doctrine\Migrations\Exception\MigrationNotAvailable;
  6. use Doctrine\Migrations\Version\Version;
  7. use function array_values;
  8. use function count;
  9. /**
  10. * Represents a non sorted list of migrations that may or may not be already executed.
  11. */
  12. final class AvailableMigrationsSet implements Countable
  13. {
  14. /** @var AvailableMigration[] */
  15. private $items = [];
  16. /**
  17. * @param AvailableMigration[] $items
  18. */
  19. public function __construct(array $items)
  20. {
  21. $this->items = array_values($items);
  22. }
  23. /**
  24. * @return AvailableMigration[]
  25. */
  26. public function getItems(): array
  27. {
  28. return $this->items;
  29. }
  30. public function count(): int
  31. {
  32. return count($this->items);
  33. }
  34. public function hasMigration(Version $version): bool
  35. {
  36. foreach ($this->items as $migration) {
  37. if ($migration->getVersion()->equals($version)) {
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. public function getMigration(Version $version): AvailableMigration
  44. {
  45. foreach ($this->items as $migration) {
  46. if ($migration->getVersion()->equals($version)) {
  47. return $migration;
  48. }
  49. }
  50. throw MigrationNotAvailable::forVersion($version);
  51. }
  52. }