n2n Bind
Every value object has a #[Marshal] and an #[Unmarshal] mapper. Use them with Mappers::unmarshal() and Mappers::marshal().
Unmarshal
The input is normalized and validated, then converted into a value object.
use n2n\bind\build\impl\Bind;
use n2n\bind\mapper\impl\Mappers;
use valobj\string\Email;
use valobj\string\Name;
use valobj\int\NbId;
$result = Bind::attrs(['email' => ' Info@N2N.rocks ', 'name' => 'Hans', 'id' => '5'])
->prop('email', Mappers::unmarshal(Email::class))
->prop('name', Mappers::unmarshal(Name::class))
->prop('id', Mappers::unmarshal(NbId::class))
->toArray()
->exec();
var_dump($result->get());
Output:
array(3) {
["email"]=>
object(valobj\string\Email)#70 (1) {
["value":protected]=>
string(14) "info@n2n.rocks"
}
["name"]=>
object(valobj\string\Name)#71 (1) {
["value":protected]=>
string(4) "Hans"
}
["id"]=>
object(valobj\int\NbId)#76 (1) {
["value":protected]=>
int(5)
}
}
Validation errors
Invalid input results in error messages, not exceptions:
$result = Bind::attrs(['email' => 'no-email', 'name' => str_repeat('a', 81), 'id' => '0'])
->prop('email', Mappers::unmarshal(Email::class))
->prop('name', Mappers::unmarshal(Name::class))
->prop('id', Mappers::unmarshal(NbId::class))
->toArray()
->exec();
$result->isValid(); // false
$result->getErrorMap()->getChild('email')->isEmpty(); // false
Error messages:
| Property | Message |
|---|---|
Email | |
| name | Maxlength [maxlength = 80] |
| id | Min [min = 1] |
Mandatory values
Mappers::unmarshal() keeps null. Add Validators::mandatory() if a value is required:
use n2n\validation\validator\impl\Validators;
$result = Bind::attrs(['email' => null])
->prop('email', Mappers::unmarshal(Email::class), Validators::mandatory())
->toArray()
->exec();
$result->isValid(); // false
Arrays
use valobj\int\NbIdArray;
$result = Bind::attrs(['ids' => [1, '2', 3]])
->prop('ids', Mappers::unmarshal(NbIdArray::class))
->toArray()
->exec();
$result->get()['ids']; // NbIdArray [NbId(1), NbId(2), NbId(3)]
Marshal
Converts value objects back to scalars, e.g. for a JSON response:
$result = Bind::attrs([
'email' => Email::from('info@n2n.rocks'),
'id' => NbId::from(5),
'ids' => new NbIdArray([NbId::from(1), NbId::from(2)])
])
->props(['email', 'id', 'ids'], Mappers::marshal())
->toArray()
->exec();
var_dump($result->get());
Output:
array(3) {
["email"]=>
string(14) "info@n2n.rocks"
["id"]=>
int(5)
["ids"]=>
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
}
Example: REST controller
use n2n\web\http\controller\ControllerAdapter;
use n2n\web\http\controller\ParamBody;
use n2n\bind\build\impl\Bind;
use n2n\bind\mapper\impl\Mappers;
use n2n\validation\validator\impl\Validators;
class NewsletterController extends ControllerAdapter {
function postDoSubscribe(ParamBody $body, NewsletterDao $newsletterDao): void {
$result = Bind::attrs($body->parseJson())
->prop('email', Mappers::unmarshal(Email::class), Validators::mandatory())
->prop('name', Mappers::unmarshal(Name::class))
->toArray()
->exec();
if (!$result->isValid()) {
$this->sendJson(['errors' => $result->getErrorMap()]);
return;
}
$data = $result->get();
$newsletterDao->subscribe($data['email'], $data['name']);
$this->sendJson(['status' => 'ok']);
}
}
Binding to an object
Bind directly into an object with value object properties:
class SubscriptionForm {
public Email $email;
public ?Name $name = null;
}
$form = new SubscriptionForm();
$result = Bind::attrs(['email' => ' Info@N2N.rocks ', 'name' => 'Hans'])
->prop('email', Mappers::unmarshal(Email::class), Validators::mandatory())
->prop('name', Mappers::unmarshal(Name::class))
->toObj($form)
->exec();
$form->email; // Email("info@n2n.rocks")