laravel-dynamic-placeholder maintained by halaabusalim
Description
A Laravel package to generate context-aware placeholder content.
Author
Last update
2025/07/28 20:21
(dev-main)
License
Downloads
1
Laravel Dynamic Placeholder
A Laravel package to generate context-aware placeholder content for your applications.
Installation
- Install the package via Composer:
composer require halaabusalim/laravel-dynamic-placeholder
- Publish the configuration file:
php artisan vendor:publish --provider="Halaabusalim\LaravelDynamicPlaceholder\DynamicPlaceholderServiceProvider" --tag="config"
Usage
Using the Facade
use Halaabusalim\LaravelDynamicPlaceholder\Facades\DynamicPlaceholder;
// Generate text placeholders
$productName = DynamicPlaceholder::text('ecommerce.product_name');
$blogTitle = DynamicPlaceholder::text('blog.title');
// Generate number placeholders
$price = DynamicPlaceholder::number('ecommerce.price');
// Generate image placeholders
$imageUrl = DynamicPlaceholder::image('product-image');
Using Dependency Injection
use Halaabusalim\LaravelDynamicPlaceholder\DynamicPlaceholder;
class ProductController extends Controller
{
public function __construct(private DynamicPlaceholder $placeholder)
{
}
public function index()
{
$productName = $this->placeholder->text('ecommerce.product_name');
$price = $this->placeholder->number('ecommerce.price');
return view('products.index', compact('productName', 'price'));
}
}
Configuration
The package comes with a configuration file that you can customize. Edit config/dynamic-placeholder.php:
return [
'contexts' => [
'ecommerce' => [
'product_name' => ['Laptop', 'Smartphone', 'Headphones', 'Smartwatch'],
'description' => [
'High-performance device for professionals.',
'Sleek design with cutting-edge technology.',
'Perfect for everyday use.'
],
'price' => ['min' => 50, 'max' => 1000],
],
'blog' => [
'title' => ['Top 10 Tips for Success', 'How to Start a Blog', 'The Future of Tech'],
'content' => ['Lorem ipsum dolor sit amet...', 'In this article, we explore...'],
],
],
];
Methods
text(string $context): string
Returns a random text from the configured context.
number(string $context): float
Returns a random number within the configured range.
image(string $context): string
Returns a placeholder image URL (currently uses via.placeholder.com).
Testing
Running Tests
- Unit Tests with PHPUnit:
./vendor/bin/phpunit
- Manual Testing:
php test-manual.php
Test Coverage
The test suite covers:
- ✅ Text placeholder generation
- ✅ Number placeholder generation with ranges
- ✅ Image placeholder URL generation
- ✅ Error handling for missing contexts
- ✅ Facade functionality
- ✅ Service provider registration
- ✅ Configuration merging
- ✅ Singleton pattern verification
License
The MIT License (MIT). Please see License File for more information.