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 4 forks source link

Models to Repository and to one another #6

Open LeTraceurSnork opened 4 weeks ago

LeTraceurSnork commented 4 weeks 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 4 weeks 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

felixarntz commented 1 week ago

@LeTraceurSnork Thanks for raising this!

I like the idea of dedicated getters and setters. That said, it would be great to maintain the ability of passing the WP typical "array of args". I think we could achieve both, so that the API could be used in either way.

One note about the code example: For this library, we should continue to follow the WordPress coding standards, i.e. snake_case instead of camelCase.