daikazu/misc-helpers

Misc PHP and Laravel helper functions and Classes

v1.0.1 2024-10-31 21:15 UTC

This package is auto-updated.

Last update: 2024-10-31 21:19:10 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.