jhesyong / laravel-attribute
创建用于构建选择框、单选框、复选框的属性选项。使用您的属性验证表单。
v1.1.1
2015-11-12 05:13 UTC
Requires
- php: >=5.5.9
- illuminate/support: ~5.0
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-28 18:42:18 UTC
README
创建用于构建选择框、单选框、复选框的属性选项。使用您的属性验证表单。
##安装
Composer
将以下内容添加到 composer.json
文件中,然后执行 composer update
。
"require": {
...
"jhesyong/laravel-attribute": "~1.0"
}
服务提供者
将以下内容添加到应用配置中
'providers' => [
...
Jhesyong\Attribute\AttributeServiceProvider::class,
...
],
外观
将以下内容添加到应用配置中
'aliases' => [
...
'Attr' => Jhesyong\Attribute\Facades\Attr::class,
...
],
创建属性
要将一个类转换为属性,使用 AttributeTrait
并定义 getOptions()
方法。
namespace Acme;
use Jhesyong\Attribute\AttributeTrait;
class MammalAnimal
{
use AttributeTrait;
protected function getOptions()
{
return ['cat' => 'Cat', 'dog' => 'Dog', 'elephant' => 'Elephant'];
}
}
然后,在应用中注册您的属性。您可以在服务提供者的 boot()
方法中执行以下操作。
// Registered as "mammal_animal" by default
$this->app['attr']->register(new \Acme\MammalAnimal);
// To specify the name, pass the name to the register method.
$this->app['attr']->register(new \Acme\MammalAnimal, 'animal');
或者通过外观注册。
Attr::register(new \Acme\MammalAnimal);
用法
您可以使用 AttributeTrait
中的所有公共方法。您还可以定义自己的方法,然后可以通过外观调用它们。外观将方法调用转发到第一个参数指定的属性。其余参数将传递给属性方法。
有键
// return true
Attr::hasKey('mammal_animal, 'cat');
// return false
Attr::hasKey('mammal_animal, 'bird');
标签
// return 'Cat'
Attr::label('mammal_animal', 'cat');
哈希数组
// Useful for building select, radio, checkbox, etc.
// return ['cat' => 'Cat', 'dog' => 'Dog', 'elephant' => 'Elephant']
Attr::hashArray('mammal_animal');
// To add an empty option, pass true as the second argument.
// return ['' => 'Please Select', cat' => 'Cat', 'dog' => 'Dog', 'elephant' => 'Elephant']
Attr::hashArray('mammal_animal', true);
// To customize the empty message, pass it as the third argument.
// return ['' => '---Please Select---', cat' => 'Cat', 'dog' => 'Dog', 'elephant' => 'Elephant']
Attr::hashArray('mammal_animal', true, '---Please Select---');
成对数组
// return [['label' => 'Cat', 'value' => 'cat'], ...];
Attr::pairArray('mammal_animal');
// To add an empty option, pass true as the second argument.
Attr::pairArray('mammal_animal', true);
// To customize the empty message, pass it as the third argument.
Attr::pairArray('mammal_animal', true, '---Please Select---');
键
// return ['cat', 'dog', 'elephant'];
Attr::keys('mammal_animal');
验证
您可以使用 attr
作为规则名称来验证表单输入是否为属性选项。
'animal_type' => 'required|attr:mammal_animal',
上下文
有时您可能希望根据特定条件获取不同的选项。例如,您可能希望根据某些列的值从数据库中获取选项并过滤选项。
您可以将 getOptions()
改为 getOptions($context = null)
并根据 $context
返回不同的选项。例如,
class Fruit
{
use AttributeTrait;
protected function getOptions($context = null)
{
$category = [
'sour' => ['lemon' => 'Lemon', 'grape' => 'Grape', 'kiwi' => 'Kiwi'],
'sweet' => ['banana' => 'Banana', 'apple' => 'Apple'],
];
if (array_key_exists($context, $category)) {
return $category[$context];
}
return array_reduce($category, 'array_merge', []);
}
}
要获取选项,首先传递上下文。上下文仅影响下一个方法调用。
Attr::context('sour')->hashArray('food');
要验证数据,在属性名称后添加上下文。
'sweet_fruit' => 'required|attr:fruit,sweet',