b2pweb/bdf-util
Utilities for working with BDF (Binary Data Format) files in PHP/Laravel. Includes helpers to read, write, parse, and validate BDF data plus common convenience functions for import/export workflows and integration into your application.
Installation
composer require b2pweb/bdf-util
Add the service provider to config/app.php:
'providers' => [
// ...
B2PWeb\BDFUtil\BDFUtilServiceProvider::class,
],
First Use Case: Basic Helper Functions The package provides utility functions for common tasks like:
BDFUtil::slugify('Hello World') → hello-world).BDFUtil::flatten([1, [2, 3]]) → [1, 2, 3]).BDFUtil::formatDate('2023-01-01', 'd/m/Y')).BDFUtil::getFileExtension('image.png')).Example:
use B2PWeb\BDFUtil\Facades\BDFUtil;
$slug = B2PUtil::slugify('My Awesome Post');
// Returns: 'my-awesome-post'
Where to Look First
app/BDFUtil.php (if published) or vendor/b2pweb/bdf-util/src/Facades/BDFUtil.php.src/Helpers/BDFUtil.php for all available functions.README.md for use cases. Otherwise, inspect the Helpers class directly.$title = "Laravel 10: New Features & Best Practices";
$slug = BDFUtil::slugify($title, '-'); // 'laravel-10-new-features-best-practices'
$truncated = BDFUtil::truncate('Long text here...', 10); // 'Long text...'
$template = "Hello {name}, welcome to {site}!";
$result = BDFUtil::replaceTemplate($template, ['name' => 'John', 'site' => 'Laravel']);
// 'Hello John, welcome to Laravel!'
$nested = [1, [2, [3, 4]]];
$flat = BDFUtil::flatten($nested); // [1, 2, 3, 4]
$users = [
['name' => 'John', 'role' => 'admin'],
['name' => 'Jane', 'role' => 'user'],
];
$grouped = BDFUtil::groupBy($users, 'role');
// ['admin' => [...], 'user' => [...]]
$array1 = ['a' => 1, 'b' => 2];
$array2 = ['b' => 3, 'c' => 4];
$merged = BDFUtil::mergeArrays($array1, $array2, function ($val1, $val2) {
return $val1 + $val2;
});
// ['a' => 1, 'b' => 5, 'c' => 4]
$ago = BDFUtil::timeAgo('2023-01-01'); // '2 years ago'
$formatted = BDFUtil::formatDate('2023-12-25', 'Y-m-d H:i:s'); // '2023-12-25 00:00:00'
$carbon = BDFUtil::parseDate('2023-01-01');
$carbon->addDays(5);
$extension = BDFUtil::getFileExtension('document.pdf'); // 'pdf'
$mime = BDFUtil::getMimeType('document.pdf'); // 'application/pdf'
$baseUrl = 'https://example.com';
$fullUrl = BDFUtil::makeUrl($baseUrl, '/posts/1'); // 'https://example.com/posts/1'
slugify for route parameters.
// In a Form Request
public function rules()
{
return [
'title' => 'required|string',
'slug' => 'required|unique:posts|slug:title', // Custom rule using BDFUtil
];
}
// In a service provider
Blade::directive('slug', function ($expression) {
return "<?php echo BDFUtil::slugify({$expression}); ?>";
});
Usage in Blade:
<h1>{{ slug($post->title) }}</h1>
php artisan vendor:publish --provider="B2PWeb\BDFUtil\BDFUtilServiceProvider" --tag="config"
Example config (config/bdf-util.php):
'slug_separator' => '_', // Default is '-'
'default_locale' => 'en_US',
Facade vs. Helper Class
BDFUtil) and a static helper class (\B2PWeb\BDFUtil\Helpers\BDFUtil).config/app.php.Locale-Sensitive Functions
formatDate or timeAgo may rely on the app’s locale.config/app.php).BDFUtil::formatDate('2023-12-25', 'd/m/Y', 'fr_FR'); // '25/12/2023'
Array Manipulation Side Effects
flatten or groupBy may return new arrays without modifying the original.$array = BDFUtil::flatten($array) won’t alter $array unless documented).BDFUtil::flatten($array, true) if a "mutate" option exists (check docs).File/URL Helper Limitations
getMimeType may not support all file types out of the box.null or a generic type for unsupported files.mime/mime-types for robust MIME detection.Performance with Large Arrays
flatten or groupBy may be resource-intensive for large datasets.$flat = collect($nestedArray)->flatten()->toArray();
Missing Documentation
slugify handling special characters).src/Helpers/BDFUtil.php) for undocumented features or edge cases. Example:
BDFUtil::slugify('Café au Lait'); // 'caf-au-lait' (handles accents)
BDFUtil class or create a decorator:
class CustomBDFUtil extends \B2PWeb\BDFUtil\Helpers\
How can I help you explore Laravel packages today?