felixarntz / wp-oop-plugin-lib

A library providing classes around WordPress APIs, to be used for example in object oriented WordPress plugins.
GNU General Public License v2.0
51 stars 3 forks source link

Models to Repository and to one another #6

Open LeTraceurSnork opened 3 days ago

LeTraceurSnork commented 3 days ago

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
LeTraceurSnork commented 3 days ago

@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