leuverink/livewire-property-groups

1.4.1 2024-08-29 00:20 UTC

This package is auto-updated.

Last update: 2024-09-02 12:22:07 UTC


README

codestyle tests

此软件包通过允许将相关属性组织到命名的组中来简化 Livewire 组件中的属性管理、验证和操作。

安装

composer require leuverink/livewire-property-groups

基本用法

use Leuverink\PropertyAttribute\Group;
use Leuverink\PropertyAttribute\WithGroups;

class Form extends Component
{
    use WithGroups;

    #[Group('a')]
    public $foo = 1;

    #[Group('a')]
    public $bar = 2;

    #[Group('b')]
    public $baz = 3;

    public function submit()
    {
        $groupA = $this->group('a'); // ['foo' => 1, 'bar' => 2]
        $groupB = $this->group('b'); // ['baz' => 3]
    }
}

访问组属性

在您的组件或表单对象中,使用 WithGroups 特性来访问 group 方法。

// Get all properties in a group
$this->group('a'); // ['foo' => 1, 'bar' => 2]

// Get property names
$this->group('a')->keys(); // ['foo', 'bar']

// Get property values
$this->group('a')->values(); // [1, 2]

// Iterate over properties
$this->group('a')->each(fn() => /* */);

// Get all grouped properties, excluding non grouped
$this->group();

// Access a group as an array or an object
$this->group('a')['foo'];
$this->group('a')->foo;

代理 Livewire 方法

// Reset properties to initial state
$this->group('a')->reset();

// Return all properties and reset to initial state
$this->group('a')->pull();

// Validate all properties in a group
$this->group('a')->validate();

// Works inside a form object
$this->userForm->group('a')->validate();

处理多个组

// Retrieve properties from multiple groups
$this->group(['a', 'b']);

// Validate multiple groups
$this->group(['a', 'b'])->validate();

调试

// dump group properties
$this->group('a')->dump();

// dd group properties
$this->group('a')->dd();

// dump is chainable
$validated = $this->group('a')
    ->dump()
    ->validate();

冲突的 group 方法签名

我意识到 group 是一个非常通用的方法名,您可能在您自己的组件中也会使用它。您可以通过提供别名来更改方法签名。

use WithGroups {
    group as fooBar;
}

开发

composer lint # run all linters
composer fix # run all fixers

composer analyze # run static analysis
composer baseline # generate static analysis baseline

许可证

此软件包是开源软件,受 MIT 许可证的许可。