sucohi/扫把

主要针对Laravel开发的PHP包,用于通过Trait管理选项值。

3.0.3 2019-09-11 09:47 UTC

This package is auto-updated.

Last update: 2024-09-11 20:31:59 UTC


README

主要针对Laravel开发的PHP包,用于通过Trait管理选项值。
(此包由L54维护)

演示

安装

执行composer命令。

composer require sukohi/broom:3.*

准备

简单方法

在您的模型中,设置BroomTrait并添加一个名为"options"的方法,该方法返回您想要的值。

<?php

namespace App;

use Sukohi\Broom\BroomTrait;

class Color {

    use BroomTrait;

    public static function options() {

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

    }

或者,如果您的模型扩展了Illuminate\Database\Eloquent\Model,您可以简单地通过设置$option属性来准备,如下所示。

<?php

namespace App;

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

class Color extends Model {

    use BroomTrait;

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

现在您也可以调用以下方法。

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

用法

选项

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

/* Output

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

*/

注意:当您设置$option时,您将获得Collection

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

或者您可以设置默认值。

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

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

$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 = \App\Color::optionKeys();
print_r($keys);

/* Output

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

*/

随机

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
    )

*/

键随机

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

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

/* Output

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

*/

有键

$key = 3;

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

    echo 'Has key!';

}

有值

$value = 'Red';

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

    echo 'Has value!';

}

带默认标题

$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
        )
)

*/

缓存

$option_cache允许您使用缓存以快速访问。

<?php

namespace App;

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

class Color extends Model {

    use BroomTrait;

    protected $option_cache = true;

然后使用forgetCache()来删除缓存;

自定义方法名称

您还可以使用自定义方法名称,如"redOptions"。;

use Sukohi\Broom\BroomTrait;

class Color {

    use BroomTrait;

    public static function redOptions() {

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

    }

现在您可以调用redOptionValue()、redOptionValues()、redOptionKey()、redOptionKeys()、redOptionRandom()、redOptionKeyRandom()、redOptionHasKey()、redOptionHasValue()和redOptionsWithTitle()。

许可证

本包根据MIT许可证授权。

版权所有2015 Sukohi Kuhoh