Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,23 @@ Onym::make('document', 'pdf', 'hash', [
Onym::hash(string $defaultFilename, string $extension, ?array $options = [])
```

## Customizing the Default Separator

When creating an Onym instance manually (outside the facade),
you can customize the default separator used between filename parts.

```php
use Blaspsoft\Onym\Onym;

// Create an instance with a custom default separator
$onym = new Onym([], '|');

// Use the timestamp strategy with the custom separator
$filename = $onym->timestamp('invoice', 'pdf');

// Result: "2024-03-15|invoice.pdf"
```

## Global Configuration

You can set default values for all strategies in your `config/onym.php` file:
Expand All @@ -270,7 +287,10 @@ return [

// Default extension when none is provided
'default_extension' => 'txt',


// Default separator when none is provided
'default_separator' => '_',

// Default strategy when none is specified
'strategy' => 'random',

Expand Down Expand Up @@ -314,6 +334,16 @@ return [

These defaults can be overridden on a per-call basis using the `options` parameter in the `make()` and in all strategy methods.

>⚠️ **Note:**
>If you have previously published the `config/onym.php` file, you need to manually add the new `default_separator` key:
>
>```php
>'default_separator' => '_',
>```
>
>Otherwise, the fallback separator `_` will still be used automatically by the package.


## License

Blasp is open-sourced software licensed under the [MIT license](LICENSE).
9 changes: 9 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
*/
'default_extension' => 'txt',

/*
|--------------------------------------------------------------------------
| Onym Default Separator
|--------------------------------------------------------------------------
|
| Configure the default separator to use.
*/
'default_separator' => '_',

/*
|--------------------------------------------------------------------------
| Onym Generation Settings
Expand Down
24 changes: 18 additions & 6 deletions src/Onym.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,20 @@ class Onym
*/
public string $defaultExtension;

public function __construct()
/**
* The default separator to use.
*
* @var string
*/
public string $defaultSeparator;

public function __construct(array $options = [], ?string $defaultSeparator = null)
{
$this->strategy = config('onym.strategy', 'random');
$this->options = config('onym.options', []);
$this->options = $options ?: config('onym.options', []);
$this->defaultFilename = config('onym.default_filename', 'file');
$this->defaultExtension = config('onym.default_extension', 'txt');
$this->defaultSeparator = $defaultSeparator ?? config('onym.default_separator', '_');
}

/**
Expand Down Expand Up @@ -135,7 +143,8 @@ public function timestamp(string $defaultFilename, string $extension, ?array $op

$format = $options['format'] ?? 'Y-m-d_H-i-s';
$date = new DateTime();
$filename = $date->format($format) . '_' . $defaultFilename;
$separator = $options['separator'] ?? $this->defaultSeparator;
$filename = $date->format($format) . $separator . $defaultFilename;
return $this->applyAffixes($filename, $extension, $options);
}

Expand All @@ -152,7 +161,8 @@ public function date(string $defaultFilename, string $extension, ?array $options
$options = $this->mergeOptions($options, 'date', $this->options);
$format = $options['format'] ?? 'Y-m-d';
$date = new DateTime();
$filename = $date->format($format) . '_' . $defaultFilename;
$separator = $options['separator'] ?? $this->defaultSeparator;
$filename = $date->format($format) . $separator . $defaultFilename;
return $this->applyAffixes($filename, $extension, $options);
}

Expand All @@ -168,7 +178,8 @@ public function numbered(string $defaultFilename, string $extension, ?array $opt
{
$options = $this->mergeOptions($options, 'numbered', $this->options);
$number = $options['number'] ?? 1;
$filename = $defaultFilename . '_' . $number;
$separator = $options['separator'] ?? $this->defaultSeparator;
$filename = $defaultFilename . $separator . $number;
return $this->applyAffixes($filename, $extension, $options);
}

Expand All @@ -182,7 +193,8 @@ public function numbered(string $defaultFilename, string $extension, ?array $opt
public function slug(string $defaultFilename, string $extension, ?array $options = [])
{
$options = $this->mergeOptions($options, 'slug', $this->options);
$filename = Str::slug($defaultFilename, $options['separator']);
$separator = $options['separator'] ?? $this->defaultSeparator;
$filename = Str::slug($defaultFilename, $separator);
return $this->applyAffixes($filename, $extension, $options);
}

Expand Down
79 changes: 79 additions & 0 deletions tests/OnymTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,83 @@ public function it_allows_options_override_in_hash_method_with_null_values()
$filename = $this->onym->hash('filename', 'txt', ['prefix' => null, 'suffix' => null, 'algorithm' => null]);
$this->assertEquals($hash . '.txt', $filename);
}

#[Test]
public function test_timestamp_uses_default_separator()
{
$onym = new Onym([], '|'); // Inject custom defaultSeparator
$result = $onym->timestamp('filename', 'txt');
$this->assertStringContainsString('|filename.txt', $result);
}

#[Test]
public function test_timestamp_overrides_separator_in_options()
{
$onym = new Onym([], '|');
$result = $onym->timestamp('filename', 'txt', ['separator' => '.']);
$this->assertStringContainsString('.filename.txt', $result);
$this->assertStringNotContainsString('|filename.txt', $result);
}

#[Test]
public function test_date_uses_default_separator()
{
$onym = new Onym([], '|');
$result = $onym->date('filename', 'txt');
$this->assertStringContainsString('|filename.txt', $result);
}

#[Test]
public function test_date_overrides_separator_in_options()
{
$onym = new Onym([], '|');
$result = $onym->date('filename', 'txt', ['separator' => '.']);
$this->assertStringContainsString('.filename.txt', $result);
$this->assertStringNotContainsString('|filename.txt', $result);
}

#[Test]
public function test_numbered_uses_default_separator()
{
$onym = new Onym([
'numbered' => ['prefix' => '', 'suffix' => '', 'number' => 5] // No separator defined!
], '|');

$result = $onym->numbered('filename', 'txt');

$this->assertStringContainsString('|5.txt', $result);
}

#[Test]
public function test_numbered_overrides_separator_in_options()
{
$onym = new Onym([], '|');
$result = $onym->numbered('filename', 'txt', ['separator' => '-', 'number' => 5]);
$this->assertStringContainsString('-5.txt', $result);
$this->assertStringNotContainsString('|5.txt', $result);
}

#[Test]
public function test_slug_uses_default_separator()
{
$onym = new Onym([
'slug' => ['prefix' => '', 'suffix' => ''] // No separator defined!
], '_');

$result = $onym->slug('My File Name', 'txt');

$this->assertStringContainsString('_', $result);
$this->assertStringNotContainsString('-', $result);
$this->assertStringEndsWith('.txt', $result);
}

#[Test]
public function test_slug_overrides_separator_in_options()
{
$onym = new Onym([], '_');
$result = $onym->slug('My File Name', 'txt', ['separator' => '.']);
$this->assertStringContainsString('.', $result);
$this->assertStringNotContainsString('_', $result);
$this->assertStringEndsWith('.txt', $result);
}
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ protected function getEnvironmentSetUp($app): void
config()->set('onym.options', config('onym.options', []));
config()->set('onym.default_filename', config('onym.default_filename', 'file'));
config()->set('onym.default_extension', config('onym.default_extension', 'txt'));
config()->set('onym.default_separator', config('onym.default_separator', '_'));
}
}