2017 © Pedro Peláez
 

library broom

A PHP package mainly developed for Laravel to manage option values using Trait.

image

sukohi/broom

A PHP package mainly developed for Laravel to manage option values using Trait.

  • Thursday, October 5, 2017
  • by Sukohi
  • Repository
  • 1 Watchers
  • 7 Stars
  • 320 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 18 Versions
  • 0 % Grown

The README.md

Broom

A PHP package mainly developed for Laravel to manage option values using Trait.
(This packages is maintained under L54), (*1)

Demo, (*2)

Installation

Execute composer command., (*3)

composer require sukohi/broom:3.*

Preparation

Simple Way, (*4)

In your model, set BroomTrait and add a method named "options" which return values you want., (*5)

<?php

namespace App;

use Sukohi\Broom\BroomTrait;

class Color {

    use BroomTrait;

    public static function options() {

        return [
            '1' => 'Red',
            '2' => 'Blue',
            '3' => 'Green'
        ];

    }

Or you can simply prepare by setting $option property like so if your model extends Illuminate\Database\Eloquent\Model., (*6)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Sukohi\Broom\BroomTrait;

class Color extends Model {

    use BroomTrait;

    protected $option = ['id' => 'name']; // or 'name';

Now you also can call the next methods., (*7)

  • optionValue()
  • optionValues()
  • optionKey()
  • optionKeys()
  • optionRandom()
  • optionKeyRandom()
  • optionHasKey()
  • optionHasValue()
  • optionsWithTitle()

Usage

Options

$colors = \App\Color::options();
print_r($colors);

/* Output

    Array
    (
        [1] => Red
        [2] => Blue
        [3] => Green
    )

*/

Note: You will get Collection when you set $option., (*8)

Value

echo \App\Color::optionValue(2);   // Blue

or You can set default value., (*9)

echo \App\Color::optionValue(5, 'Default');   // Default

Key

echo \App\Color::optionKey('Green');      // 3

Values

$values = \App\Color::optionValues();
print_r($values);

/* Output

    Array
    (
        [0] => Red
        [1] => Blue
        [2] => Green
    )

*/

You also can filter the values by setting ids.

$values = \App\Color::optionValues([2, 3]);
print_r($values);

/* Output

    Array
    (
        [0] => Blue
        [1] => Green
    )

*/

Keys

$keys = \App\Color::optionKeys();
print_r($keys);

/* Output

    Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
    )

*/

Random

echo \App\Color::optionRandom();     // Blue

$options = \App\Color::optionRandom(2);  // If you set a numeric argument, you can get array values.
print_r($options);

/* Output

    Array
    (
        [0] => Blue
        [1] => Green
    )

*/

Key Random

echo \App\Color::optionKeyRandom();  // 2

$options = \App\Color::optionKeyRandom(2);
print_r($options);

/* Output

Array
(
    [0] => 1
    [1] => 3
)

*/

Has Key

$key = 3;

if(\App\Color::optionHasKey($key)) {

    echo 'Has key!';

}

Has Value

$value = 'Red';

if(\App\Color::optionHasValue($value)) {

    echo 'Has value!';

}

With Default Title

$options = \App\Color::optionsWithTitle('Pick one');

/* Output

Array
(
    [] => Pick one
    [1] => Red
    [2] => Blue
    [3] => Green
)

*/


$options = \App\Color::optionsWithTitle('Pick one', '-99');

/* Output

Array
(
    [-99] => Pick one
    [1] => Red
    [2] => Blue
    [3] => Green
)

*/

optionIs

$key = '1';

if(\App\Item::optionIs('Item - 1', $key)) {

    echo 'True!';

}

optionsList

$list = \App\Item::optionsList();   // Default: $id_key => 'id', $value_key => 'text'

// or 

$id_key = 'id';
$value_key = 'text';
$list = \App\Item::optionsList($id_key, $value_key);

/*

Array
(
    [0] => Array
        (
            [id] => 1
            [text] => Red
        )

    [1] => Array
        (
            [id] => 2
            [text] => Blue
        )

    [2] => Array
        (
            [id] => 3
            [text] => Green
        )
)

*/

Cache

$option_cache allows you to use cache for quick access., (*10)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Sukohi\Broom\BroomTrait;

class Color extends Model {

    use BroomTrait;

    protected $option_cache = true;

then use forgetCache() for deleting cache;, (*11)

Customized Method Name

You also can use customized method name like "redOptions".;, (*12)

use Sukohi\Broom\BroomTrait;

class Color {

    use BroomTrait;

    public static function redOptions() {

        return [
            '1' => 'cherry',
            '2' => 'rose',
            '3' => 'crimson'
        ];

    }

Now you can call redOptionValue(), redOptionValues(), redOptionKey(), redOptionKeys(), redOptionRandom(), redOptionKeyRandom(), redOptionHasKey(), redOptionHasValue() and redOptionsWithTitle()., (*13)

License

This package is licensed under the MIT License., (*14)

Copyright 2015 Sukohi Kuhoh, (*15)

The Versions

05/10 2017

dev-master

9999999-dev

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

05/10 2017

3.0.x-dev

3.0.9999999.9999999-dev

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

05/10 2017

3.0.2

3.0.2.0

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

05/10 2017

3.0.1

3.0.1.0

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

01/07 2017

3.0.0

3.0.0.0

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

25/09 2016

2.0.x-dev

2.0.9999999.9999999-dev

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

25/09 2016

2.0.8

2.0.8.0

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

25/09 2016

2.0.7

2.0.7.0

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

07/07 2016

2.0.6

2.0.6.0

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

31/05 2016

1.0.x-dev

1.0.9999999.9999999-dev

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

31/05 2016

2.0.5

2.0.5.0

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

31/05 2016

2.0.4

2.0.4.0

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

31/05 2016

1.0.1

1.0.1.0

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

21/04 2016

2.0.3

2.0.3.0

A PHP package mainly developed for Laravel to manage option values using Trait.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

06/08 2015

2.0.2

2.0.2.0

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

06/08 2015

2.0.1

2.0.1.0

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

06/08 2015

2.0.0

2.0.0.0

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

06/08 2015

1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi