Passed
Push — master ( a8d237...a8074a )
by Thomas
11:47
created

CountryPhoneField::setDataFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace LeKoala\PhoneNumber;
4
5
use Exception;
6
use SilverStripe\Forms\FieldGroup;
7
use libphonenumber\PhoneNumberFormat;
8
use SilverStripe\Forms\DropdownField;
9
10
/**
11
 * A country/phone combo field
12
 */
13
class CountryPhoneField extends FieldGroup
14
{
15
    protected $dataformat = null;
16
17
    public function __construct($name, $title = null, $value = null)
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

17
    public function __construct($name, $title = null, /** @scrutinizer ignore-unused */ $value = null)

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...
18
    {
19
        $country = new DropdownField($name . "[CountryCode]", "");
20
        $country->setSource(PhoneHelper::getCountriesList());
21
        $country->setHasEmptyDefault(true);
22
        $country->setAttribute('style', 'max-width:166px'); // Match FieldGroup min width
23
        $country->setAttribute('size', 1); // fix some weird sizing issue in cms
24
25
        $number = new PhoneField($name . "[Number]", "");
26
27
        parent::__construct($title, $country, $number);
28
29
        $this->name = $name;
30
    }
31
32
33
    public function hasData()
34
    {
35
        // Turn this into a datafield
36
        return true;
37
    }
38
39
    /**
40
     * @return DropdownField
41
     */
42
    public function getCountryField()
43
    {
44
        return $this->fieldByName($this->name . "[CountryCode]");
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fieldByName($this->name . '[CountryCode]') targeting SilverStripe\Forms\CompositeField::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
45
    }
46
47
    /**
48
     * @return PhoneField
49
     */
50
    public function getPhoneField()
51
    {
52
        return $this->fieldByName($this->name . "[Number]");
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fieldByName($this->name . '[Number]') targeting SilverStripe\Forms\CompositeField::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53
    }
54
55
    public function getDataFormat()
56
    {
57
        return $this->dataformat;
58
    }
59
60
    public function setDataFormat($v)
61
    {
62
        $this->dataformat = $v;
63
        return $this;
64
    }
65
66
    public function setValue($value, $data = null)
67
    {
68
        // An array of value to assign to sub fields
69
        if (is_array($value)) {
70
            foreach ($value as $k => $v) {
71
                $this->fieldByName($this->name . "[$k]")->setValue($v);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fieldByName($this->name . '['.$k.']') targeting SilverStripe\Forms\CompositeField::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
72
            }
73
            return;
74
        }
75
        // It's an international number
76
        if (strpos((string)$value, '+') === 0) {
77
            $util = PhoneHelper::getPhoneNumberUtil();
78
            try {
79
                $number = $util->parse($value);
80
                $regionCode = $util->getRegionCodeForNumber($number);
81
                $this->getCountryField()->setValue($regionCode);
82
                $phone = $util->format($number, PhoneNumberFormat::NATIONAL);
83
                $this->getPhoneField()->setValue($phone);
84
            } catch (Exception $ex) {
85
                // We were unable to parse, simply set the value as is
86
                $this->getPhoneField()->setValue($phone);
87
            }
88
        } else {
89
            $this->getPhoneField()->setValue($value);
90
        }
91
    }
92
93
    /**
94
     * Value in E164 format (no formatting)
95
     *
96
     * @return string
97
     */
98
    public function dataValue()
99
    {
100
        $countryValue = $this->getCountryField()->Value();
101
        $phoneValue = $this->getPhoneField()->Value();
102
        if (!$phoneValue) {
103
            return '';
104
        }
105
        if (!$countryValue && strpos((string)$phoneValue, '+') !== 0) {
106
            return $phoneValue;
107
        }
108
109
        $util = PhoneHelper::getPhoneNumberUtil();
110
111
        try {
112
            $number = $util->parse($phoneValue, $countryValue);
113
            $format = $this->dataformat ?? PhoneNumberFormat::E164;
114
            return $util->format($number, $format);
115
        } catch (Exception $ex) {
116
            // We were unable to parse, simply return the value as is
117
            return $phoneValue;
118
        }
119
    }
120
}
121