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

106 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Lib\Inputs\Definition;
use App\Enums\Formularies\InputTypeEnum;
/**
* @property string $title The input type placeholder
* @property string $formSlug The original Formulary slug.
* @property integer $type The input type value
* @property string|array<integer,string> $value The input value, either a string or an array
* with the value index and label
* (for Multiselect and checkboxes)
* @property boolean $required The input is required ?
* @package App\Lib\Inputs
*/
class InputData extends \stdClass
{
/**
* 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, ['title', 'formSlug', 'type', 'value', 'required'])) {
return $this->{$name};
}
throw new \TypeError("undefined property {$name}");
}
/**
* Make and instance
*
* @param string $title The place holder (localized from in App).
* @param string $formSlug The original Formulary slug.
* @param \App\Enums\Formularies\InputTypeEnum|integer $type The original Formulary type.
* @param string|array<integer,string> $data The of the input depending on Formulary type.
* @param boolean $required Is the input required.
* @return \App\Lib\Inputs\Definition\InputData
* @throws \RuntimeException If $type or $data is wrong.
*/
public static function make(string $title, string $formSlug, $type, $data, bool $required): self
{
$ipTypeData = new self();
$ipTypeData->title = $title;
$ipTypeData->formSlug = $formSlug;
$ipTypeData->type = (\is_object($type) and \get_class($type) === InputTypeEnum::class) ?
$type->value : $type;
$ipTypeData->value = $data;
$ipTypeData->required = $required;
self::assertIsInputTypeData($ipTypeData);
return $ipTypeData;
}
/**
* Confirm $data is of type InputTypeData
*
* @param \App\Lib\Inputs\Definition\InputData $data
* @return void
* @throws \RuntimeException If $data is wrong.
*/
public static function assertIsInputTypeData(InputData $data)
{
$title = $data->title;
$type = $data->type;
$formSlug = $data->formSlug;
$value = $data->value;
$required = $data->required;
// @phpstan-ignore function.alreadyNarrowedType
if (!\is_string($title)) {
throw new \RuntimeException("InputTypeData 'title' should be a string");
}
// @phpstan-ignore function.alreadyNarrowedType
if (!\is_string($formSlug)) {
throw new \RuntimeException("InputTypeData 'formSlug' should be a string");
}
// @phpstan-ignore function.alreadyNarrowedType
if (!\is_integer($type)) {
throw new \RuntimeException("InputTypeData 'type' should be an integer");
}
// @phpstan-ignore function.alreadyNarrowedType
if (!\is_bool($required)) {
throw new \RuntimeException("InputTypeData 'required' should be a boolean");
}
try {
$type = InputTypeEnum::make($type);
} catch (\BadMethodCallException $e) {
throw new \RuntimeException($e->getMessage());
}
// @phpstan-ignore function.impossibleType, function.alreadyNarrowedType, booleanAnd.alwaysFalse
if (!\is_null($value) && !\is_string($value) && !\is_array($value)) {
$type = \gettype($value);
$value = \json_encode($value);
throw new \RuntimeException(
"InputTypeData 'value' should be string or array given '{$value}' of type '{$type}'"
);
}
}
}