ethantechnology/php-helper

A collection of useful PHP utility functions

dev-main 2025-04-29 14:24 UTC

This package is not auto-updated.

Last update: 2025-07-23 14:00:19 UTC


README

A collection of useful PHP utility functions.

Installation

You can install the package via Composer:

composer require ethantechnology/php-helper

Usage

String Functions

use Eta\Str;
// or
use Eta\Helper;

// Convert to snake_case
$snakeCase = Str::toSnakeCase('HelloWorld'); // Returns: hello_world

// Convert to camelCase
$camelCase = Str::toCamelCase('hello-world'); // Returns: helloWorld

// Check if string starts with
$startsWith = Str::startsWith('Hello World', 'Hello'); // Returns: true

// Check if string ends with
$endsWith = Str::endsWith('Hello World', 'World'); // Returns: true

Random Generation Functions

use Eta\Str;
// or
use Eta\Helper;

// Generate random string with letters and numbers
$random = Str::random(10); // Returns: "aB3cD9fG2h"

// Alias for random()
$randomString = Str::randomString(10); // Returns: "aB3cD9fG2h"

// Generate random number string
$randomNumber = Str::randomNumber(10); // Returns: "1234567890"

Laravel Specific Functions

use Eta\Laravel;
// or
use Eta\Helper;

// Get controller class name
$controller = Laravel::ctl('User'); // Returns: App\Http\Controllers\UserController

// Check token expiration
$isExpired = Laravel::checkTokenExpiration($user); // Returns: true/false

// Make model class name
$modelClass = Laravel::makeModelClassName('User', 'Eloquent'); // Returns: \App\Models\User\UserEloquent

Utility Functions

use Eta\Str;
// or
use Eta\Helper;

// Check if value is valid JSON
$isJson = Str::isJson('{"key": "value"}'); // Returns: true
$isJson = Str::isJson('invalid json'); // Returns: false

Available Functions

String Functions

  • toSnakeCase(string $string): string - Convert string to snake_case
  • toCamelCase(string $string): string - Convert string to camelCase
  • startsWith(string $haystack, string $needle): bool - Check if string starts with
  • endsWith(string $haystack, string $needle): bool - Check if string ends with

Random Generation Functions

  • random(int $length = 16): string - Generate random string with letters and numbers
  • randomString(int $length = 16): string - Alias for random()
  • randomNumber(int $length = 16): string - Generate random number string

Laravel Functions

  • ctl(string $key): string - Get fully qualified controller class name
  • checkTokenExpiration($user): bool - Check if user's token has expired
  • makeModelClassName(string $name, string $type = ''): string - Make full model class name

Utility Functions

  • isJson(mixed $value): bool - Check if value is valid JSON

Using Helper Class

You can also use the Helper class to access all functions:

use Eta\Helper;

// String functions
$snakeCase = Helper::toSnakeCase('HelloWorld');

// Random functions
$random = Helper::random(10);

// Laravel functions
$controller = Helper::ctl('User');

// Utility functions
$isJson = Helper::isJson('{"key": "value"}');

License

The MIT License (MIT). Please see License File for more information.