Skip to content

Commit 59b4b87

Browse files
committedMar 4, 2025
removed suffix and refix as strategies and made them options for all other stragegies
1 parent 2b84a37 commit 59b4b87

File tree

4 files changed

+79
-117
lines changed

4 files changed

+79
-117
lines changed
 

‎README.md

+12-38
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A flexible Laravel package for generating filenames using various strategies and
1515
## 🚀 Features
1616

1717
-**Flexible Filename Generation** – Generate filenames dynamically using various strategies.
18-
- 🎲 **Multiple Strategies** – Supports `random`, `uuid`, `timestamp`, `date`, `prefix`, `suffix`, `numbered`, `slug`, and `hash`.
18+
- 🎲 **Multiple Strategies** – Supports `random`, `uuid`, `timestamp`, `date`, `numbered`, `slug`, and `hash`.
1919
- 🔧 **Customizable Output** – Specify filename, extension, and additional formatting options.
2020
- 🎯 **Laravel-Friendly** – Designed to work seamlessly with Laravel's filesystem and configuration.
2121
- 📂 **Human-Readable & Unique Names** – Ensures filenames are structured, collision-free, and easy to understand.
@@ -76,7 +76,7 @@ Generates a random string of characters.
7676
Onym::make(strategy: 'random', options: ['length' => 8]); // e.g., "a1b2c3d4.txt"
7777

7878
// Use the random strategy method directly
79-
Onym::random(8, 'txt'); // e.g., "a1b2c3d4.txt"
79+
Onym::random('txt', options: ['length' => 8]); // e.g., "a1b2c3d4.txt"
8080
```
8181

8282
#### UUID
@@ -121,34 +121,6 @@ Onym::date('document', 'pdf', ['format' => 'Y-m-d']);
121121
// Result: "2024-03-15_document.pdf"
122122
```
123123

124-
#### Prefix
125-
126-
Adds a prefix to the filename.
127-
128-
```php
129-
// Use the make method and override config values
130-
Onym::make('document', 'pdf', 'prefix', ['prefix' => 'draft_']);
131-
// Result: "draft_document.pdf"
132-
133-
// Use the prefix strategy method directly
134-
Onym::prefix('document', 'pdf', ['prefix' => 'draft_']);
135-
// Result: "draft_document.pdf"
136-
```
137-
138-
#### Suffix
139-
140-
Adds a suffix to the filename.
141-
142-
```php
143-
// Use the make method and override config values
144-
Onym::make('document', 'pdf', 'suffix', ['suffix' => '_v1']);
145-
// Result: "document_v1.pdf"
146-
147-
// Use the suffix strategy method directly
148-
Onym::suffix('document', 'pdf', ['suffix' => '_v1']);
149-
// Result: "document_v1.pdf"
150-
```
151-
152124
#### Numbered
153125

154126
Adds a number to the filename.
@@ -377,32 +349,34 @@ return [
377349

378350
'random' => [
379351
'length' => 16,
352+
'prefix' => '',
353+
'suffix' => '',
380354
],
381355

382356
'timestamp' => [
383357
'format' => 'Y-m-d_H-i-s',
358+
'prefix' => '',
359+
'suffix' => '',
384360
],
385361

386362
'date' => [
387363
'format' => 'Y-m-d',
388-
],
389-
390-
'prefix' => [
391-
'prefix' => 'onym_',
392-
],
393-
394-
'suffix' => [
395-
'suffix' => '_onym',
364+
'prefix' => '',
365+
'suffix' => '',
396366
],
397367

398368
'numbered' => [
399369
'number' => 1,
400370
'separator' => '_',
371+
'prefix' => '',
372+
'suffix' => '',
401373
],
402374

403375
'hash' => [
404376
'algorithm' => 'md5',
405377
'length' => 16,
378+
'prefix' => '',
379+
'suffix' => '',
406380
],
407381
],
408382
];

‎config/config.php

+9-29
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
*/
6767
'random' => [
6868
'length' => 16,
69+
'prefix' => '',
70+
'suffix' => '',
6971
],
7072

7173
/*
@@ -81,6 +83,8 @@
8183
*/
8284
'timestamp' => [
8385
'format' => 'Y-m-d_H-i-s',
86+
'prefix' => 'onym_',
87+
'suffix' => '_o',
8488
],
8589

8690
/*
@@ -96,36 +100,8 @@
96100
*/
97101
'date' => [
98102
'format' => 'Y-m-d',
99-
],
100-
101-
/*
102-
|--------------------------------------------------------------------------
103-
| Prefix Strategy Options
104-
|--------------------------------------------------------------------------
105-
|
106-
| Configure the options for the prefix strategy.
107-
|
108-
| Available options:
109-
| - prefix: The prefix of the filename.
110-
|
111-
*/
112-
'prefix' => [
113103
'prefix' => 'onym_',
114-
],
115-
116-
/*
117-
|--------------------------------------------------------------------------
118-
| Suffix Strategy Options
119-
|--------------------------------------------------------------------------
120-
|
121-
| Configure the options for the suffix strategy.
122-
|
123-
| Available options:
124-
| - suffix: The suffix of the filename.
125-
|
126-
*/
127-
'suffix' => [
128-
'suffix' => '_onym',
104+
'suffix' => '_o',
129105
],
130106

131107
/*
@@ -143,6 +119,8 @@
143119
'numbered' => [
144120
'number' => 1,
145121
'separator' => '_',
122+
'prefix' => 'onym_',
123+
'suffix' => '_o',
146124
],
147125

148126
/*
@@ -160,6 +138,8 @@
160138
'hash' => [
161139
'algorithm' => 'md5',
162140
'length' => 16,
141+
'prefix' => 'onym_',
142+
'suffix' => '_o',
163143
],
164144
],
165145
];

‎src/Onym.php

+57-49
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ public function make(
6060
)
6161
{
6262
$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;
6464
$defaultFilename = $defaultFilename ?? $this->defaultFilename;
6565
$extension = $extension ?? $this->defaultExtension;
6666

6767
return match ($useStrategy) {
68-
'random' => $this->random($useOptions['length'] ?? 16, $extension),
68+
'random' => $this->random($extension, $useOptions),
6969

70-
'uuid' => $this->uuid($extension),
70+
'uuid' => $this->uuid($extension, $useOptions),
7171

7272
'timestamp' => $this->timestamp($defaultFilename, $extension, $useOptions),
7373

@@ -87,16 +87,30 @@ public function make(
8787
};
8888
}
8989

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+
90102
/**
91103
* Generate a random string of characters.
92104
*
93105
* @param int|null $length The length of the random string.
94106
* @param string|null $extension The extension of the file.
95107
* @return string The random string.
96108
*/
97-
public function random(int $length, string $extension)
109+
public function random(string $extension, ?array $options = null)
98110
{
99-
return Str::random($length) . '.' . $extension;
111+
$length = $options['length'] ?? 16;
112+
$filename = Str::random($length);
113+
return $this->applyAffixes($filename, $options) . '.' . $extension;
100114
}
101115

102116
/**
@@ -105,9 +119,10 @@ public function random(int $length, string $extension)
105119
* @param string|null $extension The extension of the file.
106120
* @return string The UUID string.
107121
*/
108-
public function uuid(string $extension)
122+
public function uuid(string $extension, ?array $options = null)
109123
{
110-
return (string) Str::uuid() . '.' . $extension;
124+
$filename = (string) Str::uuid();
125+
return $this->applyAffixes($filename, $options) . '.' . $extension;
111126
}
112127

113128
/**
@@ -118,11 +133,12 @@ public function uuid(string $extension)
118133
* @param array $options The options array.
119134
* @return string The timestamp string.
120135
*/
121-
public function timestamp(string $defaultFilename, string $extension, array $options)
136+
public function timestamp(string $defaultFilename, string $extension, ?array $options = null)
122137
{
123138
$format = $options['format'] ?? 'Y-m-d_H-i-s';
124139
$date = new DateTime();
125-
return $date->format($format) . '_' . $defaultFilename . '.' . $extension;
140+
$filename = $date->format($format) . '_' . $defaultFilename;
141+
return $this->applyAffixes($filename, $options) . '.' . $extension;
126142
}
127143

128144
/**
@@ -133,43 +149,12 @@ public function timestamp(string $defaultFilename, string $extension, array $opt
133149
* @param array $options The options array.
134150
* @return string The date string.
135151
*/
136-
public function date(string $defaultFilename, string $extension, array $options)
152+
public function date(string $defaultFilename, string $extension, ?array $options = null)
137153
{
138154
$format = $options['format'] ?? 'Y-m-d';
139155
$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;
173158
}
174159

175160
/**
@@ -180,10 +165,11 @@ public function suffix(string $defaultFilename, string $extension, array $option
180165
* @param array $options The options array.
181166
* @return string The numbered string.
182167
*/
183-
public function numbered(string $defaultFilename, string $extension, array $options)
168+
public function numbered(string $defaultFilename, string $extension, ?array $options = null)
184169
{
185170
$number = $options['number'] ?? 1;
186-
return $defaultFilename . '_' . $number . '.' . $extension;
171+
$filename = $defaultFilename . '_' . $number;
172+
return $this->applyAffixes($filename, $options) . '.' . $extension;
187173
}
188174

189175
/**
@@ -193,9 +179,10 @@ public function numbered(string $defaultFilename, string $extension, array $opti
193179
* @param string|null $extension The extension of the file.
194180
* @return string The slug string.
195181
*/
196-
public function slug(string $defaultFilename, string $extension)
182+
public function slug(string $defaultFilename, string $extension, ?array $options = null)
197183
{
198-
return Str::slug($defaultFilename) . '.' . $extension;
184+
$filename = Str::slug($defaultFilename);
185+
return $this->applyAffixes($filename, $options) . '.' . $extension;
199186
}
200187

201188
/**
@@ -206,13 +193,34 @@ public function slug(string $defaultFilename, string $extension)
206193
* @param array $options The options array.
207194
* @return string The hash string.
208195
*/
209-
public function hash(string $defaultFilename, string $extension, array $options)
196+
public function hash(string $defaultFilename, string $extension, ?array $options = null)
210197
{
211198
$algorithm = $options['algorithm'] ?? 'sha256';
212199
if (!in_array($algorithm, hash_algos())) {
213200
throw new \InvalidArgumentException("Invalid hash algorithm: {$algorithm}");
214201
}
215202
$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;
217225
}
218226
}

‎src/OnymServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function boot()
1414
if ($this->app->runningInConsole()) {
1515
$this->publishes([
1616
__DIR__.'/../config/config.php' => config_path('onym.php'),
17-
], 'config');
17+
], 'onym-config');
1818
}
1919
}
2020

0 commit comments

Comments
 (0)
Failed to load comments.