Completed
Push — master ( f39c4d...b2e354 )
by Sam
03:35 queued 03:17
created

FormMessage::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
use InvalidArgumentException;
6
use SilverStripe\ORM\ValidationResult;
7
use SilverStripe\View\ViewableData;
8
9
/**
10
 * Form component which contains a castable message
11
 *
12
 * @mixin ViewableData
13
 */
14
trait FormMessage
15
{
16
17
    /**
18
     * @var string
19
     */
20
    protected $message = '';
21
22
    /**
23
     * @var string
24
     */
25
    protected $messageType = '';
26
27
    /**
28
     * Casting for message
29
     *
30
     * @var string
31
     */
32
    protected $messageCast = null;
33
34
35
    /**
36
     * Returns the field message, used by form validation.
37
     *
38
     * Use {@link setError()} to set this property.
39
     *
40
     * @return string
41
     */
42
    public function getMessage()
43
    {
44
        return $this->message;
45
    }
46
47
    /**
48
     * Returns the field message type.
49
     *
50
     * Arbitrary value which is mostly used for CSS classes in the rendered HTML, e.g "required".
51
     *
52
     * Use {@link setError()} to set this property.
53
     *
54
     * @return string
55
     */
56
    public function getMessageType()
57
    {
58
        return $this->messageType;
59
    }
60
61
    /**
62
     * Casting type for this message. Will be 'text' or 'html'
63
     *
64
     * @return string
65
     */
66
    public function getMessageCast()
67
    {
68
        return $this->messageCast;
69
    }
70
71
    /**
72
     * Sets the error message to be displayed on the form field.
73
     *
74
     * Allows HTML content, so remember to use Convert::raw2xml().
75
     *
76
     * @param string $message Message string
77
     * @param string $messageType Message type
78
     * @param string $messageCast
79
     * @return $this
80
     */
81
    public function setMessage(
82
        $message,
83
        $messageType = ValidationResult::TYPE_ERROR,
84
        $messageCast = ValidationResult::CAST_TEXT
85
    ) {
86
        if (!in_array($messageCast, [ValidationResult::CAST_TEXT, ValidationResult::CAST_HTML])) {
87
            throw new InvalidArgumentException("Invalid message cast type");
88
        }
89
        $this->message = $message;
90
        $this->messageType = $messageType;
91
        $this->messageCast = $messageCast;
92
        return $this;
93
    }
94
95
    /**
96
     * Get casting helper for message cast, or null if not known
97
     *
98
     * @return string
99
     */
100
    protected function getMessageCastingHelper()
101
    {
102
        switch ($this->getMessageCast()) {
103
            case ValidationResult::CAST_TEXT:
104
                return 'Text';
105
            case ValidationResult::CAST_HTML:
106
                return 'HTMLFragment';
107
            default:
108
                return null;
109
        }
110
    }
111
112
    /**
113
     * Get form schema encoded message
114
     *
115
     * @return array|null Message in array format, or null if no message
116
     */
117
    public function getSchemaMessage()
118
    {
119
        $message = $this->getMessage();
120
        if (!$message) {
121
            return null;
122
        }
123
        // Form schema messages treat simple strings as plain text, so nest for html messages
124
        if ($this->getMessageCast() === ValidationResult::CAST_HTML) {
125
            $message = ['html' => $message];
126
        }
127
        return [
128
            'value' => $message,
129
            'type' => $this->getMessageType(),
130
        ];
131
    }
132
}
133