Open a command console, enter your project directory and execute:
$ composer require sonofwinter/binding-bundle
Define binding properties in your entity
/**
* @var string
* @Binding(key="firstname")
*/
private $firstname;
/**
* @var string
* @Binding(key="lastname", setter="setOtherName")
*/
private $lastname;
/**
* @var integer
* @Binding(key="age", type="integer")
*/
private $age;
/**
* @var string
* @Binding()
*/
private $userEmail;
/**
* @var string
* @Binding(name="firstname")
*/
private $firstname;
/**
* @var string
* @Binding(name="lastname", setter="setOtherName")
*/
private $lastname;
/**
* @var integer
* @Binding(name="age", type="integer")
*/
private $age;
/**
* @var string
* @Binding(name="userEmail")
*/
private $userEmail;
You must defined the key|name property. It's the array value's key.
The setter property is used if you want to use another setter.
The type property is used if you want to make a type check. A BinderTypeException is throws if the type doens't correspond.
Use Binder service for bind an array to entity
public function __construct(BinderInterface $binder)
{
$this->binder = $binder;
}
function bind(BindableEntity $be, array $data): BindableEntity
{
// $data = ['lastname' => 'Doe', 'firstname' => 'John', 'age' => 20, 'userEmail' => '[email protected]'];
$this->binder->bind($be, $data);
return $be;
}
public function bind(&$object, array $params = [], array $include = [], array $exclude = [])
$include is a key array required in $params, if one or more keys are missing, an exception is thrown
$exclude is a key array ignored in $params. No exception was thrown is a key is present.
/**
* @var integer
* @Binding(key="age", type="integer", min=0, max=100)
*/
private $age;
The min and max value check if the value is in range defined by the two properties.
If not, a specific exception was thrown
Works with number (int/float), string (length) and array (count)
/**
* @var Test
* @Binding(type="App\Entity\Test")
*/
private $test;
A child entity can be binding when the type is set with the entity namespace.
The getter is use to get the sub entity. If the sub entity is null, it try to create him (without parameter), if fail the binder skip sub entity. So if the constructor need parameters, the sub entity must be defined before the binder action.
Exemple of data :
$data = [
'lastname' => 'Doe',
'firstname' => 'John',
'age' => 20,
'userEmail' => '[email protected]',
'test' => [
'testProps1' => 'value',
'testProps2' => 'value'
]
];
/**
* @var Test
* @Binding(nullable=true)
*/
private $test;
The nullable property define if a null value can be set to entity's property. The property default value is false.
Update Symfony minimum version 4.0 -> 4.1
Update Symfony minimum version 4.1 -> 4.3 || 5.0
Adds attributes and increases minimum versions :
- Symfony minimum version 5.0
- PHP minimum version 8.0
So now, you can use attribute instead of annotation.
#[Binding(key: "lastname", setter: "setLastname", type: "string", min: 2, max: 255)]
private string $lastname = '';
You have to add this configuration to use it :
sow_binding.binding_method: attribute
You can also override Binder attribute with this configuration :
sow_binding.attribute_class_name: 'SOW\BindingBundle\Attribute\Binding'