ssolweb/string-morpher
ssolweb/string-morpher offers a fluent PHP/Laravel API for morphing strings into common formats like camelCase, snake_case, slug, and studly case, plus custom rules for reusable, consistent string transformations.
The v1.1.5 release is additive: three new transformers (slugify, wrap, unwrap) join the existing transformation pipeline. There are no breaking changes, so this is a safe upgrade.
Start by locating the transformer registry/service provider that the package publishes. In a typical setup you'll find:
config/transformers.php or similar) listing available transformers.Transformer contract with a transform() method.Minimal first use case — normalising a title-style attribute before persisting it:
$value = transform('Hello World')
->slugify() // "hello-world"
->wrap('"'); // "\"hello-world\""
Then walk it back with unwrap when reading:
transform('"hello-world"')->unwrap('"'); // "hello-world"
Look first at the changelog-linked PRs (#58, #56, #55) for exact method signatures and argument defaults (e.g. whether wrap defaults to a single character, whether slugify accepts a separator). Confirm behaviour against the published docs rather than assuming parity with Str::slug().
Model accessors / casts. Chain transformers in an accessor or custom cast so slug generation and display formatting stay declarative:
protected function slug(): Attribute
{
return Attribute::make(
set: fn ($value) => transform($value)->slugify(),
);
}
Form request sanitisation. Slugify user-supplied identifiers (category names, tags, permalinks) in prepareForValidation() so downstream validation and uniqueness checks see the canonical form:
protected function prepareForValidation(): void
{
$this->merge([
'slug' => transform($this->input('slug') ?? $this->input('name'))->slugify(),
]);
}
Import/seed normalisation. When ingesting CSVs or legacy data, pipe each row's string fields through slugify then wrap/unwrap to strip inconsistent quoting or bracket decoration before insert. This is the cleanest fit for the new unwrap transformer — legacy exports frequently pad values with prefixes/suffixes.
API resources. Apply unwrap when returning values that were stored with presentation wrappers, and wrap when an external consumer expects a formatted string. Keep wrapping at the resource layer rather than in the database, so storage stays canonical.
Composing chains. Prefer a single fluent chain over repeated local variables. Because transformers are additive and stateless-looking, order matters: normalise first (slugify), then decorate (wrap), and reserve unwrap for input cleanup. Build small named helper methods (e.g. normaliseSlug()) rather than duplicating chains across models.
Testing. Add a data-provider test around each chain you rely on, especially edge cases: empty strings, strings already containing the wrapper, unicode input, and multiple consecutive separators. This guards against future transformer changes altering chain output.
wrap. Calling wrap on an already-wrapped value will double-wrap. Guard with an unwrap first, or check Str::startsWith/Str::endsWith before applying.unwrap is prefix/suffix-only. Per the release notes it removes a prefix and/or suffix — it is not a general-purpose trim replacement for arbitrary characters. Use PHP's trim() when you need character-class trimming.slugify is not necessarily Str::slug. Verify separator handling, transliteration, and whether it lowercases. Don't assume identical output to Laravel's helper when your slugs back route model binding or unique indexes.json bump under /docs (dependabot #57) doesn't affect runtime; no action needed in application composer constraints.php artisan config:clear after upgrades — cached config can hide newly registered transformers added in a minor release.str_replace calls through your codebase.^1.1 and review the changelog for further transformer additions, which are the most likely source of behaviour drift in chains you already rely on.How can I help you explore Laravel packages today?