firstPHP/src/JsonForm.php

70 lines
1.6 KiB
PHP
Raw Normal View History

2024-03-20 17:46:58 +03:00
<?php
namespace itguild\forms;
use itguild\forms\builders\Builder;
use itguild\forms\builders\LabelBuilder;
use itguild\forms\debug\Debug;
use itguild\forms\mappers\JsonInputMapper;
use itguild\forms\inputs\BaseInput;
use itguild\forms\ActiveForm;
class JsonForm
{
private $jsonData;
private $html = '';
public function __construct($json)
{
$this->jsonData = json_decode($json, true);
}
public function convertHTML(): void
{
foreach ($this->jsonData['data'] as $item) {
if (isset($item["type"]) and isset($item["name"])) {
/**
* @var $builder Builder
*/
$builder = JsonInputMapper::getBuilder($item["type"]);
unset($item["type"]);
$name = $item["name"];
unset($item["name"]);
$label = '';
if (isset($item['label'])) {
$label = $this->createLabel($item['label']);
}
unset($item['label']);
$input = $builder::build($name, $item);
$input->setLabel($label)->create();
$this->html .= $input->fetch();
}
}
//return $this->fetch($html);
}
private function createLabel($labelParams)
{
$title = '';
if(isset($labelParams['title'])){
$title = $labelParams['title'];
unset($labelParams['title']);
}
return LabelBuilder::build($title, $labelParams);
}
public function render(): void
{
echo $this->html;
}
}