|
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 hiapi\validators\LimitValidator; |
|
14
|
|
|
use hiapi\validators\RefValidator; |
|
15
|
|
|
use hiqdev\yii\DataMapper\query\Specification; |
|
16
|
|
|
use hiqdev\yii\DataMapper\query\attributes\validators\WhereValidator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class SearchCommand |
|
20
|
|
|
* |
|
21
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class SearchCommand extends EntityCommand |
|
24
|
|
|
{ |
|
25
|
|
|
public $select; |
|
26
|
|
|
public $limit; |
|
27
|
|
|
public $page; |
|
28
|
|
|
public $where = []; |
|
29
|
|
|
public $filter = []; |
|
30
|
|
|
public $with = []; |
|
31
|
|
|
public $include = []; |
|
32
|
|
|
public $count; |
|
33
|
|
|
|
|
34
|
|
|
const DEFAULT_LIMIT = 25; |
|
35
|
|
|
|
|
36
|
|
|
public function rules() |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
|
|
['select', 'safe'], |
|
40
|
|
|
[ |
|
41
|
|
|
['where', 'filter'], |
|
42
|
|
|
WhereValidator::class, |
|
43
|
|
|
'targetEntityClass' => $this->getEntityClass(), |
|
44
|
|
|
], |
|
45
|
|
|
[['page'], 'integer'], |
|
46
|
|
|
['limit', LimitValidator::class], |
|
47
|
|
|
[['with', 'include'], 'each', 'rule' => [RefValidator::class]], |
|
48
|
|
|
[['count'], 'boolean'] |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
* @psalm-var class-string<Specification> |
|
55
|
|
|
*/ |
|
56
|
|
|
protected string $specificationClassName = Specification::class; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
public function getSpecification(): Specification |
|
59
|
|
|
{ |
|
60
|
|
|
$spec = new $this->specificationClassName(); |
|
61
|
|
|
foreach (['select', 'limit', 'with', 'where'] as $key) { |
|
62
|
|
|
if (!empty($this->{$key})) { |
|
63
|
|
|
$spec->{$key} = $this->{$key}; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
if (empty($spec->limit)) { |
|
67
|
|
|
$spec->limit = self::DEFAULT_LIMIT; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $spec; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|