Completed
Pull Request — master (#1)
by Angel
07:01
created

SolicitudeSearch::validateValues()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 11.2989

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 6
nop 3
dl 0
loc 24
ccs 7
cts 19
cp 0.3684
crap 11.2989
rs 9.3554
c 0
b 0
f 0
1
<?php
2
3
namespace roaresearch\yii2\formgenerator\roa\models;
4
5
use roaresearch\yii2\roa\ResourceSearch;
6
use yii\data\ActiveDataProvider;
7
8
/**
9
 * Contract to filter and sort collections of `Solicitude` records.
10
 *
11
 * @author Angel (Faryshta) Guevara <[email protected]>
12
 */
13
class SolicitudeSearch extends Solicitude implements ResourceSearch
14
{
15
    /**
16
     * @inhertidoc
17
     */
18 2
    protected function slugBehaviorConfig(): array
19
    {
20
        return [
21 2
            'idAttribute' => [],
22
            'parentSlugRelation' => 'form',
23
            'resourceName' => 'form',
24
        ];
25
    }
26
27
    /**
28
     * @inhertidoc
29
     */
30 2
    public function attributes()
31
    {
32 2
        return array_merge(parent::attributes(), ['value']);
33
    }
34
35
    /**
36
     * @inhertidoc
37
     */
38 2
    public function rules()
39
    {
40
        return [
41 2
            [['form_id'], 'required'],
42
            [['form_id', 'created_by'], 'integer'],
43
            [['value'], 'default', 'value' => []],
44
            [['value'], 'validateValues'],
45
        ];
46
    }
47
48 1
    public function validateValues($attribute, $params, $validator)
49
    {
50 1
        $values = $this->$attribute;
51 1
        if (!is_array($values)) {
52
            $validator->addError(
53
                $this,
54
                $attribute,
55
                '{attribute} must receive an array'
56
            );
57
            return;
58
        }
59 1
        foreach ($values as $field_id => $value) {
60 1
            if (is_string($field_id)) {
61
                $validator->addError(
62
                    $this,
63
                    $attribute,
64
                    '`value` only accepts key integers to filter by `field_id`.'
65
                );
66
            }
67 1
            if (empty($value)) {
68
                $validator->addError(
69
                    $this,
70
                    $attribute,
71 1
                    '`value`  must not be empty.'
72
                );
73
            }
74
        }
75 1
    }
76
77
    /**
78
     * @inhertidoc
79
     */
80 2
    public function search(
81
        array $params,
82
        ?string $formName = ''
83
    ): ?ActiveDataProvider {
84 2
        $this->load($params, $formName);
85 2
        if (!$this->validate()) {
86 1
            return null;
87
        }
88 2
        $class = get_parent_class();
89 2
        $query = $class::find()->andFilterWhere([
90 2
            'form_id' => $this->form_id,
91 2
            'created_by' => $this->created_by,
0 ignored issues
show
Bug Best Practice introduced by
The property created_by does not exist on roaresearch\yii2\formgen...models\SolicitudeSearch. Since you implemented __get, consider adding a @property annotation.
Loading history...
92
        ]);
93
94 2
        foreach ($this->value as $field_id => $value) {
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist on roaresearch\yii2\formgen...models\SolicitudeSearch. Since you implemented __get, consider adding a @property annotation.
Loading history...
95 1
            $alias = "value_$field_id";
0 ignored issues
show
Unused Code introduced by
The assignment to $alias is dead and can be removed.
Loading history...
96 1
            $query->andWhere([
97 1
                'exists',
98 1
                SolicitudeValue::find()->andWhere([
99 1
                    'and',
100 1
                    'solicitude_id = id',
101 1
                    ['field_id' => $field_id],
102
                    [
103 1
                        'like',
104 1
                        'value',
105 1
                        $value,
106
                    ],
107
                ])
108
            ]);
109
        }
110
111 2
        return new ActiveDataProvider(['query' => $query]);
112
    }
113
}
114