Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Utilities Strings Laravel Package

myerscode/utilities-strings

A small PHP utility library providing string helper functions for common formatting and manipulation tasks. Useful for Laravel or plain PHP projects to reduce boilerplate for trimming, case conversion, searching, and other everyday string operations.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require myerscode/utilities-strings
    

    No additional configuration is required—just autoload the Myerscode\Utilities\Strings class.

  2. First Use Case: Basic String Manipulation

    use Myerscode\Utilities\Strings;
    
    $string = Strings::create('  hello world  ');
    $result = $string->trim()->toLower()->replace(' ', '_');
    echo $result; // Output: "hello_world"
    
  3. Where to Look First

    • Class Reference: Check the Strings class methods via IDE autocompletion (e.g., trim(), toLower(), replace(), slug(), pluralize()).
    • Fluent Interface: Chain methods for readability (e.g., create()->trim()->slug()).
    • Static Factory: Use Strings::create() for new instances or Strings::from() for existing strings.

Implementation Patterns

Core Workflows

  1. Fluent String Chaining

    $cleaned = Strings::create($userInput)
        ->trim()
        ->toLower()
        ->slug()
        ->replace(['-', '_'], ' ');
    
  2. Conditional Logic with Methods

    $string = Strings::create('test');
    if ($string->startsWith('test')) {
        $string->append(' case');
    }
    
  3. Integration with Laravel

    • Form Requests: Sanitize input strings:
      public function rules()
      {
          return [
              'name' => 'required|string',
              'slug' => 'required|string',
          ];
      }
      
      public function prepareForValidation()
      {
          $this->merge([
              'slug' => Strings::create($this->name)->slug()->value(),
          ]);
      }
      
    • Model Accessors:
      public function getFormattedNameAttribute()
      {
          return Strings::create($this->name)
              ->trim()
              ->titleCase()
              ->value();
      }
      
  4. Batch Processing

    $strings = ['  foo  ', 'BAR', 'baz'];
    $processed = array_map(
        fn($s) => Strings::create($s)->trim()->toLower()->value(),
        $strings
    );
    
  5. Custom Logic via Closures

    $custom = Strings::create('example')
        ->transform(fn($str) => strrev($str))
        ->value(); // "elpmaxe"
    

Gotchas and Tips

Pitfalls

  1. Immutable Operations

    • Methods like trim() or replace() do not modify the original string; they return a new Strings instance.
    • Fix: Chain methods or use ->value() to extract the result:
      $original = '  test  ';
      $trimmed = Strings::create($original)->trim(); // $original remains unchanged
      
  2. Case Sensitivity in Comparisons

    • Methods like startsWith() and endsWith() are case-sensitive by default.
    • Workaround: Chain with toLower()/toUpper() if needed:
      if (Strings::create($input)->toLower()->startsWith('test')) { ... }
      
  3. Performance with Large Strings

    • Avoid excessive chaining on very long strings (e.g., processing files). Cache intermediate results:
      $temp = Strings::create($longString)->trim();
      $result = $temp->replace(...)->value();
      
  4. Locale-Specific Methods

    • Methods like titleCase() may not handle all locales perfectly (e.g., German umlauts).
    • Tip: Combine with mb_* functions if needed:
      $string->transform(fn($s) => mb_convert_case($s, MB_CASE_TITLE, 'UTF-8'));
      

Debugging Tips

  1. Inspect Intermediate Values Use ->value() to debug:

    $step1 = Strings::create($input)->trim()->value();
    dd($step1); // Debug after each transformation
    
  2. Check for Null/Empty Inputs The package may throw exceptions or return unexpected results if input is null or empty.

    • Fix: Add guards:
      if (empty($input)) {
          return Strings::create('default');
      }
      
  3. Override Default Behavior Extend the class for custom logic:

    class CustomStrings extends Strings
    {
        public function customMethod()
        {
            return $this->transform(fn($s) => '[' . $s . ']');
        }
    }
    

Extension Points

  1. Add Custom Methods Use traits or extend the class:

    Strings::macro('customSlug', function() {
        return $this->slug()->replace(['-', '_'], ' ');
    });
    

    Now use:

    Strings::create('hello_world')->customSlug();
    
  2. Integrate with Laravel Helpers Create a helper function in app/Helpers/string.php:

    if (!function_exists('cleanString')) {
        function cleanString($str) {
            return Strings::create($str)->trim()->toLower()->value();
        }
    }
    
  3. Handle Edge Cases Override methods for specific needs (e.g., custom slug logic):

    Strings::macro('customSlug', function() {
        return $this->transform(fn($s) => Str::of($s)->slug('-'));
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor