value, $required); $this->data = $data; } /** * Get property * * @param string $name * @return mixed * @throws \TypeError If prop is undefined. */ 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. */ public static function from(InputData $data, bool $required = true) { $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. */ public static function make(string $title, string $formSlug, $data, bool $required = true) { 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 */ 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(); } }