Completed
Pull Request — master (#1815)
by
unknown
07:13
created

TelephoneLink   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 56
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setTelephone() 0 16 2
A getTelephone() 0 4 1
A getHref() 0 4 2
A getTarget() 0 4 1
1
<?php
2
3
namespace luya\web;
4
5
use yii\base\BaseObject;
6
use yii\validators\RegularExpressionValidator;
7
8
/**
9
 * Telephone Link.
10
 *
11
 * Represent a {{luya\web\LinkInterface}} of an telephone link.
12
 *
13
 * @author Bennet Klarhölter <[email protected]>
14
 * @since 1.0.9
15
 */
16
class TelephoneLink extends BaseObject implements LinkInterface
17
{
18
    use LinkTrait;
19
20
    private $_telephone;
21
22
    /**
23
     * Setter method for telephone number.
24
     *
25
     * If no valid telephone is provided, not value is set.
26
     *
27
     * @param string $telephone The telephone number which should be used for the tel link.
28
     */
29
    public function setTelephone($telephone)
30
    {
31
        /**
32
         * Hack to support leading + sign
33
         * @see \luya\cms\models\NavItemPageBlockItem::rules()
34
         * @link https://github.com/luyadev/luya/pull/1815
35
         */
36
        $telephone = ltrim($telephone, '\\');
37
38
        $validator = new RegularExpressionValidator([
39
            'pattern' => '#^(?:0|\+[0-9]{2})[\d- ]+$#'
40
        ]);
41
        if ($validator->validate($telephone, $error)) {
42
            $this->_telephone = $telephone;
43
        }
44
    }
45
46
    /**
47
     * Getter method for the telephone.
48
     *
49
     * @return string Returns the telephone from the setter method, if telephone is not valid null is returned.
50
     */
51
    public function getTelephone()
52
    {
53
        return $this->_telephone;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function getHref()
60
    {
61
        return empty($this->getTelephone()) ?: 'tel:' . $this->getTelephone();
0 ignored issues
show
Bug Compatibility introduced by
The expression empty($this->getTelephon... $this->getTelephone(); of type boolean|string adds the type boolean to the return on line 61 which is incompatible with the return type declared by the interface luya\web\LinkInterface::getHref of type string.
Loading history...
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public function getTarget()
68
    {
69
        return '_self';
70
    }
71
}
72