Completed
Pull Request — master (#1815)
by
unknown
03:08
created

TelephoneLink::getHref()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace luya\web;
4
5
use luya\cms\models\NavItemPageBlockItem;
6
use luya\tag\tags\TelTag;
7
use yii\base\BaseObject;
8
use yii\validators\RegularExpressionValidator;
9
10
/**
11
 * Telephone Link.
12
 *
13
 * Represent a {{luya\web\LinkInterface}} of an telephone link.
14
 *
15
 * @author Bennet Klarhölter <[email protected]>
16
 * @since 1.0.9
17
 */
18
class TelephoneLink extends BaseObject implements LinkInterface
19
{
20
    use LinkTrait;
21
22
    private $_telephone;
23
24
    /**
25
     * Setter method for telephone number.
26
     *
27
     * If no valid telephone is provided, not value is set.
28
     *
29
     * @param string $telephone The telephone number which should be used for the tel link.
30
     */
31
    public function setTelephone($telephone)
32
    {
33
        /**
34
         * Hack to support leading + sign
35
         * @see ArrayHelper::typeCast in NavItemPageBlockItem::rules()
36
         */
37
        $telephone = ltrim($telephone, '\\');
38
39
        $validator = new RegularExpressionValidator([
40
            'pattern' => '#^(?:0|\+[0-9]{2})[\d- ]+$#'
41
        ]);
42
        if ($validator->validate($telephone, $error)) {
43
            $this->_telephone = $telephone;
44
        }
45
    }
46
47
    /**
48
     * Getter method for the telephone.
49
     *
50
     * @return string Returns the telephone from the setter method, if telephone is not valid null is returned.
51
     */
52
    public function getTelephone()
53
    {
54
        return $this->_telephone;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function getHref()
61
    {
62
        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 62 which is incompatible with the return type declared by the interface luya\web\LinkInterface::getHref of type string.
Loading history...
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function getTarget()
69
    {
70
        return '_self';
71
    }
72
}
73