ldc-com-app-bo/app/Lib/Inputs/Definition/Input.php

198 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Lib\Inputs\Definition;
use App\Models\Formulary;
/**
* To manipulate Input
*
* @property-read \App\Lib\Inputs\Definition\InputData $data The input type data.
* @package App\Lib\Inputs
*/
abstract class Input implements InputInterface
{
/**
* The Input Type.
*
* @var \App\Lib\Inputs\Definition\InputData
*/
private $data;
/**
* Constructor
*
* @param \App\Lib\Inputs\Definition\InputData $data
* @param boolean $required
* @return void
* @throws \RuntimeException If $data is wrong.
*/
public function __construct(InputData $data, bool $required = true)
{
InputData::assertIsInputTypeData($data);
static::validateData($data->value, $required);
$this->data = $data;
}
/**
* Get property
*
* @param string $name
* @return mixed
* @throws \TypeError If prop is undefined.
*/
2023-08-31 12:50:58 +02:00
public function __get(string $name)
{
if (in_array($name, ['data'])) {
return $this->{$name};
}
throw new \TypeError("undefined property {$name}");
}
/**
* Is this input required ?
*
* @return boolean
*/
public function isRequired(): bool
{
return $this->data->required;
}
/**
* Get the matching formulary for this object
*
* @return \App\Models\Formulary|null
*/
public function getFormulary(): \App\Models\Formulary|null
{
// @phpstan-ignore return.type
return $this->formularyQuery()->first();
}
/**
* Get this input has a matching Formulary ?
*
* @return boolean
*/
public function hasFormulary(): bool
{
return $this->formularyQuery()->exists();
}
/**
* Get Matching formulary query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
private function formularyQuery(): \Illuminate\Database\Eloquent\Builder
{
return Formulary::query()
->where('slug', $this->data->formSlug)
->where('type', $this->data->type);
}
/**
* Parse DB line to Object
*
* ! Need to validate inputType
*
* @param \App\Lib\Inputs\Definition\InputData $data
* @param boolean $required
* @return static
* @throws \RuntimeException If the input type is not recognized, or data is invalid.
*/
2025-05-20 16:20:23 +02:00
public static function from(InputData $data, bool $required = true): static
{
$inputTypeDataClass = static::class;
$type = "";
try {
// @phpstan-ignore function.alreadyNarrowedType
$type = \is_numeric($data->type) ? intval($data->type) : strval($data->type);
return new static(
InputData::make($data->title, $data->formSlug, $type, $data->value, $required),
$required
);
} catch (\BadMethodCallException $e) {
throw new \RuntimeException("Could not instanciate {$type}");
} catch (\Illuminate\Validation\ValidationException $e) {
$encodedData = \json_encode($data);
throw new \RuntimeException("Data does not comply with {$inputTypeDataClass}, given {$encodedData}");
}
}
/**
* Parse DB line to Object
*
* ! Need to validate inputType
*
* @param string $title The line placeholder.
* @param string $formSlug The Form slug for this data.
* @param string|array $data The line with data.
* @param boolean $required The line is required.
* @return static
* @throws \RuntimeException If the input type is not recognized, or data is invalid.
*/
2025-05-20 16:20:23 +02:00
public static function make(string $title, string $formSlug, $data, bool $required = true): static
{
return static::from(
InputData::make(
$title,
$formSlug,
static::getType()->value,
$data,
$required
),
$required
);
}
/**
* Validate data using getValidator instance
*
* @param string|array $data The line with data.
* @param boolean $required The line is required.
* @return void
* @throws \Illuminate\Validation\ValidationException If the data is invalid.
*/
public static function validateData($data, bool $required): void
{
static::getValidator($data, $required)->validate();
}
// * TRANSFORMERS
/**
* Get the instance as an array.
*
* @return array
*/
2025-05-20 16:20:23 +02:00
public function toArray(): array
{
return (array) $this->data;
}
/**
* Convert the object to its JSON representation.
*
* @param integer $options
* @return string
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
*/
2025-05-20 16:20:23 +02:00
public function toJson($options = 0): string
{
// phpcs:enable
return \json_encode($this->jsonSerialize(), $options);
}
/**
* Convert the object into something JSON serializable.
*
* @return array
*/
2025-05-20 16:20:23 +02:00
public function jsonSerialize(): array
{
return $this->toArray();
}
}