Installation:
composer require baks-dev/reference-clothing
Ensure your composer.json requires PHP 8.4+.
First Use Case: Fetch all available sizes in a Laravel controller or service:
use BaksDev\ReferenceClothing\Facades\ClothingSize;
$sizes = ClothingSize::all(); // Returns array of size labels (e.g., ['2XS', 'XS', ..., '7XL'])
Where to Look First:
BaksDev\ReferenceClothing\Facades\ClothingSize (primary entry point).config/clothing-sizes.php for customizable defaults.tests/Feature/ClothingSizeTest.php for edge cases (e.g., invalid inputs).Fetching Sizes:
$allSizes = ClothingSize::all(); // ['2XS', 'XS', ..., '7XL']
$adultSizes = ClothingSize::filter(fn($size) => str_starts_with($size, ['S', 'M', 'L', 'XL']));
Integration with Eloquent:
Add a size column to a Product model and validate against the package:
use BaksDev\ReferenceClothing\Rules\ValidClothingSize;
class Product extends Model {
protected $casts = ['size' => 'string'];
protected function rules() {
return [
'size' => ['required', new ValidClothingSize],
];
}
}
Localization: Override default labels via config:
// config/clothing-sizes.php
'labels' => [
'2XS' => 'Двойной экстра-малыш',
'XS' => 'Экстра-малыш',
// ...
],
Then fetch localized sizes:
$localizedSizes = ClothingSize::getLocalizedLabels();
API Responses: Use the package to standardize size responses:
return response()->json([
'product' => [
'name' => 'T-Shirt',
'sizes' => ClothingSize::all(), // Consistent format
],
]);
Frontend Integration: Pass sizes to Blade:
@php
$sizes = \BaksDev\ReferenceClothing\Facades\ClothingSize::all();
@endphp
<select>
@foreach($sizes as $size)
<option>{{ $size }}</option>
@endforeach
</select>
Case Sensitivity:
The package treats sizes as case-sensitive (e.g., '2xs' ≠ '2XS'). Normalize inputs:
$normalizedSize = strtoupper($userInput);
Missing Config:
If extending labels, ensure the config file exists (config/clothing-sizes.php). Run:
php artisan vendor:publish --provider="BaksDev\ReferenceClothing\ClothingSizeServiceProvider"
Validation Rule Scope:
The ValidClothingSize rule defaults to all sizes. Restrict it to a subset (e.g., adult sizes) by passing a closure:
new ValidClothingSize(fn($size) => str_contains($size, ['S', 'M', 'L', 'XL']))
Performance:
Avoid calling ClothingSize::all() in loops. Cache the result:
$sizes = Cache::remember('clothing.sizes', now()->addHours(1), fn() => ClothingSize::all());
ClothingSize::isValid($size) method to debug:
if (!ClothingSize::isValid('8XL')) {
// Handle error
}
config/app.php under providers.Custom Size Ranges: Extend the base class to add niche sizes (e.g., shoe sizes):
use BaksDev\ReferenceClothing\Contracts\SizeCollection;
class ShoeSizeCollection implements SizeCollection {
public function all(): array {
return ['35', '36', ..., '45'];
}
}
Register it via the service provider’s extend() method.
Dynamic Label Generation:
Override the getLocalizedLabels() method in a custom facade:
class CustomClothingSizeFacade extends Facade {
public static function getLocalizedLabels(): array {
return array_map(fn($size) => "Размер: {$size}", parent::getLocalizedLabels());
}
}
Database Seeding:
Seed a sizes table using the package’s data:
use BaksDev\ReferenceClothing\Facades\ClothingSize;
DB::table('sizes')->insert(
array_map(fn($size) => ['name' => $size], ClothingSize::all())
);
How can I help you explore Laravel packages today?