I am developing an PHP API REST using Value Objects. I have some value objects as like: ID, Date, Name, etc. When they fail in their construction due to a invalid format or something it throws a InvalidArgumentException.
How can I "collect" all the Exceptions and when the script stops send them in an "error" array in the json response?
The problem is that i think that make hundred of try catch for each value object is not the best way, and I can not find the way to catch multiple exceptions in a try block.
Value Object that may throw an InvalidArgumentException
$authorID = new ID('dd'); // Must be an integer greather than 0 or null
Also I want to pass this ValueObject in the constructor of a Entity
new Insertable($authorID);
If i have multiple ValueObjects that may throw Exceptions, how can I catch them all and make a response with these exceptions as "error" array?
Thanks!