Factory::getControl()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 4
rs 9.9
1
<?php
2
3
namespace kalanis\kw_forms\Controls;
4
5
6
use kalanis\kw_forms\Exceptions\FormsException;
7
use ReflectionClass;
8
use ReflectionException;
9
10
11
class Factory
12
{
13
    /** @var array<string, string> */
14
    protected static array $map = [
15
        'input' => Input::class,
16
        'text' => Text::class,
17
        'textarea' => Textarea::class,
18
        'email' => Email::class,
19
        'pass' => Password::class,
20
        'password' => Password::class,
21
        'phone' => Telephone::class,
22
        'telephone' => Telephone::class,
23
        'chk' => Checkbox::class,
24
        'check' => Checkbox::class,
25
        'checkbox' => Checkbox::class,
26
        'checkboxswitch' => CheckboxSwitch::class,
27
        'select' => Select::class,
28
        'selectbox' => Select::class,
29
        'radio' => Radio::class,
30
        'radioset' => RadioSet::class,
31
        'radiobutton' => Radio::class,
32
        'hidden' => Hidden::class,
33
        'date' => DatePicker::class,
34
        'datetime' => DateTimePicker::class,
35
        'daterange' => DateRange::class,
36
        'description' => Description::class,
37
        'desc' => Description::class,
38
        'html' => Html::class,
39
        'file' => File::class,
40
        'button' => Button::class,
41
        'accept' => Submit::class,
42
        'submit' => Submit::class,
43
        'cancel' => Reset::class,
44
        'reset' => Reset::class,
45
        'captchadis' => Security\Captcha\Disabled::class,
46
        'captchatext' => Security\Captcha\Text::class,
47
        'captchaplus' => Security\Captcha\Numerical::class,
48
        'nocaptcha' => Security\Captcha\NoCaptcha::class,
49
        'csrf' => Security\Csrf::class,
50
        'multisend' => Security\MultiSend::class,
51
    ];
52
53
    /**
54
     * Factory for getting classes of each input available by kw_forms
55
     * @param string $type
56
     * @throws FormsException
57
     * @return AControl
58
     */
59 42
    public function getControl(string $type): AControl
60
    {
61 42
        $type = strtolower($type);
62 42
        if (isset(static::$map[$type])) {
63 39
            $control = static::$map[$type];
64
            try {
65
                /** @var class-string $control */
66 39
                $ref = new ReflectionClass($control);
67 38
                $class = $ref->newInstance();
68 38
                if ($class instanceof AControl) {
69 38
                    return $class;
70
                }
71 1
            } catch (ReflectionException $ex) {
72 1
                throw new FormsException($ex->getMessage(), $ex->getCode(), $ex);
73
            }
74
75
        }
76 4
        throw new FormsException(sprintf('Unknown type %s ', $type));
77
    }
78
}
79