Skip to content

Commit

Permalink
Merge pull request #280 from peter-gribanov/BaseSpecification_location
Browse files Browse the repository at this point in the history
Сorrect location of BaseSpecification class
  • Loading branch information
peter-gribanov authored Jan 20, 2021
2 parents 588e786 + 15ccf3f commit 191551b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 74 deletions.
2 changes: 1 addition & 1 deletion docs/0-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Then you may start to create your specifications. Put them in `Acme\DemoBundle\E

namespace Acme\DemoBundle\Entity\Spec;

use Happyr\DoctrineSpecification\BaseSpecification;
use Happyr\DoctrineSpecification\Specification\BaseSpecification;
use Happyr\DoctrineSpecification\Spec;

/**
Expand Down
4 changes: 2 additions & 2 deletions docs/1-creatingSpecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function modify(QueryBuilder $qb, $dqlAlias)

## BaseSpecification

To make your life easier you may use the `Happyr\DoctrineSpecification\BaseSpecification` class. When you extend
To make your life easier you may use the `Happyr\DoctrineSpecification\Specification\BaseSpecification` class. When you extend
this class you don't need to bother with `getFilter` or `modify`. You need to do 2 things:

1. If you implement a constructor, make sure to call the parent constructor with $dqlAlias
Expand All @@ -61,7 +61,7 @@ this class you don't need to bother with `getFilter` or `modify`. You need to do
Consider the following example.

```php
use Happyr\DoctrineSpecification\BaseSpecification;
use Happyr\DoctrineSpecification\Specification\BaseSpecification;
use Happyr\DoctrineSpecification\Spec;

/**
Expand Down
77 changes: 6 additions & 71 deletions src/BaseSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,80 +13,15 @@

namespace Happyr\DoctrineSpecification;

use Doctrine\ORM\QueryBuilder;
use Happyr\DoctrineSpecification\Filter\Filter;
use Happyr\DoctrineSpecification\Query\QueryModifier;
use Happyr\DoctrineSpecification\Specification\Specification;
use Happyr\DoctrineSpecification\Specification\BaseSpecification as BaseBaseSpecification;

@trigger_error('The '.__NAMESPACE__.'\BaseSpecification class is deprecated since version 1.1 and will be removed in 2.0, use \Happyr\DoctrineSpecification\Specification\BaseSpecification instead.', E_USER_DEPRECATED);

/**
* Extend this abstract class if you want to build a new spec with your domain logic.
*
* @deprecated This class is deprecated since version 1.1 and will be removed in 2.0, use \Happyr\DoctrineSpecification\Specification\BaseSpecification instead.
*/
abstract class BaseSpecification implements Specification
abstract class BaseSpecification extends BaseBaseSpecification
{
/**
* @var string|null
*/
private $dqlAlias;

/**
* @param string|null $dqlAlias
*/
public function __construct($dqlAlias = null)
{
$this->dqlAlias = $dqlAlias;
}

/**
* @param QueryBuilder $qb
* @param string $dqlAlias
*
* @return string
*/
public function getFilter(QueryBuilder $qb, $dqlAlias)
{
$spec = $this->getSpec();
if ($spec instanceof Filter) {
return $spec->getFilter($qb, $this->getAlias($dqlAlias));
}

return '';
}

/**
* @param QueryBuilder $qb
* @param string $dqlAlias
*/
public function modify(QueryBuilder $qb, $dqlAlias)
{
$spec = $this->getSpec();
if ($spec instanceof QueryModifier) {
$spec->modify($qb, $this->getAlias($dqlAlias));
}
}

/**
* Return all the specifications.
*
* @return Filter|QueryModifier|null
*/
protected function getSpec()
{
@trigger_error('Using the default implementation of '.__METHOD__.' method is deprecated since version 1.1 and this method will be marked as abstract in 2.0. You must overwrite this implementation.', E_USER_DEPRECATED);

return null;
}

/**
* @param string $dqlAlias
*
* @return string
*/
private function getAlias($dqlAlias)
{
if (null !== $this->dqlAlias) {
return $this->dqlAlias;
}

return $dqlAlias;
}
}
91 changes: 91 additions & 0 deletions src/Specification/BaseSpecification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* This file is part of the Happyr Doctrine Specification package.
*
* (c) Tobias Nyholm <tobias@happyr.com>
* Kacper Gunia <kacper@gunia.me>
* Peter Gribanov <info@peter-gribanov.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Happyr\DoctrineSpecification\Specification;

use Doctrine\ORM\QueryBuilder;
use Happyr\DoctrineSpecification\Filter\Filter;
use Happyr\DoctrineSpecification\Query\QueryModifier;

/**
* Extend this abstract class if you want to build a new spec with your domain logic.
*/
abstract class BaseSpecification implements Specification
{
/**
* @var string|null
*/
private $dqlAlias;

/**
* @param string|null $dqlAlias
*/
public function __construct($dqlAlias = null)
{
$this->dqlAlias = $dqlAlias;
}

/**
* @param QueryBuilder $qb
* @param string $dqlAlias
*
* @return string
*/
public function getFilter(QueryBuilder $qb, $dqlAlias)
{
$spec = $this->getSpec();
if ($spec instanceof Filter) {
return $spec->getFilter($qb, $this->getAlias($dqlAlias));
}

return '';
}

/**
* @param QueryBuilder $qb
* @param string $dqlAlias
*/
public function modify(QueryBuilder $qb, $dqlAlias)
{
$spec = $this->getSpec();
if ($spec instanceof QueryModifier) {
$spec->modify($qb, $this->getAlias($dqlAlias));
}
}

/**
* Return all the specifications.
*
* @return Filter|QueryModifier|null
*/
protected function getSpec()
{
@trigger_error('Using the default implementation of '.__METHOD__.' method is deprecated since version 1.1 and this method will be marked as abstract in 2.0. You must overwrite this implementation.', E_USER_DEPRECATED);

return null;
}

/**
* @param string $dqlAlias
*
* @return string
*/
private function getAlias($dqlAlias)
{
if (null !== $this->dqlAlias) {
return $this->dqlAlias;
}

return $dqlAlias;
}
}

0 comments on commit 191551b

Please sign in to comment.