Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: namespace SebastianBergmann\Diff\Output; Chris@14: Chris@14: /** Chris@14: * Builds a diff string representation in a loose unified diff format Chris@14: * listing only changes lines. Does not include line numbers. Chris@14: */ Chris@14: final class DiffOnlyOutputBuilder implements DiffOutputBuilderInterface Chris@14: { Chris@14: /** Chris@14: * @var string Chris@14: */ Chris@14: private $header; Chris@14: Chris@14: public function __construct(string $header = "--- Original\n+++ New\n") Chris@14: { Chris@14: $this->header = $header; Chris@14: } Chris@14: Chris@14: public function getDiff(array $diff): string Chris@14: { Chris@14: $buffer = \fopen('php://memory', 'r+b'); Chris@14: Chris@14: if ('' !== $this->header) { Chris@14: \fwrite($buffer, $this->header); Chris@14: if ("\n" !== \substr($this->header, -1, 1)) { Chris@14: \fwrite($buffer, "\n"); Chris@14: } Chris@14: } Chris@14: Chris@14: foreach ($diff as $diffEntry) { Chris@14: if ($diffEntry[1] === 1 /* ADDED */) { Chris@14: \fwrite($buffer, '+' . $diffEntry[0]); Chris@14: } elseif ($diffEntry[1] === 2 /* REMOVED */) { Chris@14: \fwrite($buffer, '-' . $diffEntry[0]); Chris@14: } elseif ($diffEntry[1] === 3 /* WARNING */) { Chris@14: \fwrite($buffer, ' ' . $diffEntry[0]); Chris@14: Chris@14: continue; // Warnings should not be tested for line break, it will always be there Chris@14: } else { /* Not changed (old) 0 */ Chris@14: continue; // we didn't write the non changs line, so do not add a line break either Chris@14: } Chris@14: Chris@14: $lc = \substr($diffEntry[0], -1); Chris@14: if ($lc !== "\n" && $lc !== "\r") { Chris@14: \fwrite($buffer, "\n"); // \No newline at end of file Chris@14: } Chris@14: } Chris@14: Chris@14: $diff = \stream_get_contents($buffer, -1, 0); Chris@14: \fclose($buffer); Chris@14: Chris@14: return $diff; Chris@14: } Chris@14: }