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
23 changes: 17 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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"
Comment on lines +274 to +276

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Correct the example output to match the default timestamp format.

The example calls timestamp() without options, so it uses the default format 'Y-m-d_H-i-s' which includes time. The actual result would be "2024-03-15_14-30-00|invoice.pdf", not "2024-03-15|invoice.pdf".

To fix, either:

  1. Pass ['format' => 'Y-m-d'] to produce date-only output, or
  2. Update the expected result to show the full timestamp with time
📝 Proposed fix (option 1: use date-only format)
 // Use the timestamp strategy with the custom separator
-$filename = $onym->timestamp('invoice', 'pdf');
+$filename = $onym->timestamp('invoice', 'pdf', ['format' => 'Y-m-d']);
 
 // Result: "2024-03-15|invoice.pdf"

Or option 2: update the expected result comment:

 // Use the timestamp strategy with the custom separator
 $filename = $onym->timestamp('invoice', 'pdf');
 
-// Result: "2024-03-15|invoice.pdf"
+// Result: "2024-03-15_14-30-00|invoice.pdf"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$filename = $onym->timestamp('invoice', 'pdf');
// Result: "2024-03-15|invoice.pdf"
$filename = $onym->timestamp('invoice', 'pdf', ['format' => 'Y-m-d']);
// Result: "2024-03-15|invoice.pdf"
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@README.md` around lines 274 - 276, The README example calling $filename =
$onym->timestamp('invoice', 'pdf'); uses the default timestamp format
'Y-m-d_H-i-s' but shows a date-only result; update the example so it matches
reality by either calling $onym->timestamp('invoice', 'pdf', ['format' =>
'Y-m-d']) to produce "2024-03-15|invoice.pdf" or change the expected result to
include the full default timestamp (e.g., "2024-03-15_14-30-00|invoice.pdf");
reference the timestamp() call and the default format 'Y-m-d_H-i-s' when making
the change.

```

## 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).
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
],
"require": {
"php": "^8.0|^8.2|^8.3|^8.4",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Search for PHP 8.1 related comments or documentation

# Check README and docs for PHP version mentions
fd -e md -e txt | xargs rg -i 'php.*8\.1' -C2

# Check for any PHP version related issues or comments in code
rg -i 'php.*8\.1|8\.1.*incompatible|8\.1.*issue' -C2

Repository: Blaspsoft/onym

Length of output: 40


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# 1) Show composer.json php constraint with line numbers around it
echo "== composer.json =="
if [ -f composer.json ]; then
  nl -ba composer.json | sed -n '1,120p'
else
  echo "composer.json not found at repo root"
fi

# 2) Search for explicit PHP 8.1 mentions/incompat notes across the repo (excluding vendor)
echo "== rg for PHP 8.1 related mentions =="
rg -n --hidden --glob '!vendor/**' --glob '!node_modules/**' --glob '!storage/**' \
  '(php[^0-9]{0,10}8\.1|8\.1[^0-9]{0,10}(incompatible|not supported|not supported|unsupported)|8\.1.*(incompatible|issue|break|fails|problem|support))' \
  -S || true

# 3) Broad search for "8.1" occurrences to see any context
echo "== rg for literal 8.1 (broad) =="
rg -n --hidden --glob '!vendor/**' --glob '!node_modules/**' \
  '8\.1' -S || true

Repository: Blaspsoft/onym

Length of output: 139


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== ls =="
ls -la | head

echo "== composer.json =="
if [ -f composer.json ]; then
  # Print first 120 lines with line numbers via awk
  awk '{printf "%5d\t%s\n", NR, $0}' composer.json | sed -n '1,120p'
else
  echo "composer.json not found at repo root"
fi

echo "== rg for PHP 8.1 related mentions =="
rg -n --hidden --glob '!vendor/**' --glob '!node_modules/**' \
  '(php[^0-9]{0,10}8\.1|8\.1[^0-9]{0,10}(incompatible|not supported|unsupported|not supported|problem|fails|issue)|8\.1.*(incompatible|issue|break|fails|problem|support))' \
  -S || true

echo "== rg for literal 8.1 occurrences (broad) =="
rg -n --hidden --glob '!vendor/**' --glob '!node_modules/**' \
  '8\.1' -S || true

Repository: Blaspsoft/onym

Length of output: 2234


Add ^8.1 back to the PHP constraint (or document why it’s excluded).

composer.json (line 19) sets "php": "^8.0|^8.2|^8.3|^8.4", which excludes PHP 8.1 and prevents installs on that runtime. There are no repo notes/comments indicating PHP 8.1 is incompatible, so please confirm whether to include ^8.1 or document the deliberate exclusion.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@composer.json` at line 19, The composer.json PHP platform constraint
currently excludes PHP 8.1; update the "php" constraint entry (the "php" key in
composer.json) to include ^8.1 (e.g., "php": "^8.0|^8.1|^8.2|^8.3|^8.4") if 8.1
is supported, or alternatively add a short note in project docs (README or
composer.json comments) explaining the intentional exclusion of 8.1 and the
reason; modify the "php" value under the composer.json file or add the
documentation accordingly so the constraint matches the project compatibility
decision.

"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": {
Expand Down
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', '_'));
}
}