1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HiAPI Yii2 base project for building API |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hiapi |
6
|
|
|
* @package hiapi |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiapi\commands; |
12
|
|
|
|
13
|
|
|
use hiqdev\DataMapper\Query\Specification; |
14
|
|
|
use hiqdev\DataMapper\Repository\RepositoryInterface; |
15
|
|
|
use hiqdev\DataMapper\Repository\EntityManagerInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class SearchHandler |
19
|
|
|
* |
20
|
|
|
* @author Dmytro Naumenko <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class SearchHandler |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var EntityManagerInterface |
26
|
|
|
*/ |
27
|
|
|
private $em; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* SearchHandler constructor. |
31
|
|
|
* |
32
|
|
|
* @param EntityManagerInterface $em |
33
|
|
|
*/ |
34
|
|
|
public function __construct(EntityManagerInterface $em) |
35
|
|
|
{ |
36
|
|
|
$this->em = $em; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return Specification |
41
|
|
|
*/ |
42
|
|
|
protected function createSpecification() |
43
|
|
|
{ |
44
|
|
|
return new Specification(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param SearchCommand $command |
49
|
|
|
*/ |
50
|
|
|
public function handle(EntityCommandInterface $command) |
51
|
|
|
{ |
52
|
|
|
$repo = $this->getRepository($command); |
|
|
|
|
53
|
|
|
$specification = $this->buildSpecification($command); |
54
|
|
|
|
55
|
|
|
if ($command->count) { |
|
|
|
|
56
|
|
|
return $repo->count($specification); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $repo->findAll($specification); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param EntityCommandInterface|SearchCommand $command |
64
|
|
|
* @return Specification |
65
|
|
|
*/ |
66
|
|
|
protected function buildSpecification(EntityCommandInterface $command) |
67
|
|
|
{ |
68
|
|
|
$spec = $this->createSpecification(); |
69
|
|
|
$spec->where(array_merge($command->filter, $command->where)); |
|
|
|
|
70
|
|
|
|
71
|
|
|
if (!$command->count) { |
|
|
|
|
72
|
|
|
if (strtolower($command->limit) === 'all') { |
|
|
|
|
73
|
|
|
$command->limit = -1; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
$limit = $command->limit ?? 25; |
|
|
|
|
76
|
|
|
$spec->limit($limit); |
77
|
|
|
if ($limit !== -1) { |
78
|
|
|
$spec->offset((($command->page ?? 0) * $limit) - $limit); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
$spec->with(array_merge($command->include, $command->with)); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $spec; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param SearchCommand $command |
88
|
|
|
* @return RepositoryInterface |
89
|
|
|
*/ |
90
|
|
|
protected function getRepository(EntityCommand $command) |
91
|
|
|
{ |
92
|
|
|
return $this->em->getRepository($command->getEntityClass()); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.