Passed
Push — master ( 779d84...3d5507 )
by Vincent
05:31
created

FormattedPhoneElement::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Bdf\Form\Phone;
4
5
use Bdf\Form\Child\ChildInterface;
6
use Bdf\Form\Child\Http\HttpFieldPath;
7
use Bdf\Form\ElementInterface;
8
use Bdf\Form\Error\FormError;
9
use Bdf\Form\Leaf\LeafRootElement;
10
use Bdf\Form\RootElementInterface;
11
use Bdf\Form\View\ElementViewInterface;
12
use libphonenumber\PhoneNumber;
13
14
/**
15
 * Decorate a PhoneElement to handle string phone number instead of instance of PhoneNumber
16
 *
17
 * @see FormattedPhoneElementBuilder For build the element
18
 * @see PhoneElement The decored element
19
 */
20
class FormattedPhoneElement implements ElementInterface
21
{
22
    /**
23
     * @var PhoneElement
24
     * @readonly
25
     */
26
    private $element;
27
28
    /**
29
     * @var int
30
     * @readonly
31
     */
32
    private $format;
33
34
    /**
35
     * @var ChildInterface|null
36
     */
37
    private $container;
38
39
    /**
40
     * FormattedPhoneElement constructor.
41
     *
42
     * @param PhoneElement $element
43
     * @param \libphonenumber\PhoneNumberFormat::* $format The phone format. Must be one of the constant PhoneNumberFormat::*
0 ignored issues
show
Documentation Bug introduced by
The doc comment $format at position 0 could not be parsed: Unknown type name '$format' at position 0 in $format.
Loading history...
44
     */
45 33
    public function __construct(PhoneElement $element, int $format)
46
    {
47 33
        $this->element = $element;
48 33
        $this->format = $format;
49 33
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 22
    public function submit($data): ElementInterface
55
    {
56 22
        $this->element->submit($data);
57
58 22
        return $this;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 2
    public function patch($data): ElementInterface
65
    {
66 2
        $this->element->patch($data);
67
68 2
        return $this;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 5
    public function import($entity): ElementInterface
75
    {
76 5
        $this->element->import($entity !== null ? $this->element->parseValue($entity) : null);
77
78 5
        return $this;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 20
    public function value(): ?string
85
    {
86 20
        $phone = $this->element->value();
87
88 20
        if (!$phone instanceof PhoneNumber) {
0 ignored issues
show
introduced by
$phone is always a sub-type of libphonenumber\PhoneNumber.
Loading history...
89 4
            return $phone;
90
        }
91
92 17
        return $this->element->getFormatter()->format($phone, $this->format);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->element->g...($phone, $this->format) returns the type string which is incompatible with the return type mandated by Bdf\Form\ElementInterface::value() of Bdf\Form\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 9
    public function httpValue()
99
    {
100 9
        return $this->element->httpValue();
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 16
    public function valid(): bool
107
    {
108 16
        return $this->element->valid();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 17
    public function error(?HttpFieldPath $field = null): FormError
115
    {
116 17
        return $this->element->error($field);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 3
    public function container(): ?ChildInterface
123
    {
124 3
        return $this->container;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 4
    public function setContainer(ChildInterface $container): ElementInterface
131
    {
132 4
        $newElement = clone $this;
133
134 4
        $newElement->element = $newElement->element->setContainer($container);
135 4
        $newElement->container = $container;
136
137 4
        return $newElement;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 2
    public function root(): RootElementInterface
144
    {
145
        // @todo save the root ?
146 2
        if (!$this->container) {
147 1
            return new LeafRootElement($this);
148
        }
149
150 1
        return $this->container->parent()->root();
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 1
    public function view(?HttpFieldPath $field = null): ElementViewInterface
157
    {
158 1
        return $this->element->view($field);
159
    }
160
}
161