xref: /web-bugs/src/Horde/Text/Diff/Mapped.php (revision e3c4b0ac)
1<?php
2/**
3 * Copyright 2007-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you did
6 * not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @author   Geoffrey T. Dairiki <dairiki@dairiki.org>
9 * @category Horde
10 * @license  http://www.horde.org/licenses/lgpl21 LGPL-2.1
11 * @package  Text_Diff
12 */
13
14/**
15 * This can be used to compute things like case-insensitve diffs, or diffs
16 * which ignore changes in white-space.
17 *
18 * @author    Geoffrey T. Dairiki <dairiki@dairiki.org>
19 * @category  Horde
20 * @copyright 2007-2017 Horde LLC
21 * @license   http://www.horde.org/licenses/lgpl21 LGPL-2.1
22 * @package   Text_Diff
23 */
24class Horde_Text_Diff_Mapped extends Horde_Text_Diff
25{
26    /**
27     * Computes a diff between sequences of strings.
28     *
29     * @param string $engine  Name of the diffing engine to use.  'auto' will
30     *                        automatically select the best.
31     * @param array $params   Parameters to pass to the diffing engine:
32     *                        - Two arrays, each containing the lines from a
33     *                          file.
34     *                        - Two arrays with the same size as the first
35     *                          parameters. The elements are what is actually
36     *                          compared when computing the diff.
37     */
38    public function __construct($engine, $params)
39    {
40        list($from_lines, $to_lines, $mapped_from_lines, $mapped_to_lines) = $params;
41        assert(count($from_lines) == count($mapped_from_lines));
42        assert(count($to_lines) == count($mapped_to_lines));
43
44        parent::__construct($engine, array($mapped_from_lines, $mapped_to_lines));
45
46        $xi = $yi = 0;
47        for ($i = 0; $i < count($this->_edits); $i++) {
48            $orig = &$this->_edits[$i]->orig;
49            if (is_array($orig)) {
50                $orig = array_slice($from_lines, $xi, count($orig));
51                $xi += count($orig);
52            }
53
54            $final = &$this->_edits[$i]->final;
55            if (is_array($final)) {
56                $final = array_slice($to_lines, $yi, count($final));
57                $yi += count($final);
58            }
59        }
60    }
61}
62