- How do I install this polyfill in a Laravel project?
- Run `composer require symfony/polyfill-intl-grapheme` in your project directory. No additional Laravel-specific setup is required—it works as a standalone PHP library. For Laravel 11+, ensure your PHP version is 8.6+ to leverage native `grapheme_strrev()` without the polyfill.
- Does this package support Laravel’s Str helper for grapheme-aware operations?
- Yes. You can extend Laravel’s Str facade with macros like `Str::macro('graphemeReverse', fn($str) => grapheme_strrev($str));` to enable fluent syntax. This integrates seamlessly with Laravel’s existing string manipulation ecosystem.
- What Laravel versions and PHP versions are supported?
- This polyfill works with any Laravel version but requires PHP 7.2+. For PHP 8.6+, the native `grapheme_strrev()` function is preferred—use this polyfill only if the `intl` extension is unavailable. Laravel 11+ projects benefit most from PHP 8.6+ for native support.
- How does this handle RTL (right-to-left) languages like Arabic or Hebrew?
- The polyfill accurately processes grapheme clusters, including RTL scripts and combining characters (e.g., Arabic diacritics). Use `grapheme_strrev()` for correct text reversal in RTL contexts, or integrate with Laravel Blade directives like `@reverseGraphemes` for dynamic RTL rendering.
- Can I use this for emoji or complex Unicode sequences (e.g., 👨👩👧👦) in Laravel validation?
- Absolutely. The polyfill’s `grapheme_strlen()` and `grapheme_substr()` functions ensure emoji sequences and combining marks are treated as single units. Add a custom validation rule (e.g., `palindrome_graphemes`) to Laravel’s validator for emoji/Unicode-aware checks.
- What’s the performance impact compared to native `intl` extension or `strrev()`?
- The polyfill adds minimal overhead—benchmarks show it’s ~10-20% slower than the native `intl` extension but significantly faster than `strrev()` for complex Unicode. Use it only when `intl` is unavailable; for ASCII-only text, `strrev()` is sufficient.
- How do I fall back to the `intl` extension if available?
- Check for the `intl` extension at runtime: `if (extension_loaded('intl')) { use native grapheme functions; } else { require symfony/polyfill-intl-grapheme; }`. Laravel’s service container can automate this via a binding for `GraphemeHandlerInterface`.
- Will this break existing Laravel apps using `strrev()`?
- No. The polyfill only adds grapheme-aware functions (e.g., `grapheme_strrev()`). Existing `strrev()` calls remain unchanged. For backward compatibility, use a wrapper like `function grapheme_strrev_fallback($str) { return function_exists('grapheme_strrev') ? grapheme_strrev($str) : strrev($str); }`.
- Are there alternatives to this polyfill for Laravel?
- Yes. For PHP < 8.6, use the native `intl` extension if available. For minimalist needs, `mb_strrev()` (with `mbstring` extension) handles basic RTL cases but lacks grapheme cluster precision. This polyfill is the most Laravel-friendly option for full Unicode compliance without `intl`.
- How do I test this in a Laravel project for RTL/emoji support?
- Test with strings like `'👨👩👧👦'` (emoji family) or `'مرحبا'` (Arabic). Verify `grapheme_strlen()` returns 1 for the emoji and `grapheme_strrev()` correctly reverses RTL text. Use Laravel’s `Assert` methods: `assertEquals(1, grapheme_strlen('👨👩👧👦'));`.