Chunk.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/diff.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Diff;
  11. use ArrayIterator;
  12. use IteratorAggregate;
  13. use Traversable;
  14. /**
  15. * @template-implements IteratorAggregate<int, Line>
  16. */
  17. final class Chunk implements IteratorAggregate
  18. {
  19. private int $start;
  20. private int $startRange;
  21. private int $end;
  22. private int $endRange;
  23. private array $lines;
  24. public function __construct(int $start = 0, int $startRange = 1, int $end = 0, int $endRange = 1, array $lines = [])
  25. {
  26. $this->start = $start;
  27. $this->startRange = $startRange;
  28. $this->end = $end;
  29. $this->endRange = $endRange;
  30. $this->lines = $lines;
  31. }
  32. public function start(): int
  33. {
  34. return $this->start;
  35. }
  36. public function startRange(): int
  37. {
  38. return $this->startRange;
  39. }
  40. public function end(): int
  41. {
  42. return $this->end;
  43. }
  44. public function endRange(): int
  45. {
  46. return $this->endRange;
  47. }
  48. /**
  49. * @psalm-return list<Line>
  50. */
  51. public function lines(): array
  52. {
  53. return $this->lines;
  54. }
  55. /**
  56. * @psalm-param list<Line> $lines
  57. */
  58. public function setLines(array $lines): void
  59. {
  60. foreach ($lines as $line) {
  61. if (!$line instanceof Line) {
  62. throw new InvalidArgumentException;
  63. }
  64. }
  65. $this->lines = $lines;
  66. }
  67. /**
  68. * @deprecated Use start() instead
  69. */
  70. public function getStart(): int
  71. {
  72. return $this->start;
  73. }
  74. /**
  75. * @deprecated Use startRange() instead
  76. */
  77. public function getStartRange(): int
  78. {
  79. return $this->startRange;
  80. }
  81. /**
  82. * @deprecated Use end() instead
  83. */
  84. public function getEnd(): int
  85. {
  86. return $this->end;
  87. }
  88. /**
  89. * @deprecated Use endRange() instead
  90. */
  91. public function getEndRange(): int
  92. {
  93. return $this->endRange;
  94. }
  95. /**
  96. * @psalm-return list<Line>
  97. *
  98. * @deprecated Use lines() instead
  99. */
  100. public function getLines(): array
  101. {
  102. return $this->lines;
  103. }
  104. public function getIterator(): Traversable
  105. {
  106. return new ArrayIterator($this->lines);
  107. }
  108. }