<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

/**
 * Creates and returns the Service Options Model for an Order.
 *
 * Example
 */
trait OrderServiceOptions
{
	/*
	// Information required during the order process
	protected array $order_attributes = [
		'options.input'=>[
			'request'=>'options.input',
			'key'=>'column',
			'validation'=>'required|string:10',
			'validation_message'=>'It is a required field.',
		],
	];

	protected string $order_model = NULL;
	*/

	public function orderValidation(Request $request): ?Model
	{
		if ((! isset($this->order_attributes)) || (! isset($this->order_model)))
			return NULL;

		$request->validate(collect($this->order_attributes)->pluck('validation','request')->toArray());

		$o = new $this->order_model;

		$x = [];
		foreach ($this->order_attributes as $k => $v)
			$x[$v['key']] = $request->input($k);

		$o->forceFill(array_undot($x));

		// @todo Make this automatic
		$o->site_id = config('site')->site_id;

		return $o;
	}
}