starfolksoftware / kalibrant
A package to manage setting of your models.
Installs: 1 352
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- spatie/laravel-package-tools: ^1.9.2
- symfony/options-resolver: ^6.0
Requires (Dev)
- nunomaduro/collision: ^6.0|^7.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0|^8.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
README
Introduction
For your laravel 9.x applications, Kalibrant
is a package that provides a simple way to manage your models settings. It is a simple way to manage your user and team models settings.
Installation
You can install the package via composer:
composer require starfolksoftware/kalibrant
You can publish and run the migrations with:
php artisan vendor:publish --tag="kalibrant-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="kalibrant-config"
This is the contents of the published config file:
return [ /** * Define all the settings groups. */ 'groups' => [ // 'setting-group' => SettingGroup::class, ], 'middleware' => ['web'], ];
Usage
Add the HasSetting
trait to your model:
<?php namespace App\Models; use StarfolkSoftware\Kalibrant\HasSettings; class User extends Authenticatable { use HasSetting; }
You can create a new setting file by running the following command from the terminal:
php artisan make:setting AutopilotSettings
This is will create a new file in the /app/Settings/
directory. Here is a sample file:
<?php namespace App\Settings; use App\Models\User; use StarfolkSoftware\Kalibrant\Settings; use Symfony\Component\OptionsResolver\OptionsResolver; class AutopilotSettings extends Settings { /** * The route to redirect to after update. * * @var mixed */ public $redirectRoute = 'profile.show'; /** * Constructor. * * @param mixed $id * @return void */ public function __construct( protected $id ) { parent::__construct(); } /** * Configure the settings attributes * * @param OptionsResolver $resolver * * @return void */ public function configureAttributes(OptionsResolver $resolver) { $resolver->define('enabled') ->default(false) ->allowedTypes('boolean') ->info('Whether autopilot is enabled'); $resolver->define('channels') ->default(['twitter']) ->allowedTypes('array') ->info('The channels to autopilot'); $resolver->define('tweet_freq') ->default(60) ->allowedTypes('integer', 'string') ->info('Tweets count every hour.'); $resolver->define('retweet_freq') ->default(15) ->allowedTypes('integer', 'string') ->info('Number of retweets in an hour'); $resolver->define('like_freq') ->default(15) ->allowedTypes('integer', 'string') ->info('Number of likes in an hour'); $resolver->define('follow_freq') ->default(15) ->allowedTypes('integer', 'string') ->info('Number of follows in an hour'); $resolver->define('hashtags') ->default(['#programming', '#dev']) ->allowedTypes('array') ->info('Relavant hashtags.'); } /** * Returns the setable type * * @return string */ public static function setableType() { return User::class; } /** * Returns the setable id * * @return int */ public function setableId() { return $this->id; } /** * Return the settings group * * @return string */ public static function group() { return 'autopilot-settings'; } /** * Validation rules. * * @return array */ public function rules(): array { return [ 'enabled' => ['required', 'boolean'], 'channels' => ['required', 'array'], 'channels.*' => ['required', 'string', 'in:twitter,facebook,instagram,linkedin'], 'tweet_freq' => ['required', 'integer', 'max:12'], 'retweet_freq' => ['required', 'integer', 'max:12'], 'like_freq' => ['required', 'integer', 'max:12'], 'follow_freq' => ['required', 'integer', 'max:12'], 'hashtags' => ['required', 'array'], 'hashtags.*' => ['required', 'string'], ]; } }
To update the settings, make a PUT
request to route('settings.update', ['group' => '..', 'id' => '...'])
with the defined attributes as data. A sample request to update the above settings:
<?php $response = $this->putJson(route('settings.update', ['group' => 'autopilot-settings', 'id' => $user->id]), [ 'enabled' => true, 'channels' => ['twitter', 'facebook'], 'tweet_freq' => '60', 'retweet_freq' => '15', 'like_freq' => '15', 'follow_freq' => '15', 'hashtags' => ['#programming', '#dev'], ]);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.