summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/web/src/FormElement/TermInput/RegisteredTerm.php
blob: dd79dd18cfa7eb0d5149726e76649b0d906944b4 (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
<?php

namespace ipl\Web\FormElement\TermInput;

class RegisteredTerm implements Term
{
    /** @var string The search value */
    protected $value;

    /** @var ?string The label */
    protected $label;

    /** @var ?string The CSS class */
    protected $class;

    /** @var string The failure message */
    protected $message;

    /** @var string The validation constraint */
    protected $pattern;

    /**
     * Create a new RegisteredTerm
     *
     * @param string $value The search value
     */
    public function __construct(string $value)
    {
        $this->setSearchValue($value);
    }

    public function setSearchValue(string $value): self
    {
        $this->value = $value;

        return $this;
    }

    public function getSearchValue(): string
    {
        return $this->value;
    }

    public function setLabel(string $label): self
    {
        $this->label = $label;

        return $this;
    }

    public function getLabel(): ?string
    {
        return $this->label;
    }

    public function setClass(string $class): self
    {
        $this->class = $class;

        return $this;
    }

    public function getClass(): ?string
    {
        return $this->class;
    }

    public function setMessage(string $message): self
    {
        $this->message = $message;

        return $this;
    }

    public function getMessage(): ?string
    {
        return $this->message;
    }

    public function setPattern(string $pattern): self
    {
        $this->pattern = $pattern;

        return $this;
    }

    public function getPattern(): ?string
    {
        if ($this->message === null) {
            return null;
        }

        return $this->pattern ?? sprintf(Term::DEFAULT_CONSTRAINT, $this->getLabel() ?? $this->getSearchValue());
    }

    /**
     * Render this term as a string
     *
     * Pass the separator being used to separate multiple terms. If the term's value contains it,
     * the result will be automatically quoted.
     *
     * @param string $separator
     *
     * @return string
     */
    public function render(string $separator): string
    {
        if (strpos($this->value, $separator) !== false) {
            return '"' . $this->value . '"';
        }

        return $this->value;
    }

    /**
     * Apply the given term data to this term
     *
     * @param array $termData
     *
     * @return void
     */
    public function applyTermData(array $termData): void
    {
        if (isset($termData['search'])) {
            $this->value = $termData['search'];
        }

        if (isset($termData['label'])) {
            $this->setLabel($termData['label']);
        }

        if (isset($termData['class'])) {
            $this->setClass($termData['class']);
        }

        if (isset($termData['invalidMsg'])) {
            $this->setMessage($termData['invalidMsg']);
        }

        if (isset($termData['pattern'])) {
            $this->setPattern($termData['pattern']);
        }
    }
}