diff --git a/app/Domains/Prop/Resources/PropValueResource.php b/app/Domains/Prop/Resources/PropValueResource.php new file mode 100644 index 0000000..1a5b16a --- /dev/null +++ b/app/Domains/Prop/Resources/PropValueResource.php @@ -0,0 +1,50 @@ + $propValues + * @return array + */ + public static function collapsed(iterable $propValues): array + { + return collect($propValues) + ->mapWithKeys(fn (ModelPropValue $propValue) => [ + $propValue->prop->codigo => $propValue->value, + ]) + ->all(); + } + + /** + * @return array + */ + 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 + */ + public function toArray(Request $request): array + { + return [$this->prop->codigo => $this->value]; + } +} diff --git a/app/Domains/Tenant/Resources/TenantResource.php b/app/Domains/Tenant/Resources/TenantResource.php index 6327faf..aa5cac4 100644 --- a/app/Domains/Tenant/Resources/TenantResource.php +++ b/app/Domains/Tenant/Resources/TenantResource.php @@ -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, ];