58 lines
1.4 KiB
PHP
58 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\Supplier;
|
|
|
|
class SupplierRepository
|
|
{
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public function create(array $data): Supplier
|
|
{
|
|
$supplier = Supplier::create($this->extractSupplierAttributes($data));
|
|
$this->syncCategories($supplier, $data['supplier_category_ids'] ?? []);
|
|
|
|
return $supplier;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public function update(Supplier $supplier, array $data): Supplier
|
|
{
|
|
$supplier->update($this->extractSupplierAttributes($data));
|
|
$this->syncCategories($supplier, $data['supplier_category_ids'] ?? []);
|
|
|
|
return $supplier->fresh();
|
|
}
|
|
|
|
/**
|
|
* @param array<int|string>|null $categoryIds
|
|
*/
|
|
public function syncCategories(Supplier $supplier, array $categoryIds): void
|
|
{
|
|
$ids = array_values(array_filter(array_map('intval', $categoryIds)));
|
|
|
|
$supplier->supplierCategories()->sync($ids);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function extractSupplierAttributes(array $data): array
|
|
{
|
|
return collect($data)->only([
|
|
'name',
|
|
'url',
|
|
'contact_person',
|
|
'email',
|
|
'phone',
|
|
'country_id',
|
|
'notes',
|
|
'active',
|
|
])->all();
|
|
}
|
|
}
|