1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Form\Field; |
6
|
|
|
|
7
|
|
|
class Text extends Field |
8
|
|
|
{ |
9
|
|
|
use PlainInput; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $icon = 'fa-pencil'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Add custom fa-icon. |
18
|
|
|
* |
19
|
|
|
* @param string $icon |
20
|
|
|
* |
21
|
|
|
* @return $this |
22
|
|
|
*/ |
23
|
|
|
public function addIcon($icon) |
24
|
|
|
{ |
25
|
|
|
$this->icon = $icon; |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Render this filed. |
32
|
|
|
* |
33
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
34
|
|
|
*/ |
35
|
|
|
public function render() |
36
|
|
|
{ |
37
|
|
|
$this->initPlainInput(); |
38
|
|
|
|
39
|
|
|
$this->prepend('<i class="fa '.$this->icon.' fa-fw"></i>') |
40
|
|
|
->defaultAttribute('type', 'text') |
41
|
|
|
->defaultAttribute('id', $this->id) |
42
|
|
|
->defaultAttribute('name', $this->elementName ?: $this->formatName($this->column)) |
|
|
|
|
43
|
|
|
->defaultAttribute('value', old($this->elementName ?: $this->column, $this->value())) |
|
|
|
|
44
|
|
|
->defaultAttribute('class', 'form-control '.$this->getElementClassString()) |
45
|
|
|
->defaultAttribute('placeholder', $this->getPlaceholder()); |
46
|
|
|
|
47
|
|
|
$this->addVariables([ |
48
|
|
|
'prepend' => $this->prepend, |
49
|
|
|
'append' => $this->append, |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
return parent::render(); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Add inputmask to an elements. |
57
|
|
|
* |
58
|
|
|
* @param array $options |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function inputmask($options) |
63
|
|
|
{ |
64
|
|
|
$options = $this->json_encode_options($options); |
65
|
|
|
|
66
|
|
|
$this->script = "$('{$this->getElementClassSelector()}').inputmask($options);"; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Encode options to Json. |
73
|
|
|
* |
74
|
|
|
* @param array $options |
75
|
|
|
* |
76
|
|
|
* @return $json |
|
|
|
|
77
|
|
|
*/ |
78
|
|
|
protected function json_encode_options($options) |
79
|
|
|
{ |
80
|
|
|
$data = $this->prepare_options($options); |
81
|
|
|
|
82
|
|
|
$json = json_encode($data['options']); |
83
|
|
|
|
84
|
|
|
$json = str_replace($data['toReplace'], $data['original'], $json); |
85
|
|
|
|
86
|
|
|
return $json; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Prepare options. |
91
|
|
|
* |
92
|
|
|
* @param array $options |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function prepare_options($options) |
97
|
|
|
{ |
98
|
|
|
$original = []; |
99
|
|
|
$toReplace = []; |
100
|
|
|
|
101
|
|
|
foreach ($options as $key => &$value) { |
102
|
|
|
if (is_array($value)) { |
103
|
|
|
$subArray = $this->prepare_options($value); |
104
|
|
|
$value = $subArray['options']; |
105
|
|
|
$original = array_merge($original, $subArray['original']); |
106
|
|
|
$toReplace = array_merge($toReplace, $subArray['toReplace']); |
107
|
|
|
} elseif (preg_match('/function.*?/', $value)) { |
108
|
|
|
$original[] = $value; |
109
|
|
|
$value = "%{$key}%"; |
110
|
|
|
$toReplace[] = "\"{$value}\""; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return compact('original', 'toReplace', 'options'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Add datalist element to Text input. |
119
|
|
|
* |
120
|
|
|
* @param array $entries |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function datalist($entries = []) |
125
|
|
|
{ |
126
|
|
|
$this->defaultAttribute('list', "list-{$this->id}"); |
127
|
|
|
|
128
|
|
|
$datalist = "<datalist id=\"list-{$this->id}\">"; |
129
|
|
|
foreach ($entries as $k => $v) { |
130
|
|
|
$datalist .= "<option value=\"{$k}\">{$v}</option>"; |
131
|
|
|
} |
132
|
|
|
$datalist .= '</datalist>'; |
133
|
|
|
|
134
|
|
|
return $this->append($datalist); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.