XHelper

The XHelper class in the LaraUtilX package provides a collection of static helper methods for common operations including array manipulation, string processing, date formatting, and UUID generation.

Methods

Array Helpers

  1. arrayTrim(array $array): array

    • Trims whitespace from all string values in an array.
  2. arrayFlatten(array $array): array

    • Flattens a multi-dimensional array into a single-dimensional array.

String Helpers

  1. strBetween(string $string, string $start, string $end): ?string

    • Extracts a substring between two specified strings.
  2. strSlugify(string $string): string

    • Converts a string to a URL-friendly slug format.

Date Helpers

  1. carbonParse($date, $format = 'Y-m-d H:i:s'): ?string

    • Parses a date and formats it using Carbon.
  2. carbonHumanDiff($date): string

    • Returns a human-readable difference between a date and now.

Miscellaneous Helpers

  1. uuid(): string
    • Generates a UUID string.

Usage

To use the XHelper class, import it at the top of your file:

use LaraUtilX\Helpers\XHelper;

All methods are static, so you can call them directly on the class without instantiation.

Array Helpers

arrayTrim

Trims whitespace from all string values in an array.

Example:

use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class TestController extends Controller
{
    public function testArrayTrim(Request $request): JsonResponse
    {
        $input = $request->input('array', ['  hello  ', '  world  ', '  test  ']);

        $result = XHelper::arrayTrim($input);

        return response()->json([
            'function' => 'arrayTrim',
            'input' => $input,
            'output' => $result,
            'description' => 'Trims whitespace from all string values in an array'
        ]);
    }
}

Result:

{
    "function": "arrayTrim",
    "input": ["  hello  ", "  world  ", "  test  "],
    "output": ["hello", "world", "test"],
    "description": "Trims whitespace from all string values in an array"
}

arrayFlatten

Flattens a multi-dimensional array into a single-dimensional array.

Example:

use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class TestController extends Controller
{
    public function testArrayFlatten(Request $request): JsonResponse
    {
        $input = [
            'users' => [
                ['id' => 1, 'name' => 'John'],
                ['id' => 2, 'name' => 'Jane']
            ],
            'posts' => [
                ['id' => 1, 'title' => 'Post 1'],
                ['id' => 2, 'title' => 'Post 2']
            ]
        ];

        $result = XHelper::arrayFlatten($input);

        return response()->json([
            'function' => 'arrayFlatten',
            'input' => $input,
            'output' => $result,
            'description' => 'Flattens a multi-dimensional array into a single-dimensional array'
        ]);
    }
}

Result:

{
    "function": "arrayFlatten",
    "input": {
        "users": [{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}],
        "posts": [{"id": 1, "title": "Post 1"}, {"id": 2, "title": "Post 2"}]
    },
    "output": [{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}, {"id": 1, "title": "Post 1"}, {"id": 2, "title": "Post 2"}],
    "description": "Flattens a multi-dimensional array into a single-dimensional array"
}

String Helpers

strBetween

Extracts a substring between two specified strings.

Example:

use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class TestController extends Controller
{
    public function testStrBetween(Request $request): JsonResponse
    {
        $string = 'Hello [world] from Laravel';
        $start = '[';
        $end = ']';

        $result = XHelper::strBetween($string, $start, $end);

        return response()->json([
            'function' => 'strBetween',
            'input' => [
                'string' => $string,
                'start' => $start,
                'end' => $end
            ],
            'output' => $result,
            'description' => 'Extracts a substring between two specified strings'
        ]);
    }
}

Result:

{
    "function": "strBetween",
    "input": {
        "string": "Hello [world] from Laravel",
        "start": "[",
        "end": "]"
    },
    "output": "world",
    "description": "Extracts a substring between two specified strings"
}

strSlugify

Converts a string to a URL-friendly slug format.

Example:

use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class TestController extends Controller
{
    public function testStrSlugify(Request $request): JsonResponse
    {
        $input = 'Hello World! This is a Test String 123';

        $result = XHelper::strSlugify($input);

        return response()->json([
            'function' => 'strSlugify',
            'input' => $input,
            'output' => $result,
            'description' => 'Converts a string to a URL-friendly slug format'
        ]);
    }
}

Result:

{
    "function": "strSlugify",
    "input": "Hello World! This is a Test String 123",
    "output": "hello-world-this-is-a-test-string-123",
    "description": "Converts a string to a URL-friendly slug format"
}

Date Helpers

carbonParse

Parses a date and formats it using Carbon.

Example:

use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class TestController extends Controller
{
    public function testCarbonParse(Request $request): JsonResponse
    {
        $date = '2024-01-15 14:30:00';
        $format = 'Y-m-d';

        $result = XHelper::carbonParse($date, $format);

        return response()->json([
            'function' => 'carbonParse',
            'input' => [
                'date' => $date,
                'format' => $format
            ],
            'output' => $result,
            'description' => 'Parses a date and formats it using Carbon'
        ]);
    }
}

Result:

{
    "function": "carbonParse",
    "input": {
        "date": "2024-01-15 14:30:00",
        "format": "Y-m-d"
    },
    "output": "2024-01-15",
    "description": "Parses a date and formats it using Carbon"
}

carbonHumanDiff

Returns a human-readable difference between a date and now.

Example:

use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class TestController extends Controller
{
    public function testCarbonHumanDiff(Request $request): JsonResponse
    {
        $date = now()->subDays(5);

        $result = XHelper::carbonHumanDiff($date);

        return response()->json([
            'function' => 'carbonHumanDiff',
            'input' => $date->toDateTimeString(),
            'output' => $result,
            'description' => 'Returns a human-readable difference between a date and now'
        ]);
    }
}

Result:

{
    "function": "carbonHumanDiff",
    "input": "2024-01-10 14:30:00",
    "output": "5 days ago",
    "description": "Returns a human-readable difference between a date and now"
}

Miscellaneous Helpers

uuid

Generates a UUID string.

Example:

use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class TestController extends Controller
{
    public function testUuid(Request $request): JsonResponse
    {
        $result = XHelper::uuid();

        return response()->json([
            'function' => 'uuid',
            'output' => $result,
            'description' => 'Generates a UUID string'
        ]);
    }
}

Result:

{
    "function": "uuid",
    "output": "550e8400-e29b-41d4-a716-446655440000",
    "description": "Generates a UUID string"
}

Complete Example

Here's a complete example showing multiple XHelper methods in use:

use LaraUtilX\Helpers\XHelper;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class ExampleController extends Controller
{
    public function demonstrateHelpers(Request $request): JsonResponse
    {
        // Array operations
        $trimmed = XHelper::arrayTrim(['  hello  ', '  world  ']);
        $flattened = XHelper::arrayFlatten([['a' => 1], ['b' => 2]]);

        // String operations
        $extracted = XHelper::strBetween('Hello [world]', '[', ']');
        $slug = XHelper::strSlugify('Hello World!');

        // Date operations
        $formatted = XHelper::carbonParse('2024-01-15', 'F j, Y');
        $humanDiff = XHelper::carbonHumanDiff(now()->subHours(2));

        // UUID generation
        $uuid = XHelper::uuid();

        return response()->json([
            'array_trim' => $trimmed,
            'array_flatten' => $flattened,
            'str_between' => $extracted,
            'str_slugify' => $slug,
            'carbon_parse' => $formatted,
            'carbon_human_diff' => $humanDiff,
            'uuid' => $uuid
        ]);
    }
}



The XHelper class provides convenient static methods for common operations, making it easy to perform array manipulation, string processing, date formatting, and UUID generation throughout your Laravel application.