CoreFiles by mySites.guru
JsonText.php78 lines, 1,823 bytes, from Joomla_5.4.10-Stable-Full_Package.zip
Raw History Compare with 5.4.9
md5
dbaeca680821d7ed143b379ae285060a
sha1
d4834db3571def6cb73835d780267e95f2315f15
sha256
9ab80efcbc0d6fe41e6b5120b3a3bcf7d5015422e9d04ef8f850b3351d77a69f
<?php

declare(strict_types=1);

namespace Jfcherng\Diff\Renderer\Text;

use Jfcherng\Diff\Differ;
use Jfcherng\Diff\SequenceMatcher;

/**
 * Plain text Json diff generator.
 */
final class JsonText extends AbstractText
{
    /**
     * {@inheritdoc}
     */
    public const INFO = [
        'desc' => 'Text JSON',
        'type' => 'Text',
    ];

    protected function renderWorker(Differ $differ): string
    {
        $ret = [];

        foreach ($differ->getGroupedOpcodes() as $hunk) {
            $ret[] = $this->renderHunk($differ, $hunk);
        }

        if ($this->options['outputTagAsString']) {
            $this->convertTagToString($ret);
        }

        return json_encode($ret, $this->options['jsonEncodeFlags']);
    }

    /**
     * Render the hunk.
     *
     * @param Differ  $differ the differ object
     * @param int[][] $hunk   the hunk
     */
    protected function renderHunk(Differ $differ, array $hunk): array
    {
        $ret = [];

        foreach ($hunk as [$op, $i1, $i2, $j1, $j2]) {
            $ret[] = [
                'tag' => $op,
                'old' => [
                    'offset' => $i1,
                    'lines' => $differ->getOld($i1, $i2),
                ],
                'new' => [
                    'offset' => $j1,
                    'lines' => $differ->getNew($j1, $j2),
                ],
            ];
        }

        return $ret;
    }

    /**
     * Convert tags of changes to their string form for better readability.
     *
     * @param array[][] $changes the changes
     */
    protected function convertTagToString(array &$changes): void
    {
        foreach ($changes as &$hunks) {
            foreach ($hunks as &$block) {
                $block['tag'] = SequenceMatcher::opIntToStr($block['tag']);
            }
        }
    }
}

The same content is in 135 releases, Joomla 5.1.0-beta2 to 6.3.0-alpha1. See them all, or every version of this file.