- How do I generate a unified diff for API responses in Laravel?
- Use the `Differ` class with `RendererFactory` to create a unified renderer. Example: `$diff = (new Differ())->calculate($oldText, $newText); $renderer = (new RendererFactory())->createRenderer('unified'); echo $renderer->render($diff);`. This works seamlessly in Laravel controllers or services.
- Can I use this package to display side-by-side diffs in a Laravel Blade view?
- Yes, render the diff as HTML using the `sideBySide` renderer, then include the CSS via `DiffHelper::getStyleSheet()`. Add the stylesheet to your Blade layout and embed the HTML output directly in your view. Example: `<div>{{ $diffRenderer->render($diff) }}</div>`.
- What Laravel versions does jfcherng/php-diff support?
- The package requires PHP 8.3+, which aligns with Laravel 10+. For older Laravel versions (e.g., 9.x), pin to `jfcherng/php-diff:^6.x` in your `composer.json`, but note some features may not be available. Always test compatibility in your environment.
- How do I handle large diffs (e.g., 1MB+ files) without memory issues?
- Use the `lengthLimit` option in `DifferOptions` to cap input size or process files in chunks. For example: `$options = new DifferOptions(); $options->setLengthLimit(500000); $differ->calculate($oldText, $newText, $options);`. For massive files, consider streaming or splitting logic.
- Is there a built-in way to integrate diffs with Laravel’s Eloquent models?
- No, the package is stateless and requires manual setup. You can create a custom accessor or mutator in your Eloquent model to generate diffs on-the-fly, like: `public function getDiffAttribute() { return (new Differ())->calculate($this->oldValue, $this->newValue); }`.
- How do I customize the HTML diff styling to match my Laravel app’s design?
- Override the default CSS by copying `diff-table.css` from the package’s `example/` directory to your project and modifying it. Alternatively, use `DiffHelper::getStyleSheet()` as a base and extend it with your own CSS. Ensure no conflicts with Tailwind or other frameworks.
- Can I use this package to generate diffs for real-time collaborative editing (e.g., with Livewire)?
- Yes, generate JSON diffs using the `Json` renderer and transmit them to Livewire/Alpine for client-side rendering. Example: `$renderer = (new RendererFactory())->createRenderer('json'); $jsonDiff = $renderer->render($diff);`. Parse this JSON in JavaScript for dynamic updates.
- What happens if the input strings contain non-UTF-8 characters?
- The package relies on PHP’s `ext-iconv`, which handles encoding normalization. If inputs are malformed, set `ignoreLineEnding` or `ignoreWhitespace` in `DifferOptions` to mitigate issues. For robust handling, validate inputs before passing them to the differ.
- How do I test diff generation in PHPUnit?
- Mock the `Differ` and `Renderer` classes to isolate logic. Example: `$mockDiffer = $this->createMock(Differ::class); $mockDiffer->method('calculate')->willReturn($expectedDiff);`. Test edge cases like empty strings, identical inputs, or large payloads to ensure reliability.
- Are there alternatives to jfcherng/php-diff for Laravel diffs?
- Yes, consider `sebastianbergmann/diff` for lightweight needs or `ocramius/package-versions` for semantic versioning diffs. However, `jfcherng/php-diff` stands out for its multi-format support (HTML, JSON, unified) and Laravel-friendly design, making it ideal for complex use cases like CMS or audit logs.