Completed
Push — master ( a43f9a...c42311 )
by Dmitry
12:06
created

SearchCommand::getSpecification()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 9.7998
c 0
b 0
f 0
cc 4
nc 6
nop 0
crap 20
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;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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