illizian/nova-carbon-modifier-field

A field for allowing a User to configure a "Carbon Modifier"

v0.1.4 2020-11-29 22:22 UTC

This package is auto-updated.

Last update: 2024-10-29 06:06:03 UTC


README

Latest Version on Packagist License

Description

A Laravel Nova field for describing a DateTime modification using Carbon methhods.

Demo

Demo

Installation

The package can be installed through Composer.

composer require illizian/nova-carbon-modifier-field

Usage

Add a \Illizian\NovaCarbonModifier\NovaCarbonModifier, field to your fields, like so:

/* ... */
use \Illizian\NovaCarbonModifier\NovaCarbonModifier;
/* ... */

class Example extends Resource
{
    /* ... */
    public function fields(Request $request)
    {
        return [
            /* ... */
            NovaCarbonModifier::make(__('Modifier'), 'modifier'),
        ];
    }

You may also provide a list of popular modifiers, should you wish with the popular(array $modifiers) method:

/* ... */
use \Illizian\NovaSuggestWrapper\NovaSuggestWrapper;
/* ... */

class Example extends Resource
{
    /* ... */
    public function fields(Request $request)
    {
        return [
            /* ... */
            NovaCarbonModifier::make(__('Modifier'), 'modifier')
                ->popular('startOfWeek,hour:10,minute:0', 'subDays:1,hour:13,minute:0'),
        ];
    }

As you can see above, a modifier is stored as a comma-seperated string, it takes the following format:

"<method>:<parameter>"
"<method>:<parameter>,<method>:<parameter>,<method>:<parameter>"
"<method>"
"<method>,<method>,<method>"
"<method>,<method>:<parameter>,<method>:<parameter>,<method>,<method>:<parameter>,<method>"

etc...

The field class provides a static helper for applying this modifier string to a Carbon instance for your convenience:

use \Carbon\Carbon;
use \Illizian\NovaSuggestWrapper\NovaSuggestWrapper;

$date = Carbon::now();
$modifiers = 'startOfWeek,hour:10,minute:0';

$date_modified = NovaCarbonModifier::applyModifier($date, $modifiers);

License

The MIT License (MIT). Please see License File for more information.