2017 © Pedro Peláez
 

symfony-bundle binding-bundle

This Bundle provides a binding from array to Entity with Symfony

image

sonofwinter/binding-bundle

This Bundle provides a binding from array to Entity with Symfony

  • Tuesday, July 31, 2018
  • by SonOfWinter
  • Repository
  • 1 Watchers
  • 0 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 100 % Grown

The README.md

BindingBundle

This bundle provides a binding mechanism from array to Entity with Symfony., (*1)

Requirements

  • PHP 8.2 or higher
  • Symfony 7.0 or higher

Installation

Open a command console, enter your project directory and execute:, (*2)

$ composer require sonofwinter/binding-bundle

Register the bundle in your config/bundles.php file:, (*3)

return [
    // ...
    SOW\BindingBundle\SOWBindingBundle::class => ['all' => true],
];

Configuration

Configure the bundle in your config/packages/sow_binding.yaml file:, (*4)

# 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 name
  • type: Used for type checking. A BinderTypeException is thrown if the type doesn't match
  • min/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:, (*5)

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., (*6)

The Versions

31/07 2018

dev-master

9999999-dev

This Bundle provides a binding from array to Entity with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

09/07 2018

v0.3.2

0.3.2.0

This Bundle provides a binding from array to Entity with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

26/06 2018

v0.3.1

0.3.1.0

This Bundle provides a binding from array to Entity with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

07/06 2018

v0.3.0

0.3.0.0

This Bundle provides a binding from array to Entity with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

13/05 2018

v0.2.0

0.2.0.0

This Bundle provides a binding from array to Entity with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

09/04 2018

v0.1.2

0.1.2.0

This Bundle provides a binding from array to Entity with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

09/04 2018

v0.1.1

0.1.1.0

This Bundle provides a binding from array to Entity with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

01/02 2018

v0.1.0

0.1.0.0

This Bundle provides a binding from array to Entity with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc