Completed
Push — master ( aaa32f...d77fc9 )
by Nate
06:10
created

Base::setText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
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\traits;
10
11
use craft\helpers\ArrayHelper;
12
use craft\helpers\StringHelper;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
trait Base
19
{
20
21
    /**
22
     * @var string
23
     */
24
    protected $text;
25
26
    /**
27
     * @var string
28
     */
29
    protected $identifier;
30
31
    /**
32
     * @return string
33
     */
34
    abstract public function getUrl(): string;
35
36
    /**
37
     * Returns the list of attribute names.
38
     * By default, this method returns all public non-static properties of the class.
39
     * You may override this method to change the default behavior.
40
     * @return array list of attribute names.
41
     */
42
    abstract protected function attributes();
43
44
    /**
45
     * @return string
46
     */
47
    public function getText(): string
48
    {
49
        return $this->text ?: '';
50
    }
51
52
    /**
53
     * @param string $text
54
     * @return static
55
     */
56
    public function setText(string $text)
57
    {
58
        $this->text = $text;
59
        return $this;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getIdentifier(): string
66
    {
67
        if ($this->identifier === null) {
68
            $this->identifier = StringHelper::randomString();
69
        }
70
        return $this->identifier;
71
    }
72
73
    /**
74
     * @param string $identifier
75
     * @return string
76
     */
77
    public function setIdentifier(string $identifier): string
78
    {
79
        $this->identifier = $identifier;
80
        return $this;
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function properties(): array
87
    {
88
        return array_diff($this->attributes(), $this->settings());
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function getProperties(): array
95
    {
96
        $properties = [];
97
98
        foreach ($this->properties() as $property) {
99
            $properties[$property] = $this->$property;
100
        }
101
102
        return $properties;
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function settings(): array
109
    {
110
        return [];
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function getSettings(): array
117
    {
118
        $settings = [];
119
120
        foreach ($this->settings() as $attribute) {
121
            $settings[$attribute] = $this->$attribute;
122
        }
123
124
        return $settings;
125
    }
126
127
    /**
128
     * @param array $attributes
129
     * @return string
130
     */
131
    public function getHtml(array $attributes = []): string
132
    {
133
        $defaults = [
134
            'href' => $this->getUrl(),
135
            'title' => $this->getText(),
136
        ];
137
138
        $text = ArrayHelper::remove($attributes, 'text', $this->getText());
139
140
        $properties = array_filter(array_merge(
141
            $defaults,
142
            $attributes
143
        ));
144
145
        array_walk($properties, function (&$v, $k) {
146
            $v = $k . '="' . $v . '"';
147
        });
148
149
        return '<a ' . implode(' ', $properties) . '>' . $text . '</a>';
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function __toString()
156
    {
157
        return $this->getHtml();
158
    }
159
}
160