MappedDiff.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Drupal\Component\Diff;
  3. /**
  4. * FIXME: bad name.
  5. * @todo document
  6. * @private
  7. * @subpackage DifferenceEngine
  8. */
  9. class MappedDiff extends Diff {
  10. /**
  11. * Constructor.
  12. *
  13. * Computes diff between sequences of strings.
  14. *
  15. * This can be used to compute things like
  16. * case-insensitive diffs, or diffs which ignore
  17. * changes in white-space.
  18. *
  19. * @param array $from_lines
  20. * An array of strings.
  21. * (Typically these are lines from a file.)
  22. * @param array $to_lines
  23. * An array of strings.
  24. * @param array $mapped_from_lines
  25. * This array should have the same size number of elements as $from_lines.
  26. * The elements in $mapped_from_lines and $mapped_to_lines are what is
  27. * actually compared when computing the diff.
  28. * @param array $mapped_to_lines
  29. * This array should have the same number of elements as $to_lines.
  30. */
  31. public function __construct($from_lines, $to_lines, $mapped_from_lines, $mapped_to_lines) {
  32. assert(sizeof($from_lines) == sizeof($mapped_from_lines));
  33. assert(sizeof($to_lines) == sizeof($mapped_to_lines));
  34. parent::__construct($mapped_from_lines, $mapped_to_lines);
  35. $xi = $yi = 0;
  36. for ($i = 0; $i < sizeof($this->edits); $i++) {
  37. $orig = &$this->edits[$i]->orig;
  38. if (is_array($orig)) {
  39. $orig = array_slice($from_lines, $xi, sizeof($orig));
  40. $xi += sizeof($orig);
  41. }
  42. $closing = &$this->edits[$i]->closing;
  43. if (is_array($closing)) {
  44. $closing = array_slice($to_lines, $yi, sizeof($closing));
  45. $yi += sizeof($closing);
  46. }
  47. }
  48. }
  49. }