florianv/business
Laravel package to validate, normalize, and format business identifiers and tax numbers (VAT, SIRET/SIREN, etc.) across multiple countries. Provides rules/helpers for forms and models, making compliant input handling and display easy.
Installation
composer require florianv/business
Add to config/app.php under providers:
FlorianV\Business\BusinessServiceProvider::class,
Basic Configuration Publish the config file:
php artisan vendor:publish --provider="FlorianV\Business\BusinessServiceProvider"
Edit config/business.php to define your business hours (e.g., 9:00-17:00 Mon-Fri).
First Use Case Calculate the next business day from a given date:
use FlorianV\Business\Facades\Business;
$nextBusinessDay = Business::nextBusinessDay(now());
Business Hour Calculations
$newDate = Business::addHours(now(), 5); // Adds 5 business hours
$oldDate = Business::subtractHours(now(), 3);
Date Manipulation
$next = Business::nextBusinessDay(now());
$prev = Business::previousBusinessDay(now());
$days = Business::businessDaysBetween(now(), now()->addDays(10));
Holiday Handling
config/business.php:
'holidays' => [
'2023-12-25', // Christmas
'2024-01-01', // New Year
],
Time Zone Awareness
config/business.php:
'timezone' => 'America/New_York',
Business::setTimezone('Europe/London')->nextBusinessDay(now());
Custom Business Rules
Business::extend(function ($business) {
$business->isBusinessHour = function (Carbon $date) {
// Custom logic (e.g., exclude weekends + specific hours)
return $date->isWeekday() && $date->hour >= 10 && $date->hour < 18;
};
});
Time Zone Mismatches
config/business.php timezone matches your application’s default or the input Carbon instance.Business::setTimezone('UTC')->nextBusinessDay(now());
Holiday Overrides
// Saturday holiday (should still skip)
Business::nextBusinessDay(Carbon::parse('2023-12-23')); // Skips to Monday
Recurring Holidays
spatie/calendar to generate holidays dynamically and merge them into config/business.php.Edge Cases in addHours/subtractHours
// Adding 10 hours at 5 PM (business hours end at 5 PM)
Business::addHours(Carbon::parse('2023-01-01 17:00'), 10);
// Result: 2023-01-02 09:00 (next business day)
addBusinessDays() for day-level adjustments instead.Carbon Instance Mutability
addHours() mutate the Carbon instance by default. Use copy() to avoid side effects:
$original = now();
$adjusted = Business::addHours($original->copy(), 2);
Inspect Business Rules
dd(Business::getBusinessRules()); // Shows parsed config
Check Business Day Status
if (Business::isBusinessDay(now())) {
// Logic for business days
}
Log Calculations
Enable debug mode in config/business.php:
'debug' => env('APP_DEBUG', false),
Logs will appear in Laravel’s log channel.
Custom Business Day Logic
Override the isBusinessDay method in your service provider:
Business::extend(function ($business) {
$business->isBusinessDay = function (Carbon $date) {
return parent::isBusinessDay($date) && $date->day === 15; // Only on the 15th
};
});
Dynamic Config Loading Load business hours from a database or API:
$hours = Cache::remember('business_hours', now()->addHours(1), function () {
return DB::table('business_hours')->first();
});
Business::setHours($hours['open'], $hours['close']);
Testing
Use Business::fake() to mock business hours in tests:
Business::fake()
->businessHours('00:00', '23:59') // 24/7 for tests
->holidays([]);
// Test logic here
Business::shouldReceive('nextBusinessDay')->andReturn(now()->addDay());
How can I help you explore Laravel packages today?