## Getting Started
### Minimal Setup
1. **Installation**
```bash
composer require baks-dev/reference-pants
Verify in composer.json that the package is listed under require.
First Use Case Access pants sizes via the facade or service container:
use BaksDev\ReferencePants\Facades\PantsSize;
// Get all available sizes
$sizes = PantsSize::all();
// Get a specific size (e.g., 30/32)
$size = PantsSize::find(30);
Where to Look First
BaksDev\ReferencePants\Facades\PantsSize (preferred for simplicity).PantsSize class directly if avoiding facades.README.md for size ranges (25/30 to 38/40) and methods.Fetching Sizes
PantsSize::all() to retrieve an array of all supported sizes.
$allSizes = PantsSize::all(); // Returns [25, 26, ..., 40]
PantsSize::find($waist) to get a size object.
$size = PantsSize::find(32); // Returns object with waist/length properties
$evenSizes = PantsSize::all()->filter(fn($size) => $size->waist % 2 === 0);
Integration with Laravel
// In AppServiceProvider@boot()
$this->app->bind('custom.pants.size', function () {
return new \BaksDev\ReferencePants\Services\CustomPantsSizeService();
});
Product model.
// Product.php
public function pantsSize()
{
return $this->belongsTo(PantsSize::class, 'size_id');
}
Localization
PantsSize::setLabel(30, '30/32 (Medium)');
Cache::remember('pants_size_labels', now()->addHours(1), function () {
return PantsSize::all()->pluck('label', 'waist');
});
Validation
use BaksDev\ReferencePants\Rules\ValidPantsSize;
$request->validate([
'pants_size' => ['required', new ValidPantsSize],
]);
Size Range Assumptions
PantsSize::find(24)) returns null. Validate inputs:
if (!$size = PantsSize::find($request->size)) {
throw new \InvalidArgumentException("Invalid pants size.");
}
Facade vs. Direct Instantiation
\BaksDev\ReferencePants\PantsSize directly. Use the facade or container binding to ensure consistency (e.g., for future label overrides).PHP 8.4+ Requirement
php -v and update config/app.php if needed:
'php' => '8.4',
Data Mutability
PantsSize objects are immutable by default. To modify labels or metadata, use the setLabel() method or extend the class:
class CustomPantsSize extends \BaksDev\ReferencePants\PantsSize {
public function setCustomProperty($key, $value) { ... }
}
if (!$size = PantsSize::find($input)) {
\Log::warning("Pants size {$input} not found in reference data.");
}
find() vs get()).Custom Size Logic
PantsSize class to add business logic:
class ExtendedPantsSize extends \BaksDev\ReferencePants\PantsSize {
public function isLarge(): bool {
return $this->waist >= 36;
}
}
Database Storage
// Migration
Schema::create('pants_sizes', function (Blueprint $table) {
$table->integer('waist')->unique();
$table->string('label');
});
// Model
class PantsSize extends \BaksDev\ReferencePants\PantsSize {
protected static function boot() {
parent::boot();
static::addGlobalScope('db', function (Builder $builder) {
$builder->from('pants_sizes');
});
}
}
Testing
$this->mock(BaksDev\ReferencePants\Facades\PantsSize::class)
->shouldReceive('find')
->andReturn(new \BaksDev\ReferencePants\PantsSize(32));
Performance
Cache::remember('all_pants_sizes', now()->addDays(7), function () {
return PantsSize::all();
});
setLabel()). For complex setups, consider wrapping it in a config-driven service.setLabel() are not persisted. Reapply them on app boot if needed:
// In AppServiceProvider@boot()
PantsSize::setLabel(30, 'Custom Label');
How can I help you explore Laravel packages today?