style: Laravel Pint 코드 포맷팅 적용
- PSR-12 스타일 가이드 준수 - 302개 파일 스타일 이슈 자동 수정 - 코드 로직 변경 없음 (포맷팅만)
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\Models\Materials\Material;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class MaterialService extends Service
|
||||
{
|
||||
@@ -14,6 +14,7 @@ protected function v(array $input, array $rules)
|
||||
if ($v->fails()) {
|
||||
return ['error' => $v->errors()->first(), 'code' => 422];
|
||||
}
|
||||
|
||||
return $v->validated();
|
||||
}
|
||||
|
||||
@@ -23,21 +24,23 @@ public function getMaterials(array $params)
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
$p = $this->v($params, [
|
||||
'q' => 'nullable|string|max:100',
|
||||
'q' => 'nullable|string|max:100',
|
||||
'category' => 'nullable|integer|min:1',
|
||||
'page' => 'nullable|integer|min:1',
|
||||
'page' => 'nullable|integer|min:1',
|
||||
'per_page' => 'nullable|integer|min:1|max:200',
|
||||
]);
|
||||
if (isset($p['error'])) return $p;
|
||||
if (isset($p['error'])) {
|
||||
return $p;
|
||||
}
|
||||
|
||||
$q = Material::query()
|
||||
->where('tenant_id', $tenantId); // SoftDeletes가 있으면 기본적으로 deleted_at IS NULL
|
||||
|
||||
if (!empty($p['category'])) {
|
||||
$q->where('category_id', (int)$p['category']);
|
||||
if (! empty($p['category'])) {
|
||||
$q->where('category_id', (int) $p['category']);
|
||||
}
|
||||
if (!empty($p['q'])) {
|
||||
$kw = '%' . $p['q'] . '%';
|
||||
if (! empty($p['q'])) {
|
||||
$kw = '%'.$p['q'].'%';
|
||||
$q->where(function ($w) use ($kw) {
|
||||
$w->where('item_name', 'like', $kw)
|
||||
->orWhere('name', 'like', $kw)
|
||||
@@ -49,7 +52,7 @@ public function getMaterials(array $params)
|
||||
$q->orderBy('id');
|
||||
|
||||
$perPage = $p['per_page'] ?? 20;
|
||||
$page = $p['page'] ?? null;
|
||||
$page = $p['page'] ?? null;
|
||||
|
||||
return $q->paginate($perPage, ['*'], 'page', $page);
|
||||
}
|
||||
@@ -64,11 +67,13 @@ public function getMaterial(int $id)
|
||||
->where('tenant_id', $tenantId)
|
||||
->find($id);
|
||||
|
||||
if (!$row) return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404];
|
||||
if (! $row) {
|
||||
return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404];
|
||||
}
|
||||
|
||||
// 모델에서 casts가 없을 수 있으니 안전하게 배열화
|
||||
$row->attributes = is_array($row->attributes) ? $row->attributes : ($row->attributes ? json_decode($row->attributes, true) : null);
|
||||
$row->options = is_array($row->options) ? $row->options : ($row->options ? json_decode($row->options, true) : null);
|
||||
$row->options = is_array($row->options) ? $row->options : ($row->options ? json_decode($row->options, true) : null);
|
||||
|
||||
return $row;
|
||||
}
|
||||
@@ -77,44 +82,46 @@ public function getMaterial(int $id)
|
||||
public function setMaterial(array $params)
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
$p = $this->v($params, [
|
||||
'category_id' => 'nullable|integer|min:1',
|
||||
'name' => 'required|string|max:100',
|
||||
'unit' => 'required|string|max:10',
|
||||
'category_id' => 'nullable|integer|min:1',
|
||||
'name' => 'required|string|max:100',
|
||||
'unit' => 'required|string|max:10',
|
||||
'is_inspection' => 'nullable|in:Y,N',
|
||||
'search_tag' => 'nullable|string',
|
||||
'remarks' => 'nullable|string',
|
||||
'attributes' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'options' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'search_tag' => 'nullable|string',
|
||||
'remarks' => 'nullable|string',
|
||||
'attributes' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'options' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'material_code' => 'nullable|string|max:50',
|
||||
'specification' => 'nullable|string|max:100',
|
||||
]);
|
||||
if (isset($p['error'])) return $p;
|
||||
if (isset($p['error'])) {
|
||||
return $p;
|
||||
}
|
||||
|
||||
// 기존 normalizeAttributes 사용(그대로), options는 새 normalizeOptions 사용
|
||||
$attributes = $this->normalizeAttributes($p['attributes'] ?? null);
|
||||
$options = $this->normalizeOptions($p['options'] ?? null);
|
||||
$options = $this->normalizeOptions($p['options'] ?? null);
|
||||
|
||||
$itemName = $this->buildItemName($p['name'], $attributes);
|
||||
$specText = $p['specification'] ?? $this->buildSpecText($attributes);
|
||||
|
||||
$m = new Material();
|
||||
$m->tenant_id = $tenantId;
|
||||
$m->category_id = $p['category_id'] ?? null;
|
||||
$m->name = $p['name'];
|
||||
$m->item_name = $itemName;
|
||||
$m = new Material;
|
||||
$m->tenant_id = $tenantId;
|
||||
$m->category_id = $p['category_id'] ?? null;
|
||||
$m->name = $p['name'];
|
||||
$m->item_name = $itemName;
|
||||
$m->specification = $specText;
|
||||
$m->material_code = $p['material_code'] ?? null;
|
||||
$m->unit = $p['unit'];
|
||||
$m->unit = $p['unit'];
|
||||
$m->is_inspection = $p['is_inspection'] ?? 'N';
|
||||
$m->search_tag = $p['search_tag'] ?? null;
|
||||
$m->remarks = $p['remarks'] ?? null;
|
||||
$m->attributes = $attributes ?? null;
|
||||
$m->options = $options ?? null;
|
||||
$m->created_by = $userId ?? 0;
|
||||
$m->updated_by = $userId ?? null;
|
||||
$m->search_tag = $p['search_tag'] ?? null;
|
||||
$m->remarks = $p['remarks'] ?? null;
|
||||
$m->attributes = $attributes ?? null;
|
||||
$m->options = $options ?? null;
|
||||
$m->created_by = $userId ?? 0;
|
||||
$m->updated_by = $userId ?? null;
|
||||
$m->save();
|
||||
|
||||
return $this->getMaterial($m->id);
|
||||
@@ -124,55 +131,63 @@ public function setMaterial(array $params)
|
||||
public function updateMaterial(int $id, array $params = [])
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
/** @var Material|null $exists */
|
||||
$exists = Material::query()->where('tenant_id', $tenantId)->find($id);
|
||||
if (!$exists) return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404];
|
||||
if (! $exists) {
|
||||
return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404];
|
||||
}
|
||||
|
||||
$p = $this->v($params, [
|
||||
'category_id' => 'nullable|integer|min:1',
|
||||
'name' => 'nullable|string|max:100',
|
||||
'unit' => 'nullable|string|max:10',
|
||||
'category_id' => 'nullable|integer|min:1',
|
||||
'name' => 'nullable|string|max:100',
|
||||
'unit' => 'nullable|string|max:10',
|
||||
'is_inspection' => 'nullable|in:Y,N',
|
||||
'search_tag' => 'nullable|string',
|
||||
'remarks' => 'nullable|string',
|
||||
'attributes' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'options' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'search_tag' => 'nullable|string',
|
||||
'remarks' => 'nullable|string',
|
||||
'attributes' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'options' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'material_code' => 'nullable|string|max:50',
|
||||
'specification' => 'nullable|string|max:100',
|
||||
]);
|
||||
if (isset($p['error'])) return $p;
|
||||
if (isset($p['error'])) {
|
||||
return $p;
|
||||
}
|
||||
|
||||
$currentAttrs = is_array($exists->attributes) ? $exists->attributes
|
||||
: ($exists->attributes ? json_decode($exists->attributes, true) : null);
|
||||
$currentOpts = is_array($exists->options) ? $exists->options
|
||||
$currentOpts = is_array($exists->options) ? $exists->options
|
||||
: ($exists->options ? json_decode($exists->options, true) : null);
|
||||
|
||||
// 변경 점만 정규화
|
||||
$attrs = array_key_exists('attributes', $p)
|
||||
? $this->normalizeAttributes($p['attributes'])
|
||||
: $currentAttrs;
|
||||
$opts = array_key_exists('options', $p)
|
||||
$opts = array_key_exists('options', $p)
|
||||
? $this->normalizeOptions($p['options'])
|
||||
: $currentOpts;
|
||||
|
||||
$baseName = array_key_exists('name', $p) ? ($p['name'] ?? $exists->name) : $exists->name;
|
||||
$baseName = array_key_exists('name', $p) ? ($p['name'] ?? $exists->name) : $exists->name;
|
||||
|
||||
$exists->category_id = $p['category_id'] ?? $exists->category_id;
|
||||
$exists->name = $baseName;
|
||||
$exists->item_name = $this->buildItemName($baseName, $attrs);
|
||||
$exists->category_id = $p['category_id'] ?? $exists->category_id;
|
||||
$exists->name = $baseName;
|
||||
$exists->item_name = $this->buildItemName($baseName, $attrs);
|
||||
$exists->specification = array_key_exists('specification', $p)
|
||||
? ($p['specification'] ?? null)
|
||||
: ($exists->specification ?: $this->buildSpecText($attrs));
|
||||
$exists->material_code = $p['material_code'] ?? $exists->material_code;
|
||||
$exists->unit = $p['unit'] ?? $exists->unit;
|
||||
$exists->unit = $p['unit'] ?? $exists->unit;
|
||||
$exists->is_inspection = $p['is_inspection'] ?? $exists->is_inspection;
|
||||
$exists->search_tag = $p['search_tag'] ?? $exists->search_tag;
|
||||
$exists->remarks = $p['remarks'] ?? $exists->remarks;
|
||||
$exists->search_tag = $p['search_tag'] ?? $exists->search_tag;
|
||||
$exists->remarks = $p['remarks'] ?? $exists->remarks;
|
||||
|
||||
if (array_key_exists('attributes', $p)) $exists->attributes = $attrs;
|
||||
if (array_key_exists('options', $p)) $exists->options = $opts;
|
||||
if (array_key_exists('attributes', $p)) {
|
||||
$exists->attributes = $attrs;
|
||||
}
|
||||
if (array_key_exists('options', $p)) {
|
||||
$exists->options = $opts;
|
||||
}
|
||||
|
||||
$exists->updated_by = $userId ?? $exists->updated_by;
|
||||
$exists->save();
|
||||
@@ -190,7 +205,9 @@ public function destroyMaterial(int $id)
|
||||
->where('tenant_id', $tenantId)
|
||||
->find($id);
|
||||
|
||||
if (!$row) return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404];
|
||||
if (! $row) {
|
||||
return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404];
|
||||
}
|
||||
|
||||
$row->delete();
|
||||
|
||||
@@ -208,47 +225,61 @@ public function destroyMaterial(int $id)
|
||||
→ specification: "두께 10T, 길이 150CM"
|
||||
------------------------- */
|
||||
|
||||
private function normalizeAttributes(null|array $attrs): ?array
|
||||
private function normalizeAttributes(?array $attrs): ?array
|
||||
{
|
||||
if (!$attrs) return null;
|
||||
if (! $attrs) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$out = [];
|
||||
foreach ($attrs as $a) {
|
||||
if (!is_array($a)) continue;
|
||||
$label = trim((string)($a['label'] ?? ''));
|
||||
$value = trim((string)($a['value'] ?? ''));
|
||||
$unit = trim((string)($a['unit'] ?? ''));
|
||||
if ($label === '' && $value === '' && $unit === '') continue;
|
||||
if (! is_array($a)) {
|
||||
continue;
|
||||
}
|
||||
$label = trim((string) ($a['label'] ?? ''));
|
||||
$value = trim((string) ($a['value'] ?? ''));
|
||||
$unit = trim((string) ($a['unit'] ?? ''));
|
||||
if ($label === '' && $value === '' && $unit === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$out[] = ['label' => $label, 'value' => $value, 'unit' => $unit];
|
||||
}
|
||||
|
||||
return $out ?: null;
|
||||
}
|
||||
|
||||
private function buildItemName(string $name, ?array $attrs): string
|
||||
{
|
||||
if (!$attrs || count($attrs) === 0) return $name;
|
||||
if (! $attrs || count($attrs) === 0) {
|
||||
return $name;
|
||||
}
|
||||
|
||||
$parts = [];
|
||||
foreach ($attrs as $a) {
|
||||
$value = (string)($a['value'] ?? '');
|
||||
$unit = (string)($a['unit'] ?? '');
|
||||
$chunk = trim($value . $unit);
|
||||
if ($chunk !== '') $parts[] = $chunk;
|
||||
$value = (string) ($a['value'] ?? '');
|
||||
$unit = (string) ($a['unit'] ?? '');
|
||||
$chunk = trim($value.$unit);
|
||||
if ($chunk !== '') {
|
||||
$parts[] = $chunk;
|
||||
}
|
||||
}
|
||||
return trim($name . ' ' . implode(' ', $parts));
|
||||
|
||||
return trim($name.' '.implode(' ', $parts));
|
||||
}
|
||||
|
||||
private function buildSpecText(?array $attrs): ?string
|
||||
{
|
||||
if (!$attrs || count($attrs) === 0) return null;
|
||||
if (! $attrs || count($attrs) === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$parts = [];
|
||||
foreach ($attrs as $a) {
|
||||
$label = (string)($a['label'] ?? '');
|
||||
$value = (string)($a['value'] ?? '');
|
||||
$unit = (string)($a['unit'] ?? '');
|
||||
$valueWithUnit = trim($value . $unit);
|
||||
$label = (string) ($a['label'] ?? '');
|
||||
$value = (string) ($a['value'] ?? '');
|
||||
$unit = (string) ($a['unit'] ?? '');
|
||||
$valueWithUnit = trim($value.$unit);
|
||||
|
||||
if ($label !== '' && $valueWithUnit !== '') {
|
||||
$parts[] = "{$label} {$valueWithUnit}";
|
||||
@@ -256,6 +287,7 @@ private function buildSpecText(?array $attrs): ?string
|
||||
$parts[] = $valueWithUnit;
|
||||
}
|
||||
}
|
||||
|
||||
return $parts ? implode(', ', $parts) : null;
|
||||
}
|
||||
|
||||
@@ -264,9 +296,11 @@ private function buildSpecText(?array $attrs): ?string
|
||||
* - 이미 리스트(triple)면 그대로 정규화
|
||||
* - 맵({"키":"값"})이면 [{label:키, value:값, unit:""}...]로 변환
|
||||
*/
|
||||
private function normalizeOptions(null|array $in): ?array
|
||||
private function normalizeOptions(?array $in): ?array
|
||||
{
|
||||
if (!$in) return null;
|
||||
if (! $in) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 연관 맵 형태인지 간단 판별
|
||||
$isAssoc = array_keys($in) !== range(0, count($in) - 1);
|
||||
@@ -274,24 +308,32 @@ private function normalizeOptions(null|array $in): ?array
|
||||
if ($isAssoc) {
|
||||
$out = [];
|
||||
foreach ($in as $k => $v) {
|
||||
$label = trim((string)$k);
|
||||
$value = is_scalar($v) ? trim((string)$v) : json_encode($v, JSON_UNESCAPED_UNICODE);
|
||||
if ($label === '' && $value === '') continue;
|
||||
$label = trim((string) $k);
|
||||
$value = is_scalar($v) ? trim((string) $v) : json_encode($v, JSON_UNESCAPED_UNICODE);
|
||||
if ($label === '' && $value === '') {
|
||||
continue;
|
||||
}
|
||||
$out[] = ['label' => $label, 'value' => $value, 'unit' => ''];
|
||||
}
|
||||
|
||||
return $out ?: null;
|
||||
}
|
||||
|
||||
// 리스트(triple) 정규화
|
||||
$out = [];
|
||||
foreach ($in as $a) {
|
||||
if (!is_array($a)) continue;
|
||||
$label = trim((string)($a['label'] ?? ''));
|
||||
$value = trim((string)($a['value'] ?? ''));
|
||||
$unit = trim((string)($a['unit'] ?? ''));
|
||||
if ($label === '' && $value === '' && $unit === '') continue;
|
||||
if (! is_array($a)) {
|
||||
continue;
|
||||
}
|
||||
$label = trim((string) ($a['label'] ?? ''));
|
||||
$value = trim((string) ($a['value'] ?? ''));
|
||||
$unit = trim((string) ($a['unit'] ?? ''));
|
||||
if ($label === '' && $value === '' && $unit === '') {
|
||||
continue;
|
||||
}
|
||||
$out[] = ['label' => $label, 'value' => $value, 'unit' => $unit];
|
||||
}
|
||||
|
||||
return $out ?: null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user