ArrayDiffFormatter.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. */
  36. public function format( $diff ) {
  37. $oldline = 1;
  38. $newline = 1;
  39. $retval = [];
  40. foreach ( $diff->getEdits() as $edit ) {
  41. switch ( $edit->getType() ) {
  42. case 'add':
  43. foreach ( $edit->getClosing() as $line ) {
  44. $retval[] = [
  45. 'action' => 'add',
  46. 'new' => $line,
  47. 'newline' => $newline++
  48. ];
  49. }
  50. break;
  51. case 'delete':
  52. foreach ( $edit->getOrig() as $line ) {
  53. $retval[] = [
  54. 'action' => 'delete',
  55. 'old' => $line,
  56. 'oldline' => $oldline++,
  57. ];
  58. }
  59. break;
  60. case 'change':
  61. foreach ( $edit->getOrig() as $key => $line ) {
  62. $retval[] = [
  63. 'action' => 'change',
  64. 'old' => $line,
  65. 'new' => $edit->getClosing( $key ),
  66. 'oldline' => $oldline++,
  67. 'newline' => $newline++,
  68. ];
  69. }
  70. break;
  71. case 'copy':
  72. $oldline += count( $edit->getOrig() );
  73. $newline += count( $edit->getOrig() );
  74. }
  75. }
  76. return $retval;
  77. }
  78. }