feat: add Str::from() and Str::fromAll() helpers
#291
+208
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I’m not sure if
Stris the right place for this. Another option would be to create a separate helper.Summary
Adds two new methods to the
Strhelper class that normalizeBackedEnum|string|intvalues to strings. This provides a foundation for introducing enum support across framework APIs that only accept string identifiers.Motivation
Many framework APIs currently only accept strings: cache tags, session keys, Sanctum token abilities, config keys, etc. Using string literals for these is error-prone:
// Typos slip through unnoticed
BackedEnums solve this beautifully—IDE autocomplete, refactoring support, and compile-time safety:
Unfortunately you can't cast a BackedEnum to a string. This makes it awkward to add enum support to existing APIs. You need explicit
instanceofchecks and->valueaccess everywhere.Solution
Str::from()provides a clean, consistent way to normalize enum-or-string values:Enabling enum support across the framework
With this helper, adding enum support to framework APIs becomes simple. For example:
Cache tags:
Session keys:
Sanctum token abilities:
Config keys:
API
Design Decisions
Str::fromBase64()pattern. TheStr::prefix makes the output type clear.string|int|BackedEnum. Other types (Stringable, pure enums, null) are intentionally excluded as edge cases better handled explicitly.Tests
17 tests covering: