I suggest consideration changing the architecture of the Felix_Arntz\WP_OOP_Plugin_Lib\Entities namespace so that models would have named fields with their own getters and (optionally) setters. For example, $post->getId(); $post->getTitle(); $post->getContent();. The repositories working with these models would then accept the models themselves as arguments, rather than array $data. This would allow to populate the models with data first (or even provide factories) and then save them directly through repositories without worrying about field names, required fields, or the quantity of fields needed. In other words, simply create a model, pump it with data, pass it to the repository, repository saved it — done!
An example using interfaces:
$author = new User($login, $password);
$post = new Post($title, $content);
$post->setSlug($slug);
$post->setAuthor($author); // instanceof User | instanceof UserInterface
$post->setCategories($categories);
PostRepository::add($post); // instanceof Post | instanceof PostInterface
@szepeviktor @felixarntz if you want, I can submit a short fast PR with an example of that type of hierarchy just to clarify what kind of interfaces I'm talking about
I suggest consideration changing the architecture of the
Felix_Arntz\WP_OOP_Plugin_Lib\Entities
namespace so that models would have named fields with their own getters and (optionally) setters. For example, $post->getId(); $post->getTitle(); $post->getContent();. The repositories working with these models would then accept the models themselves as arguments, rather thanarray $data
. This would allow to populate the models with data first (or even provide factories) and then save them directly through repositories without worrying about field names, required fields, or the quantity of fields needed. In other words, simply create a model, pump it with data, pass it to the repository, repository saved it — done!An example using interfaces: