@@ -60,14 +60,14 @@ public function make(
60
60
)
61
61
{
62
62
$ useStrategy = $ strategy ?? $ this ->strategy ;
63
- $ useOptions = $ options !== null ? array_merge ( $ this ->options , $ options ) : $ this ->options ;
63
+ $ useOptions = $ options !== null ? $ this ->mergeOptions ( $ options , $ strategy , $ this -> options ) : $ this ->options ;
64
64
$ defaultFilename = $ defaultFilename ?? $ this ->defaultFilename ;
65
65
$ extension = $ extension ?? $ this ->defaultExtension ;
66
66
67
67
return match ($ useStrategy ) {
68
- 'random ' => $ this ->random ($ useOptions [ ' length ' ] ?? 16 , $ extension ),
68
+ 'random ' => $ this ->random ($ extension , $ useOptions ),
69
69
70
- 'uuid ' => $ this ->uuid ($ extension ),
70
+ 'uuid ' => $ this ->uuid ($ extension, $ useOptions ),
71
71
72
72
'timestamp ' => $ this ->timestamp ($ defaultFilename , $ extension , $ useOptions ),
73
73
@@ -87,16 +87,30 @@ public function make(
87
87
};
88
88
}
89
89
90
+ /**
91
+ * Merge options with the default options.
92
+ *
93
+ * @param array $options The options to merge.
94
+ * @param array $defaultOptions The default options.
95
+ * @return array The merged options.
96
+ */
97
+ private function mergeOptions (array $ options , string $ strategy , array $ defaultOptions ): array
98
+ {
99
+ return array_merge ($ defaultOptions [$ strategy ], $ options );
100
+ }
101
+
90
102
/**
91
103
* Generate a random string of characters.
92
104
*
93
105
* @param int|null $length The length of the random string.
94
106
* @param string|null $extension The extension of the file.
95
107
* @return string The random string.
96
108
*/
97
- public function random (int $ length , string $ extension )
109
+ public function random (string $ extension , ? array $ options = null )
98
110
{
99
- return Str::random ($ length ) . '. ' . $ extension ;
111
+ $ length = $ options ['length ' ] ?? 16 ;
112
+ $ filename = Str::random ($ length );
113
+ return $ this ->applyAffixes ($ filename , $ options ) . '. ' . $ extension ;
100
114
}
101
115
102
116
/**
@@ -105,9 +119,10 @@ public function random(int $length, string $extension)
105
119
* @param string|null $extension The extension of the file.
106
120
* @return string The UUID string.
107
121
*/
108
- public function uuid (string $ extension )
122
+ public function uuid (string $ extension, ? array $ options = null )
109
123
{
110
- return (string ) Str::uuid () . '. ' . $ extension ;
124
+ $ filename = (string ) Str::uuid ();
125
+ return $ this ->applyAffixes ($ filename , $ options ) . '. ' . $ extension ;
111
126
}
112
127
113
128
/**
@@ -118,11 +133,12 @@ public function uuid(string $extension)
118
133
* @param array $options The options array.
119
134
* @return string The timestamp string.
120
135
*/
121
- public function timestamp (string $ defaultFilename , string $ extension , array $ options )
136
+ public function timestamp (string $ defaultFilename , string $ extension , ? array $ options = null )
122
137
{
123
138
$ format = $ options ['format ' ] ?? 'Y-m-d_H-i-s ' ;
124
139
$ date = new DateTime ();
125
- return $ date ->format ($ format ) . '_ ' . $ defaultFilename . '. ' . $ extension ;
140
+ $ filename = $ date ->format ($ format ) . '_ ' . $ defaultFilename ;
141
+ return $ this ->applyAffixes ($ filename , $ options ) . '. ' . $ extension ;
126
142
}
127
143
128
144
/**
@@ -133,43 +149,12 @@ public function timestamp(string $defaultFilename, string $extension, array $opt
133
149
* @param array $options The options array.
134
150
* @return string The date string.
135
151
*/
136
- public function date (string $ defaultFilename , string $ extension , array $ options )
152
+ public function date (string $ defaultFilename , string $ extension , ? array $ options = null )
137
153
{
138
154
$ format = $ options ['format ' ] ?? 'Y-m-d ' ;
139
155
$ date = new DateTime ();
140
- return $ date ->format ($ format ) . '_ ' . $ defaultFilename . '. ' . $ extension ;
141
- }
142
-
143
- /**
144
- * Generate a prefix string.
145
- *
146
- * @param string|null $defaultFilename The original filename.
147
- * @param string|null $extension The extension of the file.
148
- * @param array $options The options array.
149
- * @return string The prefix string.
150
- */
151
- public function prefix (string $ defaultFilename , string $ extension , array $ options )
152
- {
153
- if (!isset ($ options ['prefix ' ])) {
154
- throw new \InvalidArgumentException ("The 'prefix' option is required for prefix strategy " );
155
- }
156
- return $ options ['prefix ' ] . $ defaultFilename . '. ' . $ extension ;
157
- }
158
-
159
- /**
160
- * Generate a suffix string.
161
- *
162
- * @param string|null $defaultFilename The original filename.
163
- * @param string|null $extension The extension of the file.
164
- * @param array $options The options array.
165
- * @return string The suffix string.
166
- */
167
- public function suffix (string $ defaultFilename , string $ extension , array $ options )
168
- {
169
- if (!isset ($ options ['suffix ' ])) {
170
- throw new \InvalidArgumentException ("The 'suffix' option is required for suffix strategy " );
171
- }
172
- return $ defaultFilename . $ options ['suffix ' ] . '. ' . $ extension ;
156
+ $ filename = $ date ->format ($ format ) . '_ ' . $ defaultFilename ;
157
+ return $ this ->applyAffixes ($ filename , $ options ) . '. ' . $ extension ;
173
158
}
174
159
175
160
/**
@@ -180,10 +165,11 @@ public function suffix(string $defaultFilename, string $extension, array $option
180
165
* @param array $options The options array.
181
166
* @return string The numbered string.
182
167
*/
183
- public function numbered (string $ defaultFilename , string $ extension , array $ options )
168
+ public function numbered (string $ defaultFilename , string $ extension , ? array $ options = null )
184
169
{
185
170
$ number = $ options ['number ' ] ?? 1 ;
186
- return $ defaultFilename . '_ ' . $ number . '. ' . $ extension ;
171
+ $ filename = $ defaultFilename . '_ ' . $ number ;
172
+ return $ this ->applyAffixes ($ filename , $ options ) . '. ' . $ extension ;
187
173
}
188
174
189
175
/**
@@ -193,9 +179,10 @@ public function numbered(string $defaultFilename, string $extension, array $opti
193
179
* @param string|null $extension The extension of the file.
194
180
* @return string The slug string.
195
181
*/
196
- public function slug (string $ defaultFilename , string $ extension )
182
+ public function slug (string $ defaultFilename , string $ extension, ? array $ options = null )
197
183
{
198
- return Str::slug ($ defaultFilename ) . '. ' . $ extension ;
184
+ $ filename = Str::slug ($ defaultFilename );
185
+ return $ this ->applyAffixes ($ filename , $ options ) . '. ' . $ extension ;
199
186
}
200
187
201
188
/**
@@ -206,13 +193,34 @@ public function slug(string $defaultFilename, string $extension)
206
193
* @param array $options The options array.
207
194
* @return string The hash string.
208
195
*/
209
- public function hash (string $ defaultFilename , string $ extension , array $ options )
196
+ public function hash (string $ defaultFilename , string $ extension , ? array $ options = null )
210
197
{
211
198
$ algorithm = $ options ['algorithm ' ] ?? 'sha256 ' ;
212
199
if (!in_array ($ algorithm , hash_algos ())) {
213
200
throw new \InvalidArgumentException ("Invalid hash algorithm: {$ algorithm }" );
214
201
}
215
202
$ hash = hash ($ algorithm , $ defaultFilename );
216
- return $ hash . '. ' . $ extension ;
203
+ $ filename = $ hash . '. ' . $ extension ;
204
+ return $ this ->applyAffixes ($ filename , $ options );
205
+ }
206
+
207
+ /**
208
+ * Apply prefix and suffix to filename
209
+ *
210
+ * @param string $filename
211
+ * @param array $options
212
+ * @return string
213
+ */
214
+ private function applyAffixes (string $ filename , ?array $ options = null ): string
215
+ {
216
+ $ options = $ options ?? $ this ->options ;
217
+
218
+ if (isset ($ options ['prefix ' ])) {
219
+ $ filename = $ options ['prefix ' ] . $ filename ;
220
+ }
221
+ if (isset ($ options ['suffix ' ])) {
222
+ $ filename = $ filename . $ options ['suffix ' ];
223
+ }
224
+ return $ filename ;
217
225
}
218
226
}
0 commit comments