A collection of useful PHP helper functions and utilities with full type safety and comprehensive documentation.
- 🔒 Fully Typed - Complete PHP 8.0+ type declarations for enhanced IDE support
- 📝 Well Documented - Comprehensive PHPDoc comments with examples
- ✅ Thoroughly Tested - High test coverage with edge case testing
- 🔍 Static Analysis - PHPStan Level 8 compliant
- 🎯 Zero Dependencies - Only requires Carbon for date/time utilities
- 🌐 UTF-8 Safe - Multi-byte string operations throughout
- PHP 8.0 or higher
- ext-mbstring
You can install the package via composer:
composer require farzai/supportThe Arr class provides utilities for working with arrays using dot notation.
Get an item from an array using dot notation:
useFarzai\Support\Arr;
$array = [
'user' => [
'name' => 'John Doe',
'email' => 'john@example.com',
'address' => [
'city' => 'New York'
]
]
];
// Get nested value
Arr::get($array, 'user.name'); // Returns: 'John Doe'
Arr::get($array, 'user.address.city'); // Returns: 'New York'// With default value
Arr::get($array, 'user.phone', 'N/A'); // Returns: 'N/A'// Get entire array
Arr::get($array, null); // Returns: entire $arrayCheck if a key exists in an array using dot notation:
useFarzai\Support\Arr;
$array = ['user' => ['name' => 'John']];
Arr::exists($array, 'user.name'); // Returns: true
Arr::exists($array, 'user.email'); // Returns: falseCheck if a value is array accessible:
useFarzai\Support\Arr;
Arr::accessible(['foo' => 'bar']); // Returns: true
Arr::accessible(newArrayObject()); // Returns: true
Arr::accessible('string'); // Returns: falseThe Str class provides a rich set of string manipulation methods.
useFarzai\Support\Str;
// camelCase
Str::camel('foo_bar'); // Returns: 'fooBar'
Str::camel('foo-bar'); // Returns: 'fooBar'// StudlyCase (PascalCase)
Str::studly('foo_bar'); // Returns: 'FooBar'// snake_case
Str::snake('fooBar'); // Returns: 'foo_bar'
Str::snake('FooBar', '-'); // Returns: 'foo-bar'// lowercase
Str::lower('FOO BAR'); // Returns: 'foo bar'useFarzai\Support\Str;
Str::isSnakeCase('foo_bar'); // Returns: true
Str::isCamelCase('fooBar'); // Returns: true
Str::isStudlyCase('FooBar'); // Returns: trueuseFarzai\Support\Str;
// Replace
Str::replace('foo', 'bar', 'foo baz'); // Returns: 'bar baz'// Check if starts with
Str::startsWith('foobar', 'foo'); // Returns: true
Str::startsWith('foobar', ['bar', 'foo']); // Returns: true// Check if ends with
Str::endsWith('foobar', 'bar'); // Returns: true// Check if contains
Str::contains('foobar', 'oob'); // Returns: true
Str::contains('foobar', ['baz', 'bar']); // Returns: true// Length (UTF-8 safe)
Str::length('foo'); // Returns: 3
Str::length('ñoño'); // Returns: 4// Substring (UTF-8 safe)
Str::substr('foobar', 0, 3); // Returns: 'foo'
Str::substr('foobar', 3); // Returns: 'bar'All random methods use cryptographically secure randomness:
useFarzai\Support\Str;
// Random alphanumeric (base64-like)
Str::random(16); // Returns: 'a3K7mN9pQ1xY2zB5'// Random ASCII
Str::randomAscii(16);
// Random numeric only
Str::randomNumeric(6); // Returns: '472891'// Random alphanumeric (A-Z, a-z, 0-9)
Str::randomAlphanumeric(12); // Returns: 'aB3xY9mK2nP7'// Random with custom character set
Str::randomString(8, 'ABCD123'); // Returns: 'A2B1C3D2'// Random with special characters (for passwords)
Str::randomStringWithSpecialCharacter(16); // Returns: 'aB3!xY@9#mK2$pQ5'The Carbon class extends the popular Carbon library with additional convenience methods.
useFarzai\Support\Carbon;
usefunctionFarzai\Support\now;
// Get current date/time$now = now();
// or$now = Carbon::now();
// With timezone$now = now('America/New_York');
$now = Carbon::now('UTC');
// From timestamp$date = Carbon::fromTimestamp(1609459200);useFarzai\Support\Carbon;
$today = Carbon::now();
$yesterday = Carbon::yesterday();
$tomorrow = Carbon::tomorrow();
// Check if today$today->isToday(); // Returns: true$yesterday->isToday(); // Returns: false// Check if past$yesterday->isPast(); // Returns: true$tomorrow->isPast(); // Returns: false// Check if future$tomorrow->isFuture(); // Returns: true$yesterday->isFuture(); // Returns: false// Check if between dates$today->isBetweenDates($yesterday, $tomorrow); // Returns: trueuseFarzai\Support\Carbon;
$date = Carbon::now();
// Format as date string$date->toDateString(); // Returns: '2024-03-18'// Format as time string$date->toTimeString(); // Returns: '14:30:45'// Format as datetime string$date->toDateTimeString(); // Returns: '2024-03-18 14:30:45'useFarzai\Support\Carbon;
$date = Carbon::now();
// Start of day$start = $date->startOfDay(); // Returns: today at 00:00:00// End of day$end = $date->endOfDay(); // Returns: today at 23:59:59useFarzai\Support\Carbon;
$today = Carbon::now();
$yesterday = Carbon::yesterday();
// Absolute difference in days$today->diffInDaysAbsolute($yesterday); // Returns: 1Global helper functions in the Farzai\Support namespace.
Execute a callback on a value and return the value:
usefunctionFarzai\Support\tap;
// With callback$user = tap($user, function ($u) {
$u->update(['last_login' => now()]);
});
// Returns $user after updating// Without callback (higher-order proxy)$user = tap($user)
->update(['last_login' => now()])
->save();
// Chains methods but returns $userGet the current date/time:
usefunctionFarzai\Support\now;
$current = now(); // Returns: Carbon instance$ny = now('America/New_York'); // Returns: Carbon instance in NY timezoneGet the class name without namespace:
usefunctionFarzai\Support\class_basename;
class_basename('App\Models\User'); // Returns: 'User'class_basename(new \App\Models\User); // Returns: 'User'composer testcomposer test-coveragecomposer formatcomposer analyzecomposer checkPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.