-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubmitter.php
More file actions
194 lines (166 loc) · 4.1 KB
/
Submitter.php
File metadata and controls
194 lines (166 loc) · 4.1 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Helpers Http Component
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Helpers\Http;
/**
* Dynamic request submitter class,
* Used to format request input.
*/
final class Submitter
{
use \FloatPHP\Kernel\TraitConfiguration,
\FloatPHP\Helpers\Framework\tr\TraitRequestable;
/**
* @access private
* @var array $vars, Request vars
* @var string $group, Request vars group
*/
private $vars;
private $group;
/**
* Init input.
*
* @access public
* @param string $group
*/
public function __construct(?string $group = null)
{
// Set input
$this->group = (new Catcher(request: ['--key' => $group]))->key;
$this->vars = $this->getVars();
}
/**
* Format request input.
*
* @access public
* @param array $input
* @return array
*/
public function format(?array $input = []) : array
{
if ( !$input ) {
$input = $this->getPost();
}
if ( !$this->isType('array', $input) ) {
$input = (array)$input;
}
$vars = $this->vars[$this->group] ?? [];
foreach ($input as $key => $value) {
if ( isset($vars[$key]) ) {
// Default
if ( !$value && !$this->isType('null', $vars[$key]['default']) ) {
$value = $vars[$key]['default'];
}
// Format
if ( $vars[$key]['type'] == 'string' ) {
$value = (string)$value;
} elseif ( $vars[$key]['type'] == 'int' ) {
$value = (int)$value;
} elseif ( $vars[$key]['type'] == 'float' ) {
$value = (float)$value;
} elseif ( $vars[$key]['type'] == 'bool' ) {
$value = (bool)$value;
}
// Length
if ( $vars[$key]['length'] && $this->isType('string', $value) ) {
$value = substr($value, 0, $vars[$key]['length']);
}
// Filter
if ( !$this->isType('null', $vars[$key]['filter']) ) {
if ( !$this->isType($vars[$key]['filter'], $value) ) {
$value = '';
}
}
// List
if ( $this->inArray('list', $vars[$key]['functions']) ) {
$list = explode("\n", $value);
foreach ($list as $n => $item) {
$item = $this->formatSpace((string)$item);
if ( empty($item) ) {
unset($list[$n]);
} else {
$list[$n] = $this->formatSpace((string)$item);
}
}
$value = implode(',', $list);
}
// Serialize
if ( $this->inArray('serialize', $vars[$key]['functions']) ) {
$value = $this->serialize($value);
}
// Lowercase
if ( $this->inArray('lowercase', $vars[$key]['functions']) ) {
$value = $this->lowercase((string)$value);
}
// Uppercase
if ( $this->inArray('uppercase', $vars[$key]['functions']) ) {
$value = $this->uppercase((string)$value);
}
// Capitalize
if ( $this->inArray('capitalize', $vars[$key]['functions']) ) {
$value = $this->capitalize((string)$value);
}
// Slugify
if ( $this->inArray('slugify', $vars[$key]['functions']) ) {
$value = $this->slugify((string)$value);
}
// Switch
if ( $this->inArray('switch', $vars[$key]['functions']) ) {
if ( $vars[$key]['type'] == 'int' ) {
$value = ($value == 'on') ? 1 : 0;
} else {
$value = ($value == 'on') ? true : false;
}
}
$input[$key] = $value;
}
}
return $input;
}
/**
* Read request input.
*
* @access public
* @param array $input
* @return array
*/
public function read(array $input) : array
{
$vars = $this->vars[$this->group] ?? [];
$input = $this->format($input);
foreach ($input as $key => $value) {
if ( isset($vars[$key]) ) {
// Unlist
if ( $this->inArray('list', $vars[$key]['functions']) ) {
$input[$key] = implode("\n", explode(',', $value));
}
}
}
return $input;
}
/**
* Format request id.
*
* @access public
* @param mixed $id
* @param bool $force
* @return int
*/
public static function formatId($id, bool $force = false) : int
{
if ( $force ) {
return intval($id);
}
return (int)$id;
}
}