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

Query by instance of #3

Closed matteosister closed 9 years ago

matteosister commented 9 years ago

Hi! Thanks for this library! Really nice work. :+1:

I can't find an implementation for INSTANCE OF

I created a custom SpecificationInterface by now....let me know if you want a pr! I need some help on the implementation...

rikbruil commented 9 years ago

Thanks for the compliment :)

You are right the INSTANCE OF implementation is missing. If you want I can take a look at implementing this (probably this weekend).

If you already have some code ready though, you can create a PR which we then can go through together.

But looking at the Doctrine documentation I could probably have a working implementation of it in the next few days.

matteosister commented 9 years ago

right now my implementation is as simple as:

<?php

namespace Prima\Filter\Specs;

use Doctrine\ORM\QueryBuilder;
use Rb\Specification\Doctrine\SpecificationInterface;

/**
 * Class IsInstanceOf
 */
class IsInstanceOf implements SpecificationInterface
{
    private $className;

    /**
     * @param $className
     */
    public function __construct($className)
    {
        $this->className = $className;
    }

    /**
     * @param QueryBuilder $queryBuilder
     * @param $dqlAlias
     * @return mixed
     */
    public function modify(QueryBuilder $queryBuilder, $dqlAlias)
    {
        $queryBuilder
            ->andWhere($dqlAlias.' INSTANCE OF '.$this->className);
    }

    /**
     * Returns a boolean indicating whether or not this specification can support the given value.
     *
     * @param mixed $value
     *
     * @return bool
     */
    public function isSatisfiedBy($value)
    {
        return class_exists($value);
    }
}

I created this without any specific knowledge about the library...so let me know...a pr is easily done! But I don't think this is really valuable :wink: