Url::getText()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 20
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://raw.githubusercontent.com/flipboxfactory/craft-link/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-link
7
 */
8
9
namespace flipbox\craft\link\types;
10
11
use craft\validators\UrlValidator;
12
use flipbox\craft\link\Link;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
class Url extends AbstractType
19
{
20
    /**
21
     * The base template path to the field type templates
22
     */
23
    const BASE_TEMPLATE_PATH = AbstractType::BASE_TEMPLATE_PATH . '/url';
24
25
    /**
26
     * The settings template path
27
     */
28
    const SETTINGS_TEMPLATE_PATH = self::BASE_TEMPLATE_PATH . '/settings';
29
30
    /**
31
     * The input template path
32
     */
33
    const INPUT_TEMPLATE_PATH = self::BASE_TEMPLATE_PATH . '/input';
34
35
    /**
36
     * @var bool
37
     */
38
    public $useUrlAsDefaultText = true;
39
40
    /**
41
     * @var bool
42
     */
43
    public $allowRelativeUrl = false;
44
    
45
    /**
46
     * @var string
47
     */
48
    public $url;
49
50
    /**
51
     * @var string|null The input’s placeholder text
52
     */
53
    public $placeholder;
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public static function displayName(): string
59
    {
60
        return Link::t('Url');
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function getText()
67
    {
68
        if ($this->allowText && $this->overrideText !== null) {
69
            return $this->overrideText;
70
        }
71
72
        return $this->useUrlAsDefaultText ? $this->getUrl() : null;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getUrl(): string
79
    {
80
        return $this->url ?: '';
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function settings(): array
87
    {
88
        return array_merge(
89
            parent::settings(),
90
            [
91
                'placeholder',
92
                'useUrlAsDefaultText',
93
                'allowRelativeUrl'
94
            ]
95
        );
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function attributes()
102
    {
103
        return array_merge(
104
            parent::attributes(),
105
            [
106
                'url'
107
            ]
108
        );
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public function rules()
115
    {
116
        return array_merge(
117
            parent::rules(),
118
            [
119
                [
120
                    [
121
                        'url'
122
                    ],
123
                    $this->allowRelativeUrl ? UrlValidator::class : 'url',
124
                    'on' => [
125
                        self::SCENARIO_INPUT
126
                    ]
127
                ],
128
                [
129
                    [
130
                        'url'
131
                    ],
132
                    'required',
133
                    'on' => [
134
                        self::SCENARIO_INPUT
135
                    ]
136
                ],
137
                [
138
                    [
139
                        'url',
140
                    ],
141
                    'safe',
142
                    'on' => [
143
                        self::SCENARIO_DEFAULT
144
                    ]
145
                ]
146
            ]
147
        );
148
    }
149
}
150