Add PropValueResource for managing property values; update TenantResource to include collapsed props data
This commit is contained in:
50
app/Domains/Prop/Resources/PropValueResource.php
Normal file
50
app/Domains/Prop/Resources/PropValueResource.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Prop\Resources;
|
||||
|
||||
use App\Domains\Prop\Concerns\Propable;
|
||||
use App\Domains\Prop\Models\ModelPropValue;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @mixin ModelPropValue
|
||||
*/
|
||||
class PropValueResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* @param iterable<ModelPropValue> $propValues
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function collapsed(iterable $propValues): array
|
||||
{
|
||||
return collect($propValues)
|
||||
->mapWithKeys(fn (ModelPropValue $propValue) => [
|
||||
$propValue->prop->codigo => $propValue->value,
|
||||
])
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function collapsedFromPropable(object $model): array
|
||||
{
|
||||
if (! in_array(Propable::class, class_uses_recursive($model), true)) {
|
||||
throw new InvalidArgumentException('The given model must use the Propable trait.');
|
||||
}
|
||||
|
||||
return static::collapsed(
|
||||
$model->propValues()->with('prop')->get(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [$this->prop->codigo => $this->value];
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Domains\Tenant\Resources;
|
||||
|
||||
use App\Domains\Prop\Resources\PropValueResource;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
@@ -20,6 +21,7 @@ class TenantResource extends JsonResource
|
||||
'codigo' => $this->codigo,
|
||||
'nombre' => $this->nombre,
|
||||
'dominio' => $this->dominio,
|
||||
'props' => PropValueResource::collapsedFromPropable($this->resource),
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user