Skip to content

Commit 8772ab6

Browse files
authored
Merge pull request rikbruil#19 from elt/repository_as_trait
SpecificationRepository as trait
2 parents 86bbc2a + f61b79c commit 8772ab6

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"psr-4": {"Rb\\Specification\\Doctrine\\": "src/"}
1616
},
1717
"autoload-dev": {
18-
"psr-4": {"spec\\Rb\\Specification\\Doctrine\\": "spec/"}
18+
"psr-4": {"spec\\Rb\\Specification\\Doctrine\\": "spec/"},
19+
"files": ["spec/SpecificationRepositoryStub.php"]
1920
},
2021
"require": {
2122
"php": ">=5.5",

spec/SpecificationRepositorySpec.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class SpecificationRepositorySpec extends ObjectBehavior
2222

2323
public function let(EntityManager $entityManager, ClassMetadata $classMetadata)
2424
{
25+
$this->beAnInstanceOf('Rb\Specification\Doctrine\SpecificationRepositoryStub');
2526
$this->beConstructedWith($entityManager, $classMetadata);
2627
}
2728

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Rb\Specification\Doctrine;
4+
5+
use Doctrine\ORM\EntityRepository;
6+
7+
class SpecificationRepositoryStub extends EntityRepository implements SpecificationAware
8+
{
9+
use SpecificationRepositoryTrait;
10+
}

src/SpecificationAware.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rb\Specification\Doctrine;
4+
5+
use Doctrine\ORM\Query;
6+
use Rb\Specification\Doctrine\Result\ModifierInterface;
7+
8+
/**
9+
* SpecificationAware can be used to implement custom repository.
10+
*/
11+
interface SpecificationAware
12+
{
13+
/**
14+
* Get the query after matching with given specification.
15+
*
16+
* @param SpecificationInterface $specification
17+
* @param ModifierInterface $modifier
18+
*
19+
* @return Query
20+
*/
21+
public function match(SpecificationInterface $specification, ModifierInterface $modifier = null);
22+
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,29 @@
22

33
namespace Rb\Specification\Doctrine;
44

5-
use Doctrine\ORM\EntityRepository;
65
use Doctrine\ORM\Query;
76
use Doctrine\ORM\QueryBuilder;
87
use Rb\Specification\Doctrine\Exception\LogicException;
98
use Rb\Specification\Doctrine\Result\ModifierInterface;
109

1110
/**
12-
* Class SpecificationRepository.
11+
* Class SpecificationRepositoryTrait.
1312
*/
14-
class SpecificationRepository extends EntityRepository
13+
trait SpecificationRepositoryTrait
1514
{
1615
/**
1716
* @var string
1817
*/
19-
private $dqlAlias = 'e';
18+
protected $dqlAlias = 'e';
2019

2120
/**
22-
* Get the query after matching with given specification.
21+
* @see SpecificationAware::match()
2322
*
2423
* @param SpecificationInterface $specification
25-
* @param ModifierInterface $modifier
26-
*
27-
* @throws LogicException
24+
* @param ModifierInterface|null $modifier
2825
*
2926
* @return Query
27+
* @throws LogicException
3028
*/
3129
public function match(SpecificationInterface $specification, ModifierInterface $modifier = null)
3230
{

0 commit comments

Comments
 (0)