sonofwinter/binding-bundle
最新稳定版本:v1.0.1
Composer 安装命令:
composer require sonofwinter/binding-bundle
包简介
This Bundle provides a binding from array to Entity with Symfony
README 文档
README
This bundle provides a binding mechanism from array to Entity with Symfony.
Requirements
- PHP 8.2 or higher
- Symfony 7.0 or higher
Installation
Open a command console, enter your project directory and execute:
$ composer require sonofwinter/binding-bundle
Register the bundle in your config/bundles.php file:
return [ // ... SOW\BindingBundle\SOWBindingBundle::class => ['all' => true], ];
Configuration
Configure the bundle in your config/packages/sow_binding.yaml file:
# Default configuration sow_binding: # Use attribute binding method (required for PHP 8+) binding_method: attribute # Optional: Override the default attribute class # attribute_class_name: 'SOW\BindingBundle\Attribute\Binding'
Usage
Define binding properties in your entity
#[Binding(key: "lastname", setter: "setLastname", type: "string", min: 2, max: 255)] private string $lastname = ''; #[Binding(key: "firstname")] private string $firstname = ''; #[Binding(key: "age", type: "integer", min: 0, max: 120)] private int $age = 0; #[Binding(key: "user_email", )] private string $userEmail = ''; #[Binding(key: "test", type: "App\Entity\Test", nullable: true)] private ?Test $test = null;
Binding options
key: The array value's key (required)setter: Used if you want to use another setter method nametype: Used for type checking. A BinderTypeException is thrown if the type doesn't matchmin/max: Check if the value is in the defined range (works with numbers, string length, and array count)nullable: Defines if a null value can be set to the entity's property (default: false)
Use Binder service to bind an array to an entity
public function __construct(BinderInterface $binder) { $this->binder = $binder; } function bind(BindableEntity $be, array $data): BindableEntity { // Example data // $data = [ // 'lastname' => 'Doe', // 'firstname' => 'John', // 'age' => 20, // 'userEmail' => 'some.email@mail.com', // 'test' => [ // 'testProps1' => 'value', // 'testProps2' => 'value' // ] // ]; $this->binder->bind($be, $data); // Or with include/exclude options: // $this->binder->bind($be, $data, ['firstname', 'lastname'], ['age']); return $be; }
Advanced binding options
You can use include/exclude arrays to control which properties are bound:
public function bind(&$object, array $params = [], array $include = [], array $exclude = [])
$include: A key array required in$params. If one or more keys are missing, an exception is thrown$exclude: A key array ignored in$params. No exception is thrown if a key is present
Changelog
For version history and detailed changes, see the changelog directory.
统计信息
- 总下载量: 3.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-12