ArrayDiffFormatter.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Portions taken from phpwiki-1.3.3.
  4. *
  5. * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
  6. * You may copy this code freely under the conditions of the GPL.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. * @ingroup DifferenceEngine
  25. */
  26. /**
  27. * A pseudo-formatter that just passes along the Diff::$edits array
  28. * @ingroup DifferenceEngine
  29. */
  30. class ArrayDiffFormatter extends DiffFormatter {
  31. /**
  32. * @param Diff $diff A Diff object.
  33. *
  34. * @return array[] List of associative arrays, each describing a difference.
  35. * @suppress PhanParamSignatureMismatch
  36. */
  37. public function format( $diff ) {
  38. $oldline = 1;
  39. $newline = 1;
  40. $retval = [];
  41. foreach ( $diff->getEdits() as $edit ) {
  42. switch ( $edit->getType() ) {
  43. case 'add':
  44. foreach ( $edit->getClosing() as $line ) {
  45. $retval[] = [
  46. 'action' => 'add',
  47. 'new' => $line,
  48. 'newline' => $newline++
  49. ];
  50. }
  51. break;
  52. case 'delete':
  53. foreach ( $edit->getOrig() as $line ) {
  54. $retval[] = [
  55. 'action' => 'delete',
  56. 'old' => $line,
  57. 'oldline' => $oldline++,
  58. ];
  59. }
  60. break;
  61. case 'change':
  62. foreach ( $edit->getOrig() as $key => $line ) {
  63. $retval[] = [
  64. 'action' => 'change',
  65. 'old' => $line,
  66. 'new' => $edit->getClosing( $key ),
  67. 'oldline' => $oldline++,
  68. 'newline' => $newline++,
  69. ];
  70. }
  71. break;
  72. case 'copy':
  73. $oldline += count( $edit->getOrig() );
  74. $newline += count( $edit->getOrig() );
  75. }
  76. }
  77. return $retval;
  78. }
  79. }