Completed
Push — master ( 11bd39...51b962 )
by Nate
01:41
created

Url::getText()   A

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 flipbox\craft\link\Link;
12
13
/**
14
 * @author Flipbox Factory <[email protected]>
15
 * @since 1.0.0
16
 */
17
class Url extends AbstractType
18
{
19
    /**
20
     * The base template path to the field type templates
21
     */
22
    const BASE_TEMPLATE_PATH = AbstractType::BASE_TEMPLATE_PATH . '/url';
23
24
    /**
25
     * The settings template path
26
     */
27
    const SETTINGS_TEMPLATE_PATH = self::BASE_TEMPLATE_PATH . '/settings';
28
29
    /**
30
     * The input template path
31
     */
32
    const INPUT_TEMPLATE_PATH = self::BASE_TEMPLATE_PATH . '/input';
33
34
    /**
35
     * @var bool
36
     */
37
    public $useUrlAsDefaultText = true;
38
    
39
    /**
40
     * @var string
41
     */
42
    public $url;
43
44
    /**
45
     * @var string|null The input’s placeholder text
46
     */
47
    public $placeholder;
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public static function displayName(): string
53
    {
54
        return Link::t('Url');
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function getText()
61
    {
62
        if ($this->allowText && $this->overrideText !== null) {
63
            return $this->overrideText;
64
        }
65
66
        return $this->useUrlAsDefaultText ? $this->getUrl() : null;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getUrl(): string
73
    {
74
        return $this->url ?: '';
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function settings(): array
81
    {
82
        return array_merge(
83
            parent::settings(),
84
            [
85
                'placeholder',
86
                'useUrlAsDefaultText'
87
            ]
88
        );
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function attributes()
95
    {
96
        return array_merge(
97
            parent::attributes(),
98
            [
99
                'url'
100
            ]
101
        );
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function rules()
108
    {
109
        return array_merge(
110
            parent::rules(),
111
            [
112
                [
113
                    [
114
                        'url'
115
                    ],
116
                    'url',
117
                    'defaultScheme' => '',
118
                    'on' => [
119
                        self::SCENARIO_INPUT
120
                    ]
121
                ],
122
                [
123
                    [
124
                        'url'
125
                    ],
126
                    'required',
127
                    'on' => [
128
                        self::SCENARIO_INPUT
129
                    ]
130
                ],
131
                [
132
                    [
133
                        'url',
134
                    ],
135
                    'safe',
136
                    'on' => [
137
                        self::SCENARIO_DEFAULT
138
                    ]
139
                ]
140
            ]
141
        );
142
    }
143
}
144