1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Porpaginas\Doctrine\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
7
|
|
|
use Zenstruck\Porpaginas\Doctrine\DBALQueryBuilderResult; |
8
|
|
|
use Zenstruck\Porpaginas\Exception\NotFound; |
9
|
|
|
use Zenstruck\Porpaginas\Factory\FactoryResult; |
10
|
|
|
use Zenstruck\Porpaginas\Repository; |
11
|
|
|
use Zenstruck\Porpaginas\Result; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Kevin Bond <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
abstract class DBALObjectRepository implements Repository |
17
|
|
|
{ |
18
|
2 |
|
public function getIterator(): \Traversable |
19
|
|
|
{ |
20
|
2 |
|
return static::createResult($this->qb()); |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
public function count(): int |
24
|
|
|
{ |
25
|
1 |
|
return \count(static::createResult($this->qb())); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param callable(QueryBuilder $qb) $specification |
30
|
|
|
*/ |
31
|
2 |
|
public function get(callable $specification): object |
32
|
|
|
{ |
33
|
2 |
|
$result = $this->queryBuilderForSpecification($specification) |
|
|
|
|
34
|
2 |
|
->setMaxResults(1) |
35
|
2 |
|
->execute() |
36
|
2 |
|
->fetch() |
37
|
|
|
; |
38
|
|
|
|
39
|
2 |
|
if (!$result) { |
40
|
1 |
|
throw new NotFound(\sprintf('Object from "%s" table not found for given specification.', static::tableName())); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
return static::createObject($result); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param callable(QueryBuilder $qb) $specification |
48
|
|
|
*/ |
49
|
1 |
|
public function filter(callable $specification): Result |
50
|
|
|
{ |
51
|
1 |
|
return self::createResult($this->queryBuilderForSpecification($specification)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
abstract protected static function createObject(array $data): object; |
55
|
|
|
|
56
|
|
|
abstract protected static function tableName(): string; |
57
|
|
|
|
58
|
|
|
abstract protected function connection(): Connection; |
59
|
|
|
|
60
|
3 |
|
final protected static function createResult(QueryBuilder $qb): Result |
61
|
|
|
{ |
62
|
3 |
|
return new FactoryResult( |
63
|
|
|
static function(array $data) { |
64
|
3 |
|
return static::createObject($data); |
65
|
3 |
|
}, |
66
|
3 |
|
static::createDBALQueryBuilderResult($qb) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
protected static function createDBALQueryBuilderResult(QueryBuilder $qb): DBALQueryBuilderResult |
71
|
|
|
{ |
72
|
3 |
|
return new DBALQueryBuilderResult($qb); |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
protected function qb(): QueryBuilder |
76
|
|
|
{ |
77
|
5 |
|
return $this->connection()->createQueryBuilder()->select('*')->from(static::tableName()); |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
private function queryBuilderForSpecification(callable $specification): QueryBuilder |
81
|
|
|
{ |
82
|
3 |
|
$specification($qb = $this->qb()); |
83
|
|
|
|
84
|
3 |
|
return $qb; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.