1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Repository\Core; |
15
|
|
|
|
16
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
18
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\FileInfo; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Daniel West <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @method FileInfo|null find($id, $lockMode = null, $lockVersion = null) |
24
|
|
|
* @method FileInfo|null findOneBy(array $criteria, array $orderBy = null) |
25
|
|
|
* @method FileInfo[] findAll() |
26
|
|
|
* @method FileInfo[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
27
|
|
|
*/ |
28
|
|
|
class FileInfoRepository extends ServiceEntityRepository |
29
|
|
|
{ |
30
|
2 |
|
public function __construct(ManagerRegistry $registry) |
31
|
|
|
{ |
32
|
2 |
|
parent::__construct($registry, FileInfo::class); |
33
|
2 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return FileInfo[] |
37
|
|
|
*/ |
38
|
1 |
|
public function findByPathsAndFilters(array $paths, ?array $filters): array |
39
|
|
|
{ |
40
|
1 |
|
if (!\count($paths)) { |
41
|
1 |
|
return []; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
$queryBuilder = $this->createQueryBuilder('f'); |
45
|
1 |
|
$expr = $queryBuilder->expr(); |
46
|
|
|
|
47
|
1 |
|
$filterQueries = []; |
48
|
1 |
|
if ($filters) { |
|
|
|
|
49
|
1 |
|
foreach ($filters as $filterIndex => $filter) { |
50
|
1 |
|
if (!$filter) { |
51
|
1 |
|
$filterQueries[] = $expr->isNull('f.filter'); |
52
|
1 |
|
continue; |
53
|
|
|
} |
54
|
1 |
|
$filterQueries[] = $expr->eq('f.filter', ':filter_' . $filterIndex); |
55
|
1 |
|
$queryBuilder->setParameter(':filter_' . $filterIndex, $filter); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
foreach ($paths as $pathIndex => $path) { |
60
|
1 |
|
$queryBuilder->setParameter(':path_' . $pathIndex, $path); |
61
|
1 |
|
if (!\count($filterQueries)) { |
62
|
|
|
$queryBuilder |
63
|
1 |
|
->orWhere( |
64
|
1 |
|
$expr->eq('f.path', ':path_' . $pathIndex) |
65
|
|
|
); |
66
|
1 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$queryBuilder |
70
|
1 |
|
->orWhere( |
71
|
1 |
|
$expr->andX( |
72
|
1 |
|
$expr->eq('f.path', ':path_' . $pathIndex), |
73
|
1 |
|
$expr->orX(...$filterQueries) |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
return $queryBuilder->getQuery()->getResult(); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function findOneByPathAndFilter(string $path, ?string $filter): ?FileInfo |
82
|
|
|
{ |
83
|
1 |
|
return $this->findOneBy([ |
84
|
1 |
|
'path' => $path, |
85
|
1 |
|
'filter' => $filter, |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.