diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 06f9a3a..7790b32 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,22 +6,33 @@ jobs: test: runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php: ["8.3", "8.4"] + laravel: ["12.*", "13.*"] + include: + - laravel: "12.*" + testbench: "10.*" + - laravel: "13.*" + testbench: "11.*" + + name: P${{ matrix.php }} - L${{ matrix.laravel }} + steps: - # Step 1: Check out the repository - name: Checkout code uses: actions/checkout@v4 - # Step 2: Set up PHP - name: Set up PHP uses: shivammathur/setup-php@v2 with: - php-version: "8.2" # Set your desired PHP version + php-version: ${{ matrix.php }} extensions: mbstring, dom, zip - # Step 3: Install Composer dependencies - name: Install dependencies - run: composer install --no-interaction --prefer-dist + run: | + composer require "illuminate/support:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update + composer update --prefer-dist --no-interaction - # Step 4: Run PHPUnit tests - name: Run tests run: php ./vendor/bin/phpunit diff --git a/README.md b/README.md index dcef448..fe65350 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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', @@ -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). diff --git a/composer.json b/composer.json index ed38a76..b9a7fe1 100644 --- a/composer.json +++ b/composer.json @@ -17,11 +17,11 @@ ], "require": { "php": "^8.0|^8.2|^8.3|^8.4", - "illuminate/support": "^8.0|^9.0|^10.0|^11.0|^12.0" + "illuminate/support": "^8.0|^9.0|^10.0|^11.0|^12.0|^13.0" }, "require-dev": { - "orchestra/testbench": "^10.0", - "phpunit/phpunit": "^11.0" + "orchestra/testbench": "^10.0|^11.0", + "phpunit/phpunit": "^11.0|^12.0" }, "autoload": { "psr-4": { diff --git a/config/config.php b/config/config.php index 082eab0..dab12dc 100644 --- a/config/config.php +++ b/config/config.php @@ -21,6 +21,15 @@ */ 'default_extension' => 'txt', + /* + |-------------------------------------------------------------------------- + | Onym Default Separator + |-------------------------------------------------------------------------- + | + | Configure the default separator to use. + */ + 'default_separator' => '_', + /* |-------------------------------------------------------------------------- | Onym Generation Settings diff --git a/src/Onym.php b/src/Onym.php index 5303a5c..59eb7d5 100644 --- a/src/Onym.php +++ b/src/Onym.php @@ -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', '_'); } /** @@ -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); } @@ -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); } @@ -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); } @@ -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); } diff --git a/tests/OnymTest.php b/tests/OnymTest.php index 383dacf..9021824 100644 --- a/tests/OnymTest.php +++ b/tests/OnymTest.php @@ -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); + } } \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index c44b6ca..029d1dc 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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', '_')); } } \ No newline at end of file