Completed
Push — master ( 5436bf...acf3d3 )
by Nate
06:05
created

Url::getHtml()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/link/license
6
 * @link       https://www.flipboxfactory.com/software/link/
7
 */
8
9
namespace flipbox\link\types;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\link\fields\Link;
14
use yii\base\Model;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class Url extends AbstractType
21
{
22
    /**
23
     * @var
24
     */
25
    public $url;
26
27
    /**
28
     * @var string
29
     */
30
    protected $identifier = 'url';
31
32
    /**
33
     * @var bool
34
     */
35
    public $showText = true;
36
37
    /**
38
     * @var bool
39
     */
40
    public $showTarget = false;
41
42
    /**
43
     * @var string
44
     */
45
    public $target = "_self";
46
47
    /**
48
     * @var string|null The input’s placeholder text
49
     */
50
    public $placeholder;
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public static function displayName(): string
56
    {
57
        return Craft::t('link', 'Url');
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getUrl(): string
64
    {
65
        return $this->url ?: '';
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function settingsHtml(): string
72
    {
73
        return Craft::$app->getView()->renderTemplate(
74
            'link/_components/fieldtypes/Link/types/url/settings',
75
            [
76
                'type' => $this
77
            ]
78
        );
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function settings(): array
85
    {
86
        return [
87
            'showText',
88
            'placeholder',
89
            'showTarget'
90
        ];
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96
    public function rules()
97
    {
98
        return array_merge(
99
            parent::rules(),
100
            [
101
                [
102
                    [
103
                        'url'
104
                    ],
105
                    'url',
106
                    'defaultScheme' => '',
107
                    'on' => [
108
                        self::SCENARIO_INPUT
109
                    ]
110
                ],
111
                [
112
                    [
113
                        'url'
114
                    ],
115
                    'required',
116
                    'on' => [
117
                        self::SCENARIO_INPUT
118
                    ]
119
                ],
120
                [
121
                    [
122
                        'url'
123
                    ],
124
                    'safe',
125
                    'on' => [
126
                        Model::SCENARIO_DEFAULT
127
                    ]
128
                ]
129
            ]
130
        );
131
    }
132
133
134
    /**
135
     * @inheritdoc
136
     */
137
    public function inputHtml(Link $field, TypeInterface $type = null, ElementInterface $element = null): string
138
    {
139
        return Craft::$app->getView()->renderTemplate(
140
            'link/_components/fieldtypes/Link/types/url/input',
141
            [
142
                'value' => $type,
143
                'element' => $element,
144
                'type' => $this,
145
                'field' => $field
146
            ]
147
        );
148
    }
149
150
    /**
151
     * @param array $attributes
152
     * @return string
153
     */
154
    public function getHtml(array $attributes = []): string
155
    {
156
        if ($this->showTarget && $this->target) {
157
            $attributes['target'] = "_blank";
158
        }
159
160
        return parent::getHtml($attributes);
161
    }
162
}
163