Passed
Push — master ( ff1389...802d43 )
by Thomas
20:58 queued 07:34
created

TelInputField   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 31
c 4
b 0
f 1
dl 0
loc 111
rs 10
wmc 13

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getHiddenField() 0 3 1
A createHiddenInput() 0 4 1
A getDataFormat() 0 3 1
A setDataFormat() 0 3 1
A Type() 0 3 1
A __construct() 0 7 2
A requirements() 0 3 1
A setName() 0 4 1
A setValue() 0 4 1
A setTitle() 0 4 1
A extraClass() 0 3 1
A Field() 0 4 1
1
<?php
2
3
namespace LeKoala\FormElements;
4
5
use SilverStripe\Forms\HiddenField;
6
use SilverStripe\Forms\TextField;
7
use SilverStripe\View\Requirements;
8
use SilverStripe\ORM\FieldType\DBHTMLText;
9
10
/**
11
 * Store phone number in full international format
12
 * Have a proper db type to format phone number accordingly
13
 */
14
class TelInputField extends TextField
15
{
16
    const FORMAT_E164 = "E164";
17
    const FORMAT_INTERNATIONAL = "INTERNATIONAL";
18
    const FORMAT_NATIONAL = "NATIONAL";
19
    const FORMAT_RFC3966 = "RFC3966";
20
21
    use BaseElement;
22
23
    /**
24
     * @config
25
     * @var array<string,mixed>
26
     */
27
    private static $default_config = [];
0 ignored issues
show
introduced by
The private property $default_config is not used, and could be removed.
Loading history...
28
29
    /**
30
     * @config
31
     * @var boolean
32
     */
33
    private static $enable_requirements = true;
0 ignored issues
show
introduced by
The private property $enable_requirements is not used, and could be removed.
Loading history...
34
35
    /**
36
     * Format to use for storage
37
     *
38
     * @var string
39
     */
40
    private static $defaut_dataformat = null;
0 ignored issues
show
introduced by
The private property $defaut_dataformat is not used, and could be removed.
Loading history...
41
42
    protected HiddenField $hiddenField;
43
44
    public function __construct($name, $title = null, $value = '', $maxLength = null, $form = null)
45
    {
46
        $this->hiddenField = new HiddenField($name, $title, $value);
47
        parent::__construct($name, $title, $value, $maxLength, $form);
48
        $this->mergeDefaultConfig();
49
        if (self::config()->default_dataformat) {
50
            $this->setDataFormat(self::config()->default_dataformat);
51
        }
52
    }
53
54
    public function getHiddenField(): HiddenField
55
    {
56
        return $this->hiddenField;
57
    }
58
59
    public function Type()
60
    {
61
        return 'telinput';
62
    }
63
64
    public function extraClass()
65
    {
66
        return 'text ' . parent::extraClass();
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getDataFormat()
73
    {
74
        return $this->getElementAttribute('data-dataformat');
75
    }
76
77
    /**
78
     * The value you want when unmasking to hidden field
79
     *
80
     * @param string $dataformat The alias or "masked" to get the masked value as is
81
     * @return $this
82
     */
83
    public function setDataFormat($dataformat)
84
    {
85
        return $this->setElementAttribute('data-dataformat', $dataformat);
86
    }
87
88
    public function setTitle($title)
89
    {
90
        $this->hiddenField->setTitle($title);
91
        return parent::setTitle($title);
92
    }
93
94
    public function setName($name)
95
    {
96
        $this->hiddenField->setName($name);
97
        return parent::setName($name);
98
    }
99
100
    public function setValue($value, $data = null)
101
    {
102
        $this->hiddenField->setValue($value, $data);
103
        return parent::setValue($value, $data);
104
    }
105
106
    protected function createHiddenInput($properties = [])
0 ignored issues
show
Unused Code introduced by
The parameter $properties is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

106
    protected function createHiddenInput(/** @scrutinizer ignore-unused */ $properties = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108
        $html = $this->hiddenField->forTemplate();
109
        return $html;
110
    }
111
112
    /**
113
     * @param array<string,mixed> $properties
114
     * @return DBHTMLText|string
115
     */
116
    public function Field($properties = [])
117
    {
118
        $extraHtml = $this->createHiddenInput($properties);
119
        return $this->wrapInElement('tel-input', $properties, $extraHtml);
120
    }
121
122
    public static function requirements(): void
123
    {
124
        Requirements::javascript("lekoala/silverstripe-form-elements: client/custom-elements/tel-input.min.js");
125
    }
126
}
127