Diff.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, Chunk>
  16. */
  17. final class Diff implements IteratorAggregate
  18. {
  19. /**
  20. * @psalm-var non-empty-string
  21. */
  22. private string $from;
  23. /**
  24. * @psalm-var non-empty-string
  25. */
  26. private string $to;
  27. /**
  28. * @psalm-var list<Chunk>
  29. */
  30. private array $chunks;
  31. /**
  32. * @psalm-param non-empty-string $from
  33. * @psalm-param non-empty-string $to
  34. * @psalm-param list<Chunk> $chunks
  35. */
  36. public function __construct(string $from, string $to, array $chunks = [])
  37. {
  38. $this->from = $from;
  39. $this->to = $to;
  40. $this->chunks = $chunks;
  41. }
  42. /**
  43. * @psalm-return non-empty-string
  44. */
  45. public function from(): string
  46. {
  47. return $this->from;
  48. }
  49. /**
  50. * @psalm-return non-empty-string
  51. */
  52. public function to(): string
  53. {
  54. return $this->to;
  55. }
  56. /**
  57. * @psalm-return list<Chunk>
  58. */
  59. public function chunks(): array
  60. {
  61. return $this->chunks;
  62. }
  63. /**
  64. * @psalm-param list<Chunk> $chunks
  65. */
  66. public function setChunks(array $chunks): void
  67. {
  68. $this->chunks = $chunks;
  69. }
  70. /**
  71. * @psalm-return non-empty-string
  72. *
  73. * @deprecated
  74. */
  75. public function getFrom(): string
  76. {
  77. return $this->from;
  78. }
  79. /**
  80. * @psalm-return non-empty-string
  81. *
  82. * @deprecated
  83. */
  84. public function getTo(): string
  85. {
  86. return $this->to;
  87. }
  88. /**
  89. * @psalm-return list<Chunk>
  90. *
  91. * @deprecated
  92. */
  93. public function getChunks(): array
  94. {
  95. return $this->chunks;
  96. }
  97. public function getIterator(): Traversable
  98. {
  99. return new ArrayIterator($this->chunks);
  100. }
  101. }