-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
179 lines (167 loc) · 3.39 KB
/
code.power
File metadata and controls
179 lines (167 loc) · 3.39 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
/**
* HTML form input field name.
*
* This value determines which $_FILES entry the upload handler reads.
*
* @var string
* @since 5.1.4
*/
protected string $field;
/**
* File type identifier.
*
* Common values:
* - "file"
* - "image"
* - "media"
* - "document"
*
* This value is passed directly to the upload handler in order to select
* the appropriate validation pipeline.
*
* @var string
* @since 5.1.4
*/
protected string $type;
/**
* Upload filter rule.
*
* Determines the sanitization and validation strategy applied by the
* upload handler.
*
* Example values:
* - "safe"
* - "clean"
* - "none"
*
* @var string|null
* @since 5.1.4
*/
protected ?string $filter;
/**
* Target filesystem path for uploads.
*
* All uploaded files will be stored in this directory.
* The directory must exist and be writable.
*
* @var string
* @since 5.1.4
*/
protected string $path;
/**
* Allowed file extensions.
*
* Values must be defined without leading dots.
*
* Example:
* - ['jpg', 'png']
* - ['csv']
* - ['pdf', 'docx']
*
* @var array<int,string>
* @since 5.1.4
*/
protected array $formats;
/**
* Constructor.
*
* Required configuration keys:
* - `field`
* - `type`
* - `path`
*
* Optional configuration keys:
* - `filter`
* - `formats`
* - `crop`
*
* @param array $config File type configuration map.
*
* @throws \InvalidArgumentException If required keys are missing.
* @since 5.1.4
*/
public function __construct(array $config)
{
foreach (['field','type','path'] as $key)
{
if (empty($config[$key]))
{
throw new \InvalidArgumentException("Missing file type config: {$key}");
}
}
$this->field = (string) $config['field'];
$this->type = (string) $config['type'];
$this->filter = $config['filter'] ?? null;
$this->path = rtrim((string) $config['path'], '/');
$this->formats = (array) ($config['formats'] ?? []);
}
/**
* Get the upload field name.
*
* @return string
* @since 5.1.4
*/
public function field(): string
{
return $this->field;
}
/**
* Get the defined file type.
*
* @return string
* @since 5.1.4
*/
public function type(): string
{
return $this->type;
}
/**
* Get the upload filter mode.
*
* @return string|null
* @since 5.1.4
*/
public function filter(): ?string
{
return $this->filter;
}
/**
* Get the target filesystem path.
*
* @return string
* @since 5.1.4
*/
public function path(): string
{
return $this->path;
}
/**
* Get the allowed file extensions.
*
* @return array<int,string>
* @since 5.1.4
*/
public function formats(): array
{
return $this->formats;
}
/**
* Export the file type definition as an associative array.
*
* This returns the same structure used to construct the
* base TypeDefinition instance, allowing the configuration
* to be serialized, stored, or cloned cleanly.
*
* @return array The configuration map.
* @since 5.1.4
*/
public function toArray(): array
{
return [
'field' => $this->field,
'type' => $this->type,
'filter' => $this->filter,
'path' => $this->path,
'formats' => $this->formats,
];
}