aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/forms/Link.php
blob: 8aa182cf9bc58143c06a6dff8b4c4e10471f6d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Studip\Forms;

/**
 * The Link class represents a part of a form that displays a link.
 */
class Link extends Part
{
    protected $url;
    protected $label;
    protected $icon;
    protected $attributes = [];

    public function __construct(string $url, string $label, ?\Icon $icon = null)
    {
        $this->url = $url;
        $this->label = $label;
        $this->icon = $icon;
    }

    /**
     * Sets the url for the link.
     *
     * @param string $url
     * @return $this
     */
    public function setURL(string $url): Link
    {
        $this->url = $url;
        return $this;
    }

    /**
     * Returns the url for the link.
     * @return string
     */
    public function getURL(): string
    {
        return $this->url;
    }

    /**
     * Sets the label for the link.
     *
     * @param string $label
     * @return $this
     */
    public function setLabel(string $label): Link
    {
        $this->label = $label;
        return $this;
    }

    /**
     * Returns the label for the link.
     *
     * @return string
     */
    public function getLabel() : string
    {
        return $this->label;
    }

    /**
     * Sets the icon for the link. May be null to remove the icon.
     *
     * @param \Icon $icon
     * @return $this
     */
    public function setIcon(?\Icon $icon = null): Link
    {
        $this->icon = $icon;
        return $this;
    }

    /**
     * Returns the icon for the link.
     * @return \Icon|null
     */
    public function getIcon(): ?\Icon
    {
        return $this->icon;
    }

    /**
     * Replaces all attributes for the link.
     *
     * @param array $attributes
     * @return $this
     */
    public function setAttributes(array $attributes): Link
    {
        $this->attributes = $attributes;
        return $this;
    }

    /**
     * Adds/appends attributes to the current attributes for the link.
     *
     * @param array $attributes
     * @return $this
     */
    public function addAttributes(array $attributes): Link
    {
        $this->attributes = array_merge($this->attributes, $attributes);
        return $this;
    }

    /**
     * Sets a single attribute for the link.
     *
     * @param string $key
     * @param mixed $value
     * @return $this
     */
    public function setAttribute(string $key, $value): Link
    {
        $this->attributes[$key] = $value;
        return $this;
    }

    /**
     * Returns the attributes for the link.
     * @return array
     */
    public function getAttributes(): array
    {
        return $this->attributes;
    }

    /**
     * Removes an attribute.
     *
     * @param string $key
     * @param bool   $throw_exception Throw an exception if the attribute does not exists (default: false)
     * @return $this
     */
    public function removeAttribute(string $key, bool $throw_exception = false): Link
    {
        if (!isset($this->attributes[$key]) && $throw_exception) {
            throw new \RuntimeException("No attribute {$key} defined");
        }

        if (isset($this->attributes[$key])) {
            unset($this->attributes[$key]);
        }

        return $this;
    }

    /**
     * "Renders" the text: Either return it directly, if it is HTML or call htmlReady first before returning it.
     *
     * @return string The text that shall be placed in the form, either as HTML or plain text.
     */
    public function render()
    {
        return sprintf(
            '<div class="formpart"><a href="%1$s" %2$s>%3$s %4$s</a></div>',
            \URLHelper::getLink($this->url, [], true),
            arrayToHtmlAttributes($this->attributes),
            $this->icon ? $this->icon->asImg(['class' => 'text-bottom']) : '',
            htmlReady($this->label)
        );
    }
}