Passed
Push — 4 ( e6ea10...17f4cc )
by Garion
08:03 queued 10s
created

Tip::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace SilverStripe\Forms;
5
6
use InvalidArgumentException;
7
8
/**
9
 * Represents a Tip which can be rendered alongside a form field in the front-end.
10
 * See the Tip component in the silverstripe/admin module.
11
 */
12
class Tip
13
{
14
    /**
15
     * These map to levels in the front-end Tip component
16
     */
17
    public const IMPORTANCE_LEVELS = [
18
        'NORMAL' => 'normal',
19
        'HIGH' => 'high',
20
    ];
21
22
    private const DEFAULT_ICON = 'lamp';
23
24
    private const DEFAULT_IMPORTANCE_LEVEL = self::IMPORTANCE_LEVELS['NORMAL'];
25
26
    /**
27
     * @var string The icon that should be used on the Tip button
28
     */
29
    private $icon;
30
31
    /**
32
     * @var string How important the tip is (normal or high). Informs the color and description.
33
     */
34
    private $importance_level;
35
36
    /**
37
     * @var string The message to display in the tip
38
     */
39
    private $message;
40
41
    /**
42
     * @param string $message The message to display in the tip
43
     * @param string $importance_level How important the tip is (normal or high). Informs the color and description.
44
     * @param string $icon The icon that should be used on the Tip button
45
     * @throws InvalidArgumentException
46
     */
47
    public function __construct(
48
        string $message,
49
        string $importance_level = self::DEFAULT_IMPORTANCE_LEVEL,
50
        string $icon = self::DEFAULT_ICON
51
    ) {
52
        $this->setMessage($message);
53
        $this->setIcon($icon);
54
        $this->setImportanceLevel($importance_level);
55
    }
56
57
    /**
58
     * Outputs props to be passed to the front-end Tip component.
59
     *
60
     * @return array
61
     */
62
    public function getTipSchema(): array
63
    {
64
        return [
65
            'content' => $this->getMessage(),
66
            'icon' => $this->getIcon(),
67
            'importance' => $this->getImportanceLevel(),
68
        ];
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getImportanceLevel(): string
75
    {
76
        return $this->importance_level;
77
    }
78
79
    /**
80
     * @param string $importance_level
81
     * @return Tip
82
     * @throws InvalidArgumentException
83
     */
84
    public function setImportanceLevel(string $importance_level): self
85
    {
86
        if (!in_array($importance_level, self::IMPORTANCE_LEVELS)) {
87
            throw new InvalidArgumentException(
88
                'Provided importance level must be defined in Tip::IMPORTANCE_LEVELS'
89
            );
90
        }
91
92
        $this->importance_level = $importance_level;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getIcon(): string
101
    {
102
        return $this->icon;
103
    }
104
105
    /**
106
     * @param string $icon
107
     * @return Tip
108
     */
109
    public function setIcon(string $icon): self
110
    {
111
        $this->icon = $icon;
112
113
        return $this;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getMessage(): string
120
    {
121
        return $this->message;
122
    }
123
124
    /**
125
     * @param string $message
126
     * @return Tip
127
     */
128
    public function setMessage(string $message): self
129
    {
130
        $this->message = $message;
131
132
        return $this;
133
    }
134
}
135