2.5 KiB
Assessment: VeruA DomainObjects & Validation
The following assessment may be solved with the help of additional sources, such as Q&A or LLMs but must be indicated as such.
General
In the example index.php there are three DomainObjects that are instantiated with default values. An Owner-Object and a Client-Object are build directly. The third, an Address-Object is passed into the address field of the Client-Object.
All three are then validated acording to their respective validators. The Address object is validated through the ClientValidator.
Exercises
-
Find out where and when the default validators of a
ValueObjectare executed. -
A default validator has to check that an e-mail address is correct on the
emailfield ofOwnerand ofClient::Addressif set, but the field can be empty.
Implement it in theValueObjects/Emailclass and use php's filter_var(FILTER_VALIDATE_EMAIL) -
The validation works well, but the requirements changed. Redesign it, to implement the possibility to have multiple validators at once. As an example implement a new
ExportAddressValidatorin addition to the standardAddressValidatorthat requires an e-mail address and a valid AHV-number. The goal is, to check, if a givenDomainObjectis valid against different validators in different circumstances, configured and executed in one run. -
DomainObjectsare often loaded from a database with the DataMapper Pattern, where you get entire collections ofDomainObjectscontaining more child objects. E.g. you have a collection ofInvoice-Objects that contains anOwner-Object, aClient-Object that in turn contains anAddress-Object.
We need a possibility to preconfigure the validators that have to be used to validate the whole collection ofDomainObjects
This excecise is about the design decisions and not the code. Feel free to design with UML, sample code or with whatever you feel the most comfortable.
Considerations
-
Rewrite it with the possibility to have different Validators from different packages.
-
Keep in mind, that if a field is not set at all, it is never validated.