ARule   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 17
c 1
b 0
f 0
dl 0
loc 44
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sentInputs() 0 19 2
A setBoundForm() 0 3 1
A getBoundForm() 0 6 2
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