laravel-static-lists maintained by fomvasss
fomvasss/laravel-static-lists
Laravel bridge for fomvasss/laravel-static-lists.
Deterministic list builder for arrays/iterables for Laravel.
Install
composer require fomvasss/laravel-static-lists
Usage
use Fomvasss\LaravelStaticLists\Traits\HasStaticLists;
class Order extends Model {
use HasStaticLists;
public static function map(): array
{
return static::staticListBuild(static::$records, 'label', 'value', [
'only' => ['paid','new'], 'sort' => 'priority'
]);
}
}
// Or collection
collect($records)->listBuild('label', 'value', ['except' => ['done']]);
Usage examles
use Fomvasss\LaravelStaticLists\Traits\HasStaticLists;
class Order extends Model
{
use HasStaticLists;
protected static array $statusRecords = [
['value' => 'new', 'label' => 'Новий', 'priority' => 30],
['value' => 'paid', 'label' => 'Оплачено', 'priority' => 10],
['value' => 'shipped', 'label' => 'Відправлено','priority' => 20],
['value' => 'done', 'label' => 'Завершено', 'priority' => 40],
['value' => 'done', 'label' => 'Завершено DUP', 'priority' => 40], // дубль для прикладу unique
];
public static function statusesList(string $columnKey = null, string $indexKey = null, array $options = []): array
{
return self::staticListBuild(self::$statusRecords, $columnKey, $indexKey, $options);
}
// 1) Карта value => label
public static function map(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: 'label',
indexKey: 'value'
);
// ['new'=>'Новий','paid'=>'Оплачено','shipped'=>'Відправлено','done'=>'Завершено DUP'] (через останній дубль)
}
// 2) Тільки label як список (без ключів)
public static function labels(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: 'label'
);
// ['Новий','Оплачено','Відправлено','Завершено','Завершено DUP']
}
// 3) only з збереженням порядку
public static function onlyPaidThenNew(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: '*',
indexKey: 'value',
options: ['only' => ['paid','new']]
);
// ['paid'=>['value'=>'paid',...],'new'=>['value'=>'new',...]]
}
// 4) except (виключити значення)
public static function withoutFinal(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: 'label',
indexKey: 'value',
options: ['except' => ['done']]
);
// ['new'=>'Новий','paid'=>'Оплачено','shipped'=>'Відправлено']
}
// 5) added='*' (додати всі можливі, якщо попередньо відфільтровано only)
public static function onlyPaidPlusAll(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: 'label',
indexKey: 'value',
options: [
'only' => ['paid'], // стартовий набір
'added' => ['*'], // додати все інше
// за замовчуванням existing має пріоритет (override=false)
]
);
// Вихід: всі статуси, при конфліктах — зберігається існуючий 'paid'
}
// 6) added із override=true (додані заміщують існуючі)
public static function overrideExample(): array
{
// Уявімо, що в allRecords є альтернативний label для 'paid'
$all = array_map(fn($r) => $r, static::$statusRecords);
$all[] = ['value'=>'paid','label'=>'Оплачено (ALT)','priority'=>15];
return static::staticListBuild(
$all,
columnKey: 'label',
indexKey: 'value',
options: [
'only' => ['paid'],
'added' => ['paid','shipped'],
'override' => true,
]
);
// ['paid'=>'Оплачено (ALT)','shipped'=>'Відправлено']
}
// 7) Сортування за полем (priority asc/desc)
public static function sortedAsc(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: 'label',
indexKey: 'value',
options: ['sort' => 'priority', 'order' => 'asc']
);
// Порядок ключів: paid(10), shipped(20), new(30), done(40), done(40)
}
public static function sortedDesc(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: 'label',
indexKey: 'value',
options: ['sort' => 'priority', 'order' => 'desc']
);
// Порядок ключів: done(40), done(40), new(30), shipped(20), paid(10)
}
// 8) Сортування кастомним компаратором (done завжди вкінці)
public static function customSort(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: 'label',
indexKey: 'value',
options: [
'sort' => function ($a, $b) {
$rank = fn($v) => $v === 'done' ? 999 : 0;
return ($rank($a['value']) <=> $rank($b['value']))
?: (($a['priority'] ?? 0) <=> ($b['priority'] ?? 0));
},
]
);
// done в кінці, інші — за priority
}
// 9) unique по indexKey
public static function uniqueByValue(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: '*',
indexKey: 'value',
options: ['unique' => true]
);
// дубль 'done' прибрано; ключі унікальні
}
// 10) Отримати асоціативний масив по ключу (без columnKey)
public static function indexByValue(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: null,
indexKey: 'value'
);
// ['new'=>[...], 'paid'=>[...], 'shipped'=>[...], 'done'=>[...]] (останній дубль перемагає)
}
// 11) Chain: only + except + sort + повернути лише labels
public static function chain(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: 'label',
indexKey: null,
options: [
'only' => ['paid','new','done','shipped'],
'except' => ['new'],
'sort' => 'priority',
'order' => 'asc',
]
);
// ['Оплачено','Відправлено','Завершено','Завершено DUP']
}
// 12) Витягнути значення одного поля як список ID (наприклад для select)
public static function valuesOnly(): array
{
return static::staticListBuild(
static::$statusRecords,
columnKey: 'value'
);
// ['new','paid','shipped','done','done']
}
}