daikazu / misc-helpers
Misc PHP and Laravel helper functions and Classes
Fund package maintenance!
Daikazu
Requires
- php: ^8.2
- illuminate/contracts: ^11.0
- illuminate/support: ^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
This package provides a collection of helper functions and classes for PHP and Laravel.
Installation
You can install the package via composer:
composer require daikazu/misc-helpers
Usage
Helper Functions
format_business_hours
Formats business hours into a human-readable string.
$hours = [ 'monday' => ['open' => '09:00', 'close' => '17:00'], 'tuesday' => ['open' => '09:00', 'close' => '17:00'], 'wednesday' => ['open' => '09:00', 'close' => '17:00'] ]; $formatted = format_business_hours($hours, 'America/New_York'); echo $formatted; // Outputs: Monday - Wednesday 9am-5pm EDT
calculate_read_time
Calculates the estimated read time for a given HTML content.
$content = '<p>This is a very short text.</p>'; $readTime = calculate_read_time($content); echo $readTime; // Outputs: 1
is_blade_section_empty
Checks if a Blade section is empty.
@section('test')
<p>Section is empty</p>
@endsection
$isEmpty = is_blade_section_empty('test'); var_dump($isEmpty); // Outputs: bool(false)
generate_initials
Generates initials from a given name.
$name = 'John Doe'; $initials = generate_initials($name); echo $initials; // Outputs: JD
clean_string
Cleans a string by removing extra whitespace and optionally converting it to lowercase.
$string = ' Hello World '; $cleaned = clean_string($string); echo $cleaned; // Outputs: Hello World
Artisan Commands
PruneLogFilesCommand
The PruneLogFilesCommand
is a custom Artisan command used to prune old log files from the storage directory.
Usage
To run the command, use the following Artisan command:
php artisan log:prune
Options
--days[=DAYS]
: The number of days to retain log files. Files older than this will be deleted. Default is 30 days.
Example
To prune log files older than 15 days, run:
php artisan log:prune --days=15
Utility Classes
PhpArrayFormatter
The PhpArrayFormatter class provides functionality to format PHP arrays into a readable string representation.
####Usage To use the PhpArrayFormatter, you can create an instance of the class and call the format method with the array you want to format.
use App\Utilities\PhpArrayFormatter; $array = [ 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'roles' => ['admin', 'user'] ]; $formatter = new PhpArrayFormatter(); $formattedArray = $formatter->format($array); echo $formattedArray; // Outputs: // [ // 'name' => 'John Doe', // 'email' => 'john.doe@example.com', // 'roles' => [ // 'admin', // 'user' // ] // ]
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.