SolicitudeSearch::slugBehaviorConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
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
 * @property string $value
14
 */
15
class SolicitudeSearch extends Solicitude implements ResourceSearch
16
{
17
    /**
18
     * @inhertidoc
19
     */
20 2
    protected function slugBehaviorConfig(): array
21
    {
22
        return [
23 2
            'idAttribute' => [],
24
            'parentSlugRelation' => 'form',
25
            'resourceName' => 'form',
26
        ];
27
    }
28
29
    /**
30
     * @inhertidoc
31
     */
32 2
    public function attributes()
33
    {
34 2
        return array_merge(parent::attributes(), ['value']);
35
    }
36
37
    /**
38
     * @inhertidoc
39
     */
40 2
    public function rules()
41
    {
42
        return [
43 2
            [['form_id'], 'required'],
44
            [['form_id', 'created_by'], 'integer'],
45
            [['value'], 'default', 'value' => []],
46
            [['value'], 'validateValues'],
47
        ];
48
    }
49
50 1
    public function validateValues($attribute, $params, $validator)
51
    {
52 1
        $values = $this->$attribute;
53 1
        if (!is_array($values)) {
54
            $validator->addError(
55
                $this,
56
                $attribute,
57
                '{attribute} must receive an array'
58
            );
59
            return;
60
        }
61 1
        foreach ($values as $field_id => $value) {
62 1
            if (is_string($field_id)) {
63
                $validator->addError(
64
                    $this,
65
                    $attribute,
66
                    '`value` only accepts key integers to filter by `field_id`.'
67
                );
68
            }
69 1
            if (empty($value)) {
70
                $validator->addError(
71
                    $this,
72
                    $attribute,
73 1
                    '`value`  must not be empty.'
74
                );
75
            }
76
        }
77 1
    }
78
79
    /**
80
     * @inhertidoc
81
     */
82 2
    public function search(
83
        array $params,
84
        ?string $formName = ''
85
    ): ?ActiveDataProvider {
86 2
        $this->load($params, $formName);
87 2
        if (!$this->validate()) {
88 1
            return null;
89
        }
90 2
        $class = get_parent_class();
91 2
        $query = $class::find()->andFilterWhere([
92 2
            'form_id' => $this->form_id,
93 2
            'created_by' => $this->created_by,
94
        ]);
95
96 2
        foreach ($this->value as $field_id => $value) {
0 ignored issues
show
Bug introduced by
The expression $this->value of type string is not traversable.
Loading history...
97 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...
98 1
            $query->andWhere([
99 1
                'exists',
100 1
                SolicitudeValue::find()->andWhere([
101 1
                    'and',
102 1
                    'solicitude_id = id',
103 1
                    ['field_id' => $field_id],
104
                    [
105 1
                        'like',
106 1
                        'value',
107 1
                        $value,
108
                    ],
109
                ])
110
            ]);
111
        }
112
113 2
        return new ActiveDataProvider(['query' => $query]);
114
    }
115
}
116