Download the PHP package loophp/repository-monadic-helper without Composer
On this page you can find all versions of the php package loophp/repository-monadic-helper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package repository-monadic-helper
Doctrine Repository Monadic Helper
Description
This project provides the necessary classes and services to use Doctrine repositories in a more functional way, by using monads.
This project also demonstrate that it's a nice and clean way to work with repositories and non-deterministic data store, in this case, a database.
There is no need to always check for the existence of an entity, so we are able to reduce the amount of conditions and cruft, while focusing on what's important and relevant only.
When using properly typed monads and callbacks, types inconsistencies will be instantly detected by static analysis tools. This provides a safer and better way to design functions and data transformation methods.
The monad in use in this project is the Either monad, provided by the contrib package Lamphpda from Marco Perone.
History
This project started as a proof-of-concept for my own needs and for many reasons.
I first wanted to get rid of all the checks and conditions that were needed in my code to test if an entity was existing or not.
Then, it turns out that this was a recurrent pattern that I was seeing the code of my friends, colleagues and issue queue.
A practical way to get rid of conditions is to use a more declarative way of programming and adopt a more functional programming style.
And lastly, willing to learn more about the monads which are some kind of "design patterns" for functional programming, I started to write this package.
This package does not have the pretention to become a de-facto standard on how to use Doctrine repositories, but it might help people understanding what monads are, how to use them, and hopefully reduce the amount of conditions in their code.
The monad package in use here is an arbitrary choice. I could have used some other packages, but marcosh/lamphpda seems to be the best option, especially when you analyse your code with static analysis tools to detect issues upfront, without running their unit tests.
Installation
Usage
To use this package and have monadic repositories, there are 3 options available:
- Without alteration of existing Doctrine repositories
- Options 1: By using a service which is creating a monadic repository from an entity class name or an existing repository.
- With alteration of existing Doctrine repositories
- Option 2: By adding an interface, a trait and relevant typing
information like:
@implements MonadicServiceEntityRepositoryInterface<EntityClassName>
- Option 3: By replacing
extends ServiceEntityRepository
withextends MonadicServiceEntityRepository
and add relevant typing information like:@extends MonadicServiceEntityRepository<EntityClassName>
- Option 2: By adding an interface, a trait and relevant typing
information like:
In my own opinion, the best way to use this package is to use the first option.
It's paramount to replace EntityClassName
with the entity class in use in the
repository in order to let static analysis tools infer types properly.
For the option 2 and 3, let's use the following usual Doctrine repository as example:
Option 1: Using a service
Option 2: Using an interface and a trait on existing repositories
Upgrade your Doctrine repositories to:
Option 3: Replace ServiceEntityRepository
with MonadicServiceEntityRepository
Upgrade your Doctrine repositories to:
API
The following methods will be available when using the service (option 1) or upgrading your repositories (options 2 and 3).
For each API methods, the placeholder MyCustomEntity
should be replaced by the
entity you're referring to.
eitherFind
Signature:
An exception is generated and wrapped in the monad when the returned result is
null
.
eitherFindAll
Signature:
An exception is generated and wrapped in the monad when the returned result is empty.
eitherFindBy
Signature:
An exception is generated and wrapped in the monad when the returned result is empty.
eitherFindOneBy
Signature:
An exception is generated and wrapped in the monad when the returned result is
null
.
Todo
- Improve documentation and code examples.
Contributing
Feel free to contribute by sending pull requests. We are a usually very responsive team and we will help you going through your pull request from the beginning to the end.
For some reasons, if you can't contribute to the code and willing to help, sponsoring is a good, sound and safe way to show us some gratitude for the hours we invested in this package.
Sponsor me on Github and/or any of [the contributors][contributors].
Changelog
See [CHANGELOG.md][47] for a changelog based on git commits.
For more detailed changelogs, please check the release changelogs.
[phpsandbox image]: https://img.shields.io/badge/Try%20it-online%20!-brightgreen?style=flat-square
[44]: https://tomasvotruba.com/blog/2017/10/16/how-to-use-repository-with-doctrine-as-service-in-symfony/
[47]: https://github.com/loophp/repository-monadic-helper/blob/main/CHANGELOG.md
[50]: https://symfony.com/doc/current/service_container.html#binding-arguments-by-name-or-type
[contributors]: https://github.com/loophp/repository-monadic-helper/graphs/contributors