vpremiss / arabicable
Advanced techniques for handling Arabic text in Laravel apps.
Fund package maintenance!
VPremiss
Requires
- php: ^8.2
- caneara/quest: ^4.0
- illuminate/contracts: ^11.0
- spatie/laravel-package-tools: ^1.16
- vpremiss/crafty: ^4.14.0
Requires (Dev)
- larastan/larastan: ^2.9
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^9.0.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- dev-main
- v3.5.0
- v3.4.0
- v3.3.3
- v3.3.2
- v3.3.1
- v3.2.0
- v3.1.1
- v3.1.0
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.1
- v2.0.0
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-dependabot/composer/orchestra/testbench-9.3.0
- dev-dependabot/composer/nunomaduro/collision-8.4.0
- dev-dependabot/composer/pestphp/pest-2.35.0
- dev-dependabot/composer/larastan/larastan-2.9.8
- dev-dependabot/github_actions/codecov/codecov-action-4.5.0
- dev-dependabot/composer/phpstan/extension-installer-1.4.1
- dev-dev
This package is auto-updated.
Last update: 2025-01-02 18:15:46 UTC
README
بسم الله الرحمن الرحيمArabicable
Several effective strategies for managing Arabic text.
Description
The primary approach here is to rely on the database to store variances of each Arabic or Indian numeral column. Having their dedicated columns makes indexing and searching efficient; combined with the appropriate choice among all available migration types based on character capactiy. And while utilizing Laravel's Eloquent observers, we can process only what's necessary again during updates.
So based on the model's property length requirement, we've added migration blueprint macros for Arabic string
, tinyText
, text
, mediumText
, and longText
; plus a date
one for Indian numerals. Using those string
or text
-type ones would generate 2 extra columns for each arabic column (a configurable affix, that is). And the indian date
one would generate a column_indian
to hold that in; where column
is an example property name.
Finally, take a look at the list of offered methods below (the API section) to understand what kind of processing we're doing on the text in order to essentially preserve the column
(without harakat), the column_with_harakat
, and the column_searchable
where each is prepared exactly for that...
Installation
-
Install the package via composer:
composer require vpremiss/arabicable
-
Run the package Artisan installer using this command:
php artisan arabicable:install
[!NOTE]The config file as well as the migration table will be published automatically.
Upgrading
-
Backup your current config, as well as the common-Arabic-text migration and seeder.
-
Republish the package stuff using these Artisan commands:
php artisan vendor:publish --tag="arabicable-config" --force php artisan vendor:publish --tag="arabicable-migrations" --force php artisan vendor:publish --tag="arabicable-seeders" --force
-
Migrate and seed gracefully again on your end; keeping in mind that seeders do change regularily.
Usage
Arabicable
Alright, so let's imagine we have Note
(s) and we want to have their content
arabicable!
-
First, create the migration and add an arabicable
column
to it:use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; // ... Schema::create('notes', function (Blueprint $table) { $table->id(); $table->arabicText('content'); // this also creates `content_searchable` and `content_with_harakat` of the same type $table->timestamps(); });
-
Then let's make the model "arabicable", which activates the observer:
use Illuminate\Database\Eloquent\Model; use VPremiss\Arabicable\Traits\Arabicable; class Note extends Model { use Arabicable; protected $fillable = ['content']; // or you'd guard the property differently }
-
Finally, the moment we create a new note and passing it some Arabic content (presumably with harakat), it will process its other columns automatically:
// When spacing_after_punctuation_only is set to `false` in configuration (default) $note = Note::create([ 'content' => '"الجَمَاعَةُ مَا وَافَقَ الحَقّ، أَنْتَ الجَمَاعَةُ وَلَو كُنْتَ وَحْدَكْ."', ]); echo $note->content; // "الجماعة ما وافق الحق ، أنت الجماعة ولو كنت وحدك ." echo $note->{ar_with_harakat('content')}; // "الجَمَاعَةُ مَا وَافَقَ الحَقّ ، أَنْتَ الجَمَاعَةُ وَلَو كُنْتَ وَحْدَكْ ." echo $note->{ar_searchable('content')}; // "الجماعة ما وافق الحق انت الجماعة ولو كنت وحدك" // When spacing_after_punctuation_only is set to `true` in configuration $seriousContentPoliticiansDoNotLike = <<<Arabic - قال المُزني: سألتُ الشافعي عن مسألة في "الكلام"، فقال: سَلني عن شيء إذا أخطأتَ فيه قُلتُ "أخطأتَ!"، ولا تسألني عن شيء إذا أخطأتَ فيه قُلتُ "كفرتَ". Arabic; $note->update(['content' => $seriousContentPoliticiansDoNotLike]); echo $note->content; // - قال المزني: سألت الشافعي عن مسألة في "الكلام"، فقال: سلني عن شيء إذا أخطأت فيه قلت "أخطأت!"، ولا تسألني عن شيء إذا أخطأت فيه قلت "كفرت". echo $note->{ar_with_harakat('content')}; // - قال المُزني: سألتُ الشافعي عن مسألة في "الكلام"، فقال: سَلني عن شيء إذا أخطأتَ فيه قُلتُ "أخطأتَ!"، ولا تسألني عن شيء إذا أخطأتَ فيه قُلتُ "كفرتَ". echo $note->{ar_searchable('content')}; // قال المزني سالت الشافعي عن مسالة في الكلام فقال سلني عن شيء اذا اخطات فيه قلت اخطات ولا تسالني عن شيء اذا اخطات فيه قلت كفرت
[!NOTE]
Notice how we can use the global helper functions (ar_with_harakat
,ar_searchable
, andar_indian
) to get the corresponding property name quickly.
[!IMPORTANT]
A validation method is employed during text processing to ensure that the text is free of punctuation anomalies that could impact spacing adjustments.
Common Arabic Text
You must ensure that the migration, model, factory, and seeder are all set in place in order for this feature to be utilized.
Among many other filtering methods that Arabic
facade provides, there is a removeCommons
one. Use it to filter those out to help you search for more focused context.
You can combine that with whole filtered ArabicFilter::forSearch
searches ahead to ensure that you didn't miss the quote itself first, and so on...
API
-
Here is a table of all the available custom migration blueprint macro columns:
-
And keep in mind the following:
- Each can be passed an
$isNullable
boolean argument, which affects all columns. - Each can be passed an
$isUnique
boolean argument, which affects the original column. arabicString
can be passed a$length
integer argument.- Both
arabicString
andarabicTinyText
can be passed a$supportsFullSearch
argument, affecting their 'searchable' column. - Finally
arabicText
,arabicMediumText
, andarabicLongText
all do have full-text search index set on their 'searchable' column.
- Each can be passed an
-
-
Below are the tables of all the
Arabicable
package helpers:
Package Development
- Change the
localTimezone
to yours in the [TestCase
] file.
Changelogs
You can check out the package's changelogs online via WTD.
Progress
You can also checkout the project's roadmap among others in the organization's dedicated section for projects.
Support
Support ongoing package maintenance as well as the development of other projects through sponsorship or one-time donations if you prefer.
And may Allah accept your strive; aameen.
License
This package is open-sourced software licensed under the MIT license.
Credits
- ChatGPT
- Graphite
- Laravel
- Spatie
- BeyondCode
- The Contributors
- All the backend packages and services this package relies on...
- And the generous individuals that we've learned from and been supported by throughout our journey...
Inspiration
- LinuxScout
emohamed@umail.iu.edu
for the original plurals list- Quest package
- AR-PHP package
- AR-PHP-Laravel package
والحمد لله رب العالمين