rikbruil / Doctrine-Specification

Doctrine Specification pattern for building queries dynamically and with re-usable classes for composition.
MIT License
50 stars 7 forks source link

SpecificationFactory #18

Open juliangut opened 7 years ago

juliangut commented 7 years ago

Needing to write

use Rb\Specification\Doctrine\Condition\Equals;
use Rb\Specification\Doctrine\Condition\IsNull;
use Rb\Specification\Doctrine\Condition\LessThan;
use Rb\Specification\Doctrine\Logic\AndX;
use Rb\Specification\Doctrine\Logic\OrX;
...

is tedious AND bloated (even with PHP7 syntax) a factory instead would be an improvement and let you choose whichever method you like

use Rb\Specification\Doctrine\SpecificationFactory as Factory;
use Rb\Specification\Doctrine\Specification;

$spec = new Specification([
    Factory::Equals('ended', 0),
    Factory::OrX(
        Factory::LessThan('endDate', new \DateTime()),
        Factory::AndX(
            Factory::IsNull('endDate'),
            Factory::LessThan('startDate', new \DateTime('-4weeks'))
        )
    )
]);
rikbruil commented 7 years ago

Agreed, will look into this.

I was never really pleased with the repetition, but did not find a 'nice' solution. The benefit before should have been less typing, but use-statements are still growing.

In this setup the use-statements will be less, however the typing contains more repetition (needing to type 'Factory' everywhere).

Not saying I'm not open to this change, just giving some motivation why I did not implement something better yet :) But as I said, I will look into getting rid of the verbosity.