Completed
Push — master ( fe0f99...6b22ab )
by Nate
03:54
created

Base::getIdentifier()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
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
    public $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
     * @return string
54
     */
55
    public function getIdentifier(): string
56
    {
57
        if ($this->identifier === null) {
58
            $this->identifier = StringHelper::randomString();
59
        }
60
        return $this->identifier;
61
    }
62
63
    /**
64
     * @param string $identifier
65
     * @return string
66
     */
67
    public function setIdentifier(string $identifier): string
68
    {
69
        $this->identifier = $identifier;
70
        return $this;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function properties(): array
77
    {
78
        return array_diff($this->attributes(), $this->settings());
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    public function getProperties(): array
85
    {
86
        $properties = [];
87
88
        foreach ($this->properties() as $property) {
89
            $properties[$property] = $this->$property;
90
        }
91
92
        return $properties;
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function settings(): array
99
    {
100
        return [];
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function getSettings(): array
107
    {
108
        $settings = [];
109
110
        foreach ($this->settings() as $attribute) {
111
            $settings[$attribute] = $this->$attribute;
112
        }
113
114
        return $settings;
115
    }
116
117
    /**
118
     * @param array $attributes
119
     * @return string
120
     */
121
    public function getHtml(array $attributes = []): string
122
    {
123
        $defaults = [
124
            'href' => $this->getUrl(),
125
            'title' => $this->getText(),
126
        ];
127
128
        $text = ArrayHelper::remove($attributes, 'text', $this->getText());
129
130
        $properties = array_filter(array_merge(
131
            $defaults,
132
            $attributes
133
        ));
134
135
        array_walk($properties, function (&$v, $k) {
136
            $v = $k . '="' . $v . '"';
137
        });
138
139
        return '<a ' . implode(' ', $properties) . '>' . $text . '</a>';
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function __toString()
146
    {
147
        return $this->getHtml();
148
    }
149
}
150