Completed
Pull Request — master (#1815)
by
unknown
05:37
created

TelephoneLink::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace luya\web;
4
5
use yii\base\BaseObject;
6
use yii\base\InvalidConfigException;
7
use yii\validators\RegularExpressionValidator;
8
9
/**
10
 * Telephone Link.
11
 *
12
 * Represent a {{luya\web\LinkInterface}} of an telephone link.
13
 *
14
 * @property $telephone
15
 *
16
 * @author Bennet Klarhölter <[email protected]>
17
 * @since 1.0.9
18
 */
19
class TelephoneLink extends BaseObject implements LinkInterface
20
{
21
    use LinkTrait;
22
23
    private $_telephone;
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public function init()
29
    {
30
        parent::init();
31
32
        if ($this->telephone === null) {
0 ignored issues
show
Bug introduced by
The property telephone does not seem to exist. Did you mean _telephone?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
33
            throw new InvalidConfigException('The telephone attribute can not be empty and must be set trough configuration array.');
34
        }
35
    }
36
37
    /**
38
     * Setter method for telephone number.
39
     *
40
     * If no valid telephone is provided, not value is set.
41
     *
42
     * @param string $telephone The telephone number which should be used for the tel link.
43
     */
44
    public function setTelephone($telephone)
45
    {
46
        /**
47
         * Hack to support leading + sign
48
         * @see \luya\cms\models\NavItemPageBlockItem::rules()
49
         * @link https://github.com/luyadev/luya/pull/1815
50
         */
51
        $telephone = ltrim($telephone, '\\');
52
53
        $validator = new RegularExpressionValidator([
54
            'pattern' => '#^(?:0|\+[0-9]{2})[\d- ()]+$#'
55
        ]);
56
        if ($validator->validate($telephone, $error)) {
57
            $this->_telephone = $telephone;
58
        }
59
    }
60
61
    /**
62
     * Getter method for the telephone.
63
     *
64
     * @return string Returns the telephone from the setter method, if telephone is not valid null is returned.
65
     */
66
    public function getTelephone()
67
    {
68
        return $this->_telephone;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function getHref()
75
    {
76
        $href = null;
77
78
        if (!empty($this->getTelephone())) {
79
            // Remove all chars expect digits and "+"
80
            $number = preg_replace('#[^\d+]#', '', $this->getTelephone());
81
            $href = 'tel:' . $number;
82
        }
83
84
        return $href;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function getTarget()
91
    {
92
        return '_self';
93
    }
94
}
95