Deeper library knowledge
Suits perfectly to situations when you need to validate data in your model layer or developing front-end application bundled with some REST api together. Validation syntax can be very simple or much advanced with very clear writing without any IF hell in your code.
$entity->getValidator()
->addRule(function(\UniMapper\Entity $entity) {
return $entity->text === "foo";
}, "Text must be foo!");
$entity->getValidator()
->on("text")
->addRule(Validator::FILLED, "Text is required!")
->on("email")
->addRule(Validator::EMAIL, "Invalid e-mail format!");
Computed properties can not be validated!
$order->getValidator()
->on("product")
->addRule(Validator::FILLED, "Product is required!");
$order->customer->getValidator()
->on("email")
->addRule(Validator::EMAIL, "Invalid e-mail!");
$order->customer->branchOffice->getValidator()
->on("phone")
->addRule(Validator::FILLED, "Invalid phone!");
$entity->getValidator()
->on("ip")
->addCondition(Validator::FILLED)
->addRule(Validator::IP, "Invalid IP format!")
->endCondition()
Using
->on(...)
is not allowed inside the condition block!
You can set validation rules to every single entity instance by calling getValidator()
method.