Completed
Push — master ( 1a90b0...9a342a )
by Andrii
10:19
created

SearchCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 49
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 25 5
A getSpecification() 0 11 3
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\RefValidator;
14
use hiqdev\yii\DataMapper\query\Specification;
15
16
/**
17
 * Class SearchCommand
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
abstract class SearchCommand extends EntityCommand
22
{
23
    public $select;
24
    public $limit;
25
    public $page;
26
    public $where = [];
27
    public $filter = [];
28
    public $with = [];
29
    public $include = [];
30
    public $count;
31
32
    public function rules()
33
    {
34
        return [
35
            ['select', 'safe'],
36
            [['where', 'filter'], 'safe'],
37
            [['page'], 'integer'],
38
            ['limit', function () {
39
                if (empty($this->limit)) {
40
                    return;
41
                }
42
43
                if (mb_strtolower($this->limit) === 'all') {
44
                    $this->limit = 'all';
45
                    return;
46
                }
47
48
                if ($this->limit < 1 || $this->limit > 1000) {
49
                    $this->addError('limit', 'Limit must be between 1 and 1000');
50
                    return;
51
                }
52
            }],
53
            [['with', 'include'], 'each', 'rule' => [RefValidator::class]],
54
            [['count'], 'boolean']
55
        ];
56
    }
57
58
    public function getSpecification(): Specification
59
    {
60
        $spec = new Specification();
61
        foreach (['select', 'limit', 'with', 'where'] as $key) {
62
            if (!empty($this->{$key})) {
63
                $spec->{$key} = $this->{$key};
64
            }
65
        }
66
67
        return $spec;
68
    }
69
}
70