This commit is contained in:
kali 2024-03-20 17:46:58 +03:00
parent fb37605942
commit fd39285e41
18 changed files with 265 additions and 73 deletions

View File

@ -8,6 +8,7 @@ use itguild\forms\ActiveForm;
use itguild\forms\inputs\TextArea;
use itguild\forms\inputs\TextInput;
use itguild\forms\inputs\Select;
use Itguild\PhpCgSelect\CGSelect;
$form = new ActiveForm()
?>
@ -23,6 +24,7 @@ $form = new ActiveForm()
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" />
<link href="https://cdn.jsdelivr.net/npm/vanillajs-datepicker/dist/css/datepicker.min.css" rel="stylesheet">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
@ -164,8 +166,8 @@ $form = new ActiveForm()
$form->field(class: Select::class, name: 'select2', params: ['value' => 2, 'class' => "form-control"])
->setOptions(['1' => 'bbb1', 2 => 'vvv3', 3 => 'ggg3', 4 => 'fgfgfgfg4'])
->render();
$form->field(\itguild\forms\inputs\Radio::class, name: "radio1", params: ["id" => "1"])->setLabel("sds?") ->render();
$form->field(\itguild\forms\inputs\Radio::class, name: "radio1", params: ["id" => "3"])->render();
$form->field(\itguild\forms\inputs\RadioButton::class, name: "radio1", params: ["id" => "1"])->setLabel("sds?") ->render();
$form->field(\itguild\forms\inputs\RadioButton::class, name: "radio1", params: ["id" => "3"])->render();
$form->field(\itguild\forms\inputs\Checkbox::class, name: "checkbox1", params: ["value" => "1"])->render();
$form->field(\itguild\forms\inputs\Checkbox::class, name: "checkbox2", params: ["value" => "1"])->render();
$form->field(\itguild\forms\inputs\Button::class, name: "checkbox2", params: ["value" => "Submit"])->render();

32
example_json.php Normal file
View File

@ -0,0 +1,32 @@
<?php
ini_set("display_errors", 1);
error_reporting(-1);
require_once "vendor/autoload.php";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="vendor/twbs/bootstrap/dist/css/bootstrap.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" />
<title>Document</title>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-12">
<?php
$form = new \itguild\forms\JsonForm(file_get_contents("form.json"));
$form->convertHTML();
$form->render();
?>
</div>
</div>
</div>
</body>
</html>

View File

@ -1,18 +1,30 @@
{
"textInpt": {
"name": "email",
"placeholder": "Email",
"id": "EmailId"
"meta": {
"action": "some",
"id": "id_form"
},
"email": "dsadas@dsadsa",
"name": "1",
"lastname": "edas",
"button": "3",
"radio1": "on",
"textarea": "dsadasads",
"select": "class3",
"one": "ads",
"two": "dsa",
"radio2": "on",
"select2": "1"
}
"data": [
{
"type": "textInput",
"name": "email",
"placeholder": "Email",
"label": {
"title": "Email", "for": "emailId", "class": "form-label"
},
"class": "form-control"
},
{
"type": "textInput",
"name": "phone",
"placeholder": "Телефон",
"class": "form-control",
"style": "color:RED;"
},
{
"type": "textArea",
"name": "description",
"class": "form-control",
"placeholder": "Описание"
}
]
}

View File

@ -2,7 +2,7 @@
use itguild\forms\Form;
use \itguild\forms\inputs\TextInput;
use \itguild\forms\inputs\Radio;
use \itguild\forms\inputs\RadioButton;
use \itguild\forms\inputs\Select;
use \itguild\forms\ActiveForm;
@ -37,13 +37,13 @@ $activeForm->field(class: TextInput::class, name: 'one', params: ["style" => "co
$activeForm->field(class: TextInput::class, name: 'two', params: [])
->render();
$activeForm->field(class: Radio::class, name: 'radio2',params: ["style" => "color:RED;display:flex"])
$activeForm->field(class: RadioButton::class, name: 'radio2',params: ["style" => "color:RED;display:flex"])
->setOptions(['1' => 'bbb1', '2' => 'vvv3', 3 => 'ggg3', 4 => 'fgfgfgfg4'])
->setTemplate("<div class='form-group'>{label}{input}</div>")
->setLabel("bbb222bbb")
->render();
$activeForm->field(class: Radio::class, name: 'radio2',params: ["style" => "color:RED;display:flex"])
$activeForm->field(class: RadioButton::class, name: 'radio2',params: ["style" => "color:RED;display:flex"])
->setLabel("bbbbbb")
->render();

View File

@ -4,14 +4,18 @@ namespace itguild\forms;
use itguild\forms\builders\ButtonBuilder;
use itguild\forms\builders\CheckBoxBuilder;
use itguild\forms\builders\RadioButtonBuilder;
use itguild\forms\builders\SelectBuilder;
use itguild\forms\builders\TextAreaBuilder;
use itguild\forms\builders\TextInputBuilder;
use itguild\forms\debug\Debug;
use itguild\forms\inputs\BaseInput;
use itguild\forms\inputs\Button;
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\RadioButton;
use itguild\forms\inputs\Select;
use itguild\forms\inputs\TextArea;
use itguild\forms\inputs\TextInput;
use itguild\forms\templates\Template;
class ActiveForm
@ -42,6 +46,9 @@ class ActiveForm
if ($class === Select::class){
$this->fieldObject = SelectBuilder::build($name, $params);
}
elseif ($class === TextInput::class){
$this->fieldObject = TextInputBuilder::build($name, $params);
}
elseif ($class === TextArea::class){
$this->fieldObject = TextAreaBuilder::build($name, $params);
}
@ -51,6 +58,9 @@ class ActiveForm
elseif ($class === Button::class) {
$this->fieldObject = ButtonBuilder::build($name, $params);
}
elseif ($class === RadioButton::class){
$this->fieldObject = RadioButtonBuilder::build($name, $params);
}
else {
$this->fieldObject = new $class($name, $params);
}
@ -85,4 +95,10 @@ class ActiveForm
$this->fieldObject->render();
}
public function fetch()
{
$this->fieldObject->create();
return $this->fieldObject->fetch();
}
}

View File

@ -6,7 +6,7 @@ use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\Select;
use itguild\forms\inputs\TextInput;
use itguild\forms\inputs\Label;
use itguild\forms\inputs\Radio;
use itguild\forms\inputs\RadioButton;
use itguild\forms\inputs\TextArea;
use itguild\forms\inputs\Button;
@ -61,7 +61,7 @@ class Form {
*/
public function radio(string $name, array $paramsArray = []): void
{
Radio::build($name, $paramsArray);
RadioButton::build($name, $paramsArray);
}
/**

69
src/JsonForm.php Normal file
View File

@ -0,0 +1,69 @@
<?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;
}
}

12
src/builders/Builder.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace itguild\forms\builders;
use itguild\forms\inputs\BaseInput;
interface Builder
{
public static function build(string $name, array $params = []): BaseInput;
}

View File

@ -0,0 +1,14 @@
<?php
namespace itguild\forms\builders;
use itguild\forms\inputs\Label;
use itguild\forms\inputs\RadioButton;
class LabelBuilder
{
public static function build(string $title, array $params = [])
{
return new Label(title: $title, paramsArray: $params);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace itguild\forms\builders;
use itguild\forms\inputs\RadioButton;
class RadioButtonBuilder
{
public static function build(string $name, array $params = [])
{
return new RadioButton(name: $name, paramsArray: $params);
}
}

View File

@ -2,18 +2,16 @@
namespace itguild\forms\builders;
use itguild\forms\inputs\Select;
use itguild\forms\inputs\TextArea;
class TextAreaBuilder
{
public static function build(string $name, array $params = [])
{
$value = $params['value'] ?? null;
$value = $params['value'] ?? "";
unset($params['value']);
return new TextArea(name: $name, value: $value, paramsArray: $params);
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace itguild\forms\builders;
use itguild\forms\inputs\BaseInput;
use itguild\forms\inputs\TextInput;
class TextInputBuilder implements Builder
{
public static function build(string $name, array $params = []): BaseInput
{
return new TextInput(name: $name, paramsArray: $params);
}
}

View File

@ -8,8 +8,8 @@ abstract class BaseInput
{
protected Template $inputTemplate;
protected bool $hasLabel = false;
protected string $html;
protected string $labelTitle = "";
protected string $html = '';
protected string|Label $label = "";
/**
* @return void
@ -19,16 +19,21 @@ abstract class BaseInput
echo $this->html;
}
public function fetch(): string
{
return $this->html;
}
public abstract function create();
/**
* @param string $title
* @param string|Label $title
* @return $this
*/
public function setLabel(string $title): self
public function setLabel(string|Label $title): self
{
$this->hasLabel = true;
$this->labelTitle = $title;
$this->label= $title;
return $this;
}

View File

@ -34,7 +34,7 @@ class Button extends BaseInput
{
$paramsString = $this->createParams($this->paramsArray);
$label = "";
$button = "<button name='$this->name' $paramsString>$this->value</button>";
$button = "<input type='button' name='$this->name' $paramsString>$this->value";
if($this->hasLabel == true) {
$label = "<label>$this->labelTitle</label>";
}

View File

@ -1,37 +0,0 @@
<?php
namespace itguild\forms\inputs;
use itguild\forms\inputs\BaseInput;
class CgSelectInput extends BaseInput
{
private string $selector;
private array $params;
private $params;
public function __construct(string $selector, array $params = [])
{
$this->selector = $selector;
$this->params = $params;
}
public function create()
{
$paramsString = $this->createParams($this->paramsArray);
$optionsString = $this->createOption($this->options, $this->value);
$label = "";
$select = "<select name='$this->name' $paramsString>$optionsString</select>";
if($this->hasLabel == true) {
$label = "<label>$this->labelTitle</label>";
}
$this->html = str_replace('{input}', $select, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $label, $this->html);
return $this;
}
public static function build(string $name, array $options = [], $value = null, array $paramsArray = []): void
{
$select = new self($name, $options, $value, $paramsArray);
$select->create()->render();
}
}

View File

@ -5,7 +5,7 @@ use itguild\forms\templates\Simple\SimpleTemplate;
use itguild\forms\traits\CreateOption;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
class Radio extends BaseInput
class RadioButton extends BaseInput
{
use CreateParams;
use CreateOption;
@ -32,7 +32,7 @@ class Radio extends BaseInput
$radioButton = "<input name='$this->name' type='radio' $paramsString>$optionsString";
$label = "";
if($this->hasLabel == true) {
$label = "<label>$this->labelTitle</label>";
$label = "<label >$this->labelTitle</label>";
}
$this->html = str_replace('{input}', $radioButton, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $label, $this->html);

View File

@ -39,7 +39,12 @@ class TextInput extends BaseInput
$label = "";
$input = "<input name='$this->name' $paramsString >";
if($this->hasLabel == true) {
$label = "<label>$this->labelTitle</label>";
if(is_string($this->label)){
$label = "<label>$this->labelTitle</label>";
}
else {
$label = $this->create()->fetch();
}
}
$this->html = str_replace('{input}', $input, $this->inputTemplate->getInputTemplate());

View File

@ -0,0 +1,35 @@
<?php
namespace itguild\forms\mappers;
use itguild\forms\builders\Builder;
use itguild\forms\builders\ButtonBuilder;
use itguild\forms\builders\LabelBuilder;
use itguild\forms\builders\RadioButtonBuilder;
use itguild\forms\builders\SelectBuilder;
use itguild\forms\builders\TextAreaBuilder;
use itguild\forms\builders\TextInputBuilder;
use itguild\forms\inputs\TextInput;
class JsonInputMapper
{
private static function getBuilders(): array
{
return [
"textInput" => TextInputBuilder::class,
"textArea" => TextAreaBuilder::class,
"radio" => RadioButtonBuilder::class,
"select" => SelectBuilder::class,
"button" => ButtonBuilder::class
];
}
public static function getBuilder(string $type): string
{
if(isset(self::getBuilders()[$type])){
return self::getBuilders()[$type];
}
return false;
}
}