starfolksoftware/kalibrant

A package to manage setting of your models.

v1.0.3 2023-02-15 13:30 UTC

This package is auto-updated.

Last update: 2024-11-15 17:06:05 UTC


README

Logo Kalibrant

Build Status Build Status Build Status Total Downloads Latest Stable Version License

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.