141 lines
3.8 KiB
PHP
141 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Lib\Inputs\Definition;
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public $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;
|
|
}
|
|
|
|
/**
|
|
* Is this input required ?
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isRequired(): bool
|
|
{
|
|
return $this->data->required;
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
public static function from(InputData $data, bool $required = true)
|
|
{
|
|
$inputTypeDataClass = static::class;
|
|
try {
|
|
$type = \is_numeric($data->type) ? intval($data->type) : strval($data->type);
|
|
return new static(InputData::make($data->title, $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|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.
|
|
*/
|
|
public static function make(string $title, $data, bool $required = true)
|
|
{
|
|
return static::from(
|
|
InputData::make(
|
|
$title,
|
|
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
|
|
*/
|
|
public function toArray()
|
|
{
|
|
return (array)$this->data;
|
|
}
|
|
|
|
/**
|
|
* Convert the object to its JSON representation.
|
|
*
|
|
* @param integer $options
|
|
* @return string
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
|
|
*/
|
|
public function toJson($options = 0)
|
|
{
|
|
// phpcs:enable
|
|
return \json_encode($this->jsonSerialize(), $options);
|
|
}
|
|
|
|
/**
|
|
* Convert the object into something JSON serializable.
|
|
*
|
|
* @return array
|
|
*/
|
|
#[\ReturnTypeWillChange]
|
|
public function jsonSerialize()
|
|
{
|
|
return $this->toArray();
|
|
}
|
|
}
|