ARule::setBoundForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace kalanis\kw_auth_forms\Rules;
4
5
6
use kalanis\kw_forms\Form;
7
use kalanis\kw_rules\Exceptions\RuleException;
8
use kalanis\kw_rules\Rules\ARule as OrigRule;
9
10
11
/**
12
 * Class ARule
13
 * @package kalanis\kw_auth_forms\Rules
14
 * Abstract class which process inputs
15
 */
16
abstract class ARule extends OrigRule
17
{
18
    protected ?Form $boundForm = null;
19
20 23
    public function setBoundForm(Form $boundForm): void
21
    {
22 23
        $this->boundForm = $boundForm;
23 23
    }
24
25
    /**
26
     * @throws RuleException
27
     * @return array<string|int, string|int|float|bool|null>
28
     */
29 16
    protected function sentInputs()
30
    {
31 16
        $this->getBoundForm()->setSentValues();
32
        // we want only predefined ones
33 15
        $whichInputs = $this->againstValue;
34 15
        if (!is_array($whichInputs)) {
35 1
            throw new RuleException('Compare only against list of known keys!');
36
        }
37 14
        $data = array_filter($this->getBoundForm()->getValues(), function ($k) use ($whichInputs) {
38 14
            return in_array($k, $whichInputs);
39 14
        }, ARRAY_FILTER_USE_KEY);
40
41
        // now set it in predefined order
42 14
        $flippedInputs = array_flip($whichInputs);
43 14
        uksort($data, function ($a, $b) use ($flippedInputs) {
44 14
            return strcmp(strval($flippedInputs[$a]), strval($flippedInputs[$b]));
45 14
        });
46
47 14
        return $data;
48
    }
49
50
    /**
51
     * @throws RuleException
52
     * @return Form
53
     */
54 16
    protected function getBoundForm(): Form
55
    {
56 16
        if (!$this->boundForm) {
57 1
            throw new RuleException('Set form first!');
58
        }
59 15
        return $this->boundForm;
60
    }
61
}
62