PHP code example of matteoc99 / laravel-preference
1. Go to this page and download the library: Download matteoc99/laravel-preference library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
matteoc99 / laravel-preference example snippets
'db' => [
'connection' => null, //string: the connection name to use
'preferences_table_name' => 'preferences',
'user_preferences_table_name' => 'user_preferences',
],
'xss_cleaning' => true, // clean user input for cross site scripting attacks
'routes' => [
'enabled' => false, // set true to register routes, more on that later
'middlewares' => [
'auth', // general middleware
'user'=> 'verified', // optional, scoped middleware
'user.general'=> 'verified' // optional, scoped & grouped middleware
],
'prefix' => 'preferences',
'groups' => [
//enum class list of preferences
'general'=>General::class
],
'scopes'=> [
// as many preferenceable models as you want
'user' => \Illuminate\Auth\Authenticatable::class
]
]
use Matteoc99\LaravelPreference\Contracts\PreferenceGroup;
enum Preferences :string implements PreferenceGroup
{
case LANGUAGE="language";
case QUALITY="quality";
case CONFIG="configuration";
}
enum General :string implements PreferenceGroup
{
case LANGUAGE="language";
case THEME="theme";
}
use Matteoc99\LaravelPreference\Enums\Cast;
public function up(): void
{
PreferenceBuilder::init(Preferences::LANGUAGE)
->withDefaultValue("en")
->withRule(new InRule("en", "it", "de"))
->create();
// Or
PreferenceBuilder::init(Preferences::LANGUAGE)->create()
// different enums with the same value do not conflict
PreferenceBuilder::init(General::LANGUAGE)->create()
// update
PreferenceBuilder::init(Preferences::LANGUAGE)
->withRule(new InRule("en", "it", "de"))
->updateOrCreate()
// or with casting
PreferenceBuilder::init(Preferences::LANGUAGE, Cast::ENUM)
->withDefaultValue(Language::EN)
->create()
// nullable support
PreferenceBuilder::init(Preferences::LANGUAGE, Cast::ENUM)
->withDefaultValue(null)
->nullable()
->create()
}
public function down(): void
{
PreferenceBuilder::delete(Preferences::LANGUAGE);
}
use Illuminate\Database\Migrations\Migration;use Matteoc99\LaravelPreference\Enums\Cast;use Matteoc99\LaravelPreference\Factory\PreferenceBuilder;use Matteoc99\LaravelPreference\Rules\InRule;
return new class extends Migration {
public function up(): void
{
PreferenceBuilder::initBulk($this->preferences(),
true // nullable for the whole Bulk
);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
PreferenceBuilder::deleteBulk($this->preferences());
}
/**
* Reverse the migrations.
*/
public function preferences(): array
{
return [
['name' => Preferences::LANGUAGE, 'cast' => Cast::STRING, 'default_value' => 'en', 'rule' => new InRule("en", "it", "de")],
['name' => Preferences::THEME, 'cast' => Cast::STRING, 'default_value' => 'light'],
['name' => Preferences::CONFIGURATION, 'cast' => Cast::ARRAY],
['name' => Preferences::CONFIGURATION,
'nullable' => true // or nullable for only one configuration
],
// or an array of initialized single-mode builders
PreferenceBuilder::init(Preferences::LANGUAGE)->withRule(new InRule("en", "it", "de")),
PreferenceBuilder::init(Preferences::THEME)->withRule(new InRule("light", "dark"))
//mixing both in one array is also possible
];
}
};
// quickly build a nullable Array preference
PreferenceBuilder::buildArray(VideoPreferences::CONFIG);
PreferenceBuilder::buildString(VideoPreferences::LANGUAGE);
use Matteoc99\LaravelPreference\Contracts\PreferenceableModel;
use Matteoc99\LaravelPreference\Enums\PolicyAction;
use Matteoc99\LaravelPreference\Traits\HasPreferences;
class User extends \Illuminate\Foundation\Auth\User implements PreferenceableModel
{
use HasPreferences;
protected $fillable = ['email'];
public function isUserAuthorized(?Authenticatable $user, PolicyAction $action): bool
{
return $user?->id == $this->id ;
}
}
$user->setPreference(Preferences::LANGUAGE,"de");
$user->getPreference(Preferences::LANGUAGE); // 'de' as string
$user->setPreference(Preferences::LANGUAGE,"fr");
// ValidationException because of the rule: ->withRule(new InRule("en","it","de"))
$user->setPreference(Preferences::LANGUAGE,2);
// ValidationException because of the cast: Cast::STRING
$user->removePreference(Preferences::LANGUAGE);
$user->getPreference(Preferences::LANGUAGE); // 'en' as string
// get all of type Preferences,
$user->getPreferences(Preferences::class)
// or of type general
$user->getPreferences(General::class)
//or all
$user->getPreferences(): Collection of UserPreferences
// removes all preferences set for tht user
$user->removeAllPreferences();
enum Theme
{
case LIGHT;
case DARK;
}
curl -X PATCH 'https://your.domain/custom_prefix/user/{scope_id}/general/{preference}' \
-d '{"value": "DARK"}'
'middlewares' => [
'web', // middleware which gets applied to all routes
'user'=> 'verified', // scoped middleware only for user routes should you have other preferencable models
'user.general'=> 'verified' // scoped & grouped middleware only for a specific model + enum
],