first commit

This commit is contained in:
kali 2024-05-28 17:10:06 +03:00
parent 92b3d15cc7
commit 49a337bd0f
43 changed files with 1799 additions and 0 deletions

107
ActiveForm.php Executable file
View File

@ -0,0 +1,107 @@
<?php
namespace itguild\forms\form;
use itguild\forms\form\builders\ButtonBuilder;
use itguild\forms\form\builders\CheckBoxBuilder;
use itguild\forms\form\builders\HiddenBuilder;
use itguild\forms\form\builders\RadioButtonBuilder;
use itguild\forms\form\builders\SelectBuilder;
use itguild\forms\form\builders\TextAreaBuilder;
use itguild\forms\form\builders\TextInputBuilder;
use itguild\forms\form\inputs\BaseInput;
use itguild\forms\form\inputs\Button;
use itguild\forms\form\inputs\Checkbox;
use itguild\forms\form\inputs\Hidden;
use itguild\forms\form\inputs\RadioButton;
use itguild\forms\form\inputs\Select;
use itguild\forms\form\inputs\TextArea;
use itguild\forms\form\inputs\TextInput;
class ActiveForm
{
private BaseInput $fieldObject;
/**
* @param string $action
* @return void
*/
public function beginForm(string $action): void
{
echo "<form method='POST' action='$action'>";
}
/**
* @return void
*/
public function endForm(): void
{
echo "</form>";
}
public function field($class, string $name, array $params = [])
{
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);
}
elseif ($class === Checkbox::class) {
$this->fieldObject = CheckBoxBuilder::build($name, $params);
}
elseif ($class === Button::class) {
$this->fieldObject = ButtonBuilder::build($name, $params);
}
elseif ($class === RadioButton::class){
$this->fieldObject = RadioButtonBuilder::build($name, $params);
}
elseif ($class === Hidden::class){
$this->fieldObject = HiddenBuilder::build($name, $params);
}
else {
$this->fieldObject = new $class($name, $params);
}
return $this;
}
public function setLabel(string $title): self
{
$this->fieldObject->setLabel($title);
return $this;
}
public function setOptions(array $options): self
{
$this->fieldObject->setOptions($options);
return $this;
}
public function setTemplate($template): self
{
$this->fieldObject->setTemplate($template);
return $this;
}
public function render()
{
$this->fieldObject->create();
$this->fieldObject->render();
}
public function fetch()
{
$this->fieldObject->create();
return $this->fieldObject->fetch();
}
}

110
Form.php Executable file
View File

@ -0,0 +1,110 @@
<?php
namespace itguild\forms\form;
use itguild\forms\form\inputs\Button;
use itguild\forms\form\inputs\Checkbox;
use itguild\forms\form\inputs\Label;
use itguild\forms\form\inputs\RadioButton;
use itguild\forms\form\inputs\Select;
use itguild\forms\form\inputs\TextArea;
use itguild\forms\form\inputs\TextInput;
class Form {
/**
* @param string $action
* @return void
*/
public function beginForm(string $action): void
{
echo "<form method='POST' action='$action'>";
}
/**
* @param string $name
* @param array $paramsArray
* @return void
*/
public function textInput(string $name, array $paramsArray = []): void
{
TextInput::build($name, $paramsArray);
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public function checkBox(string $name, string $value, array $paramsArray = []): void
{
Checkbox::build($name, $value, $paramsArray);
}
/**
* @param string $title
* @param array $paramsArray
* @return void
*/
public function label(string $title, array $paramsArray = []): void
{
Label::build($title, $paramsArray);
}
/**
* @param string $name
* @param array $paramsArray
* @return void
*/
public function radio(string $name, array $paramsArray = []): void
{
RadioButton::build($name, $paramsArray);
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public function textarea(string $name, string $value = "", array $paramsArray = []): void
{
TextArea::build($name, $value, $paramsArray);
}
/**
* @param string $name
* @param array $options
* @param $value
* @param array $paramsArray
* @return void
*/
public function select(string $name, array $options = [], $value = null, array $paramsArray = []): void
{
Select::build($name, $options, $value, $paramsArray);
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public function button(string $name, string $value, array $paramsArray = []): void
{
Button::build($name, $value, $paramsArray);
}
/**
* @return void
*/
public function endForm(): void
{
echo "</form>";
}
}

86
JsonForm.php Executable file
View File

@ -0,0 +1,86 @@
<?php
namespace itguild\forms\form;
use itguild\forms\form\builders\Builder;
use itguild\forms\form\builders\LabelBuilder;
use itguild\forms\form\mappers\JsonInputMapper;
use itguild\forms\form\traits\CreateParams;
class JsonForm
{
use CreateParams;
private $jsonData;
private $html = '';
public function __construct($json)
{
$this->jsonData = json_decode($json, true);
}
public function beginForm($params = []): string
{
$paramsString = $this->createParams($params);
return "<form $paramsString >";
}
public function endForm(): string
{
return "</form>";
}
public function convertHTML(): void
{
$this->html .= $this->beginForm($this->jsonData['meta']);
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();
}
}
$this->html .= $this->endForm();
//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;
}
public function fetch(): string
{
return $this->html;
}
}

12
builders/Builder.php Executable file
View File

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

19
builders/ButtonBuilder.php Executable file
View File

@ -0,0 +1,19 @@
<?php
namespace itguild\forms\form\builders;
use itguild\forms\form\inputs\Button;
class ButtonBuilder
{
public static function build(string $name, array $params = [])
{
$value = $params['value'] ?? null;
$typeInput = $params['typeInput'] ?? null;
unset($params['value']);
unset($params['typeInput']);
return new Button(name: $name, value: $value ?? null, typeInput: $typeInput, paramsArray: $params);
}
}

16
builders/CheckBoxBuilder.php Executable file
View File

@ -0,0 +1,16 @@
<?php
namespace itguild\forms\form\builders;
use itguild\forms\form\inputs\Checkbox;
class CheckBoxBuilder
{
public static function build(string $name, array $params = []): Checkbox
{
$value = $params['value'] ?? "";
unset($params['value']);
return new Checkbox(name: $name, value: $value, paramsArray: $params);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace itguild\forms\form\builders;
use itguild\forms\form\inputs\Hidden;
class HiddenBuilder
{
public static function build(string $name, array $params = []): Hidden
{
$value = $params['value'] ?? "";
unset($params['value']);
return new Hidden(name: $name, value: $value, paramsArray: $params);
}
}

13
builders/LabelBuilder.php Executable file
View File

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

14
builders/RadioButtonBuilder.php Executable file
View File

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

20
builders/SelectBuilder.php Executable file
View File

@ -0,0 +1,20 @@
<?php
namespace itguild\forms\form\builders;
use itguild\forms\form\inputs\Select;
class SelectBuilder
{
public static function build(string $name, array $params = [])
{
$value = $params['value'] ?? null;
unset($params['value']);
$options = $params['options'] ?? [];
unset($params['options']);
return new Select(name: $name, options: $options, value: $value, paramsArray: $params);
}
}

17
builders/TextAreaBuilder.php Executable file
View File

@ -0,0 +1,17 @@
<?php
namespace itguild\forms\form\builders;
use itguild\forms\form\inputs\TextArea;
class TextAreaBuilder
{
public static function build(string $name, array $params = [])
{
$value = $params['value'] ?? "";
unset($params['value']);
return new TextArea(name: $name, value: $value, paramsArray: $params);
}
}

15
builders/TextInputBuilder.php Executable file
View File

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

18
composer.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "itguild/form",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Itguild\\forms\\": "src/"
}
},
"authors": [
{
"name": "Kavalar",
"email": "apuc06@mail.ru"
}
],
"minimum-stability": "dev",
"require": {}
}

226
core/CgRequest.php Normal file
View File

@ -0,0 +1,226 @@
<?php
namespace itguild\forms\form\core;
use AllowDynamicProperties;
use Rakit\Validation\Validator;
#[AllowDynamicProperties]
class CgRequest
{
/**
* @var string $host Абсолютный адрес сервера
*/
public $host;
/**
* @var array $headers Заголовки запроса
*/
public $headers;
/**
* @var array
*/
public $data = [];
/**
* @var array
*/
public $errors = [];
public function __construct()
{
$this->headers = $this->getRequestHeaders();
$this->load();
}
/**
* @return array
*/
public function rules()
{
return [];
}
/**
* @return array
*/
public function messages()
{
return [];
}
/**
* Возвращает абсолютный адрес сервера.
* @return string
*/
public function getHost(): string
{
if ($this->host !== null) {
return $this->host;
}
$http = $this->getIsSecure() ? 'https' : 'http';
if ($this->headerExist('Host')) {
$this->host = $http . '://' . $this->getHeader('Host');
} elseif (isset($_SERVER['SERVER_NAME'])) {
$this->host = $http . '://' . $_SERVER['SERVER_NAME'];
}
return $this->host;
}
/**
* Возвращает true если шифрование https, иначе false.
* @return bool
*/
public function getIsSecure(): bool
{
if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)) {
return true;
}
return false;
}
/**
* Проверяет был ли передан заголовок запроса.
* @return bool
*/
public function headerExist($header): bool
{
return isset($this->headers[$header]);
}
/**
* Возвращает заголовок запроса
* @param string $header Заголовок.
* @param mixed $defaultValue Значение если, параметр не передан.
* @return mixed|null
*/
public function getHeader($header, $defaultValue = null)
{
return $this->headers[$header] ?? $defaultValue;
}
/**
* Возвращает GET - параметр.
* @param string $param Параметр.
* @param mixed $defaultValue Значение если, параметр не передан.
* @return mixed
*/
public function get($param = null, $defaultValue = null)
{
if (is_null($param)) {
return $_GET;
}
return $_GET[$param] ?? $defaultValue;
}
/**
* Возвращает POST - параметр.
* @param string $param Параметр.
* @param mixed $defaultValue Значение если, параметр не передан.
* @return mixed
*/
public function post($param = null, $defaultValue = null)
{
if (is_null($param)) {
return $_POST;
}
return $_POST[$param] ?? $defaultValue;
}
/**
* Был ли POST - запрос.
* @return bool
*/
public function isPost(): bool
{
return ($_SERVER['REQUEST_METHOD'] === 'POST');
}
/**
* Был ли GET - запрос.
* @return bool
*/
public function isGet(): bool
{
return ($_SERVER['REQUEST_METHOD'] === 'GET');
}
/**
* Загружаем свойсва
*/
public function load(): void
{
if (!empty($_REQUEST)) {
foreach ($_REQUEST as $key => $item) {
$this->{$key} = $item;
$this->data[$key] = $item;
}
}
}
/**
* @return bool
*/
public function validate()
{
if (!empty($this->data)) {
$valid = new Validator();
$validation = $valid->make($this->data, $this->rules());
$validation->setMessages($this->messages());
$validation->validate();
if ($validation->fails()) {
$this->errors = $validation->errors();
return false;
}
}
return true;
}
/**
* @return array
*/
public function getMessagesArray()
{
$msgs = [];
if($this->errors){
foreach ($this->errors->toArray() as $item){
$msgs[] = array_values($item)[0];
}
}
return $msgs;
}
/**
* @return array
*/
protected function getRequestHeaders()
{
$headers = array();
foreach ($_SERVER as $key => $value) {
if (substr($key, 0, 5) <> 'HTTP_') {
continue;
}
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$headers[$header] = $value;
}
return $headers;
}
}

59
core/cg_view/CgView.php Normal file
View File

@ -0,0 +1,59 @@
<?php
namespace itguild\forms\form\core\cg_view;
class CgView
{
public string $viewPath = '';
public bool|string $layout = false;
public function __construct()
{
}
public function render(string $view, array $data = []): void
{
$content = $this->createContent($view, $data);
echo $content;
}
public function fetch(string $view, array $data = []): false|string
{
$content = $this->createContent($view, $data);
return $content;
}
private function createContent(string $view, array $data = []): false|string
{
ob_start();
$cgView = new self();
$cgView->viewPath = $this->viewPath;
foreach ($data as $key => $datum){
${"$key"} = $datum;
}
include ($this->viewPath . $view);
$content = ob_get_contents();
ob_end_clean ();
ob_start();
$file_content = $content;
if ($this->layout){
if (file_exists($this->viewPath . $this->layout)){
include ($this->viewPath . $this->layout);
$file_content = ob_get_contents();
}
}
ob_end_clean ();
return $file_content;
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace itguild\forms\form\exceptions;
class CreateFormException extends \Exception
{
function __construct(string $msg)
{
$this->message = "Create form error: " . $msg;
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace itguild\forms\form\exceptions;
class FieldTypeNotFound extends \Exception
{
function __construct(string $type)
{
$this->message = "Тип поля $type не найден";
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace itguild\forms\form\exceptions;
class FormNotFoundException extends \Exception
{
function __construct($formId)
{
$this -> message = "Форма $formId не найдена";
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace itguild\forms\form\exceptions;
class FormResultNotFoundException extends \Exception
{
function __construct($id)
{
$this -> message = "Ответ формы $id не найдена";
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace itguild\forms\form\helpers;
class TranslitorHelper
{
public static function translit($str){
$russian = array('А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З',
'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У',
'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я',
'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к',
'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц',
'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я', ' ');
$translit = array('A', 'B', 'V', 'G', 'D', 'E', 'E', 'Gh', 'Z',
'I', 'Y', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F',
'H', 'C', 'Ch', 'Sh', 'Sch', 'Y', 'Y', 'Y', 'E', 'Yu', 'Ya', 'a',
'b', 'v', 'g', 'd', 'e', 'e', 'gh', 'z', 'i', 'y', 'k', 'l', 'm',
'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'sch',
'y', 'y', 'y', 'e', 'yu', 'ya', '_');
return str_replace($russian, $translit, $str);
}
}

66
inputs/BaseInput.php Executable file
View File

@ -0,0 +1,66 @@
<?php
namespace itguild\forms\form\inputs;
use itguild\forms\form\templates\Template;
abstract class BaseInput
{
protected Template $inputTemplate;
protected bool $hasLabel = false;
protected string $html = '';
protected string|Label $label = "";
protected string $labelString = "";
/**
* @return void
*/
public function render(): void
{
echo $this->html;
}
public function fetch(): string
{
return $this->html;
}
public abstract function create();
/**
* @param string|Label $title
* @return $this
*/
public function setLabel(string|Label $title): self
{
$this->hasLabel = true;
$this->label= $title;
return $this;
}
protected function createLabel()
{
if($this->hasLabel) {
if(is_string($this->label)){
$this->labelString = "<label>$this->label</label>";
}
else {
$this->labelString= $this->label->create()->fetch();
}
}
}
/**
* @param $template
* @return $this
*/
public function setTemplate($template): self
{
$this->inputTemplate = new $template();
return $this;
}
}

59
inputs/Button.php Executable file
View File

@ -0,0 +1,59 @@
<?php
namespace itguild\forms\form\inputs;
use itguild\forms\form\templates\Simple\SimpleTemplate;
use itguild\forms\form\traits\CreateParams;
class Button extends BaseInput
{
use CreateParams;
private $typeInput;
private string $name;
private string $value;
private array $paramsArray;
/**
* @param string $name
* @param string $value
* @param array $paramsArray
*/
public function __construct(string $name, string $value, $typeInput, array $paramsArray = [])
{
$this->typeInput = $typeInput;
$this->name = $name;
$this->paramsArray = $paramsArray;
$this->value = $value;
$this->inputTemplate = new SimpleTemplate();
}
/**
* @return $this
*/
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
$label = "";
$button = "<input type='$this->typeInput' value='$this->value' name='$this->name' $paramsString>";
$this->createLabel();
$this->html = str_replace('{input}', $button, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public static function build( string $name, string $value, string $typeInput, array $paramsArray = []): void
{
$button = new self($name, $value, $typeInput, $paramsArray);
$button->create()->render();
}
}

60
inputs/Checkbox.php Executable file
View File

@ -0,0 +1,60 @@
<?php
namespace itguild\forms\form\inputs;
use itguild\forms\form\templates\Simple\SimpleTemplate;
use itguild\forms\form\traits\CreateParams;
class Checkbox extends BaseInput
{
use CreateParams;
private string $name;
private string $value;
private array $paramsArray;
/**
* @param string $name
* @param string $value
* @param array $paramsArray
*/
public function __construct(string $name, string $value = '', array $paramsArray = [])
{
$this->name = $name;
$this->value = $value;
$this->paramsArray = $paramsArray;
$this->inputTemplate = new SimpleTemplate();
}
/**
* @return $this
*/
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
$checkBox = "<input name='$this->name' type='checkbox' value='$this->value' $paramsString >";
$label = "";
$this->createLabel();
$this->html = str_replace('{input}', $checkBox, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public static function build(string $name, string $value, array $paramsArray): void
{
$checkBox = new self($name, $value, $paramsArray);
$checkBox->create()->render();
}
}

60
inputs/Hidden.php Normal file
View File

@ -0,0 +1,60 @@
<?php
namespace itguild\forms\form\inputs;
use itguild\forms\form\templates\Simple\SimpleTemplate;
use itguild\forms\form\traits\CreateParams;
class Hidden extends BaseInput
{
use CreateParams;
private string $name;
private string $value;
private array $paramsArray;
/**
* @param string $name
* @param string $value
* @param array $paramsArray
*/
public function __construct(string $name, string $value = '', array $paramsArray = [])
{
$this->name = $name;
$this->value = $value;
$this->paramsArray = $paramsArray;
$this->inputTemplate = new SimpleTemplate();
}
/**
* @return $this
*/
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
$hidden = "<input name='$this->name' type='hidden' value='$this->value' $paramsString >";
$label = "";
$this->createLabel();
$this->html = str_replace('{input}', $hidden, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public static function build(string $name, string $value, array $paramsArray): void
{
$hidden = new self($name, $value, $paramsArray);
$hidden->create()->render();
}
}

58
inputs/Label.php Executable file
View File

@ -0,0 +1,58 @@
<?php
namespace itguild\forms\form\inputs;
use itguild\forms\form\traits\CreateParams;
class Label extends BaseInput
{
use CreateParams;
private string $title;
private array $paramsArray;
/**
* @param string $title
* @param array $paramsArray
*/
public function __construct(string $title, array $paramsArray = [])
{
$this->title = $title;
$this->paramsArray = $paramsArray;
}
/**
* @return $this
*/
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
$this->html = "<label $paramsString >$this->title</label>";
return $this;
}
/**
* @return string
*/
public function getHtml(): string
{
return $this->html;
}
/**
* @param string $title
* @param array $paramsArray
* @return void
*/
public static function build(string $title, array $paramsArray = [])
{
$label = new self($title, $paramsArray);
$label->create()->render();
}
}

63
inputs/RadioButton.php Executable file
View File

@ -0,0 +1,63 @@
<?php
namespace itguild\forms\form\inputs;
use itguild\forms\form\templates\Simple\SimpleTemplate;
use itguild\forms\form\traits\CreateOption;
use itguild\forms\form\traits\CreateParams;
class RadioButton extends BaseInput
{
use CreateParams;
use CreateOption;
private $name;
private $paramsArray;
private $options = [];
private $value;
public function __construct(string $name, array $paramsArray = [])
{
$this->name = $name;
$this->paramsArray = $paramsArray;
$this->inputTemplate = new SimpleTemplate();
}
/**
* @return $this
*/
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
$optionsString = $this->createOption($this->options, $this->value);
$radioButton = "<input name='$this->name' type='radio' $paramsString>$optionsString";
$label = "";
$this->createLabel();
$this->html = str_replace('{input}', $radioButton, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}
/**
* @param string $name
* @param array $paramsArray
* @return void
*/
public static function build(string $name, array $paramsArray = []): void
{
$radioButton = new self($name, $paramsArray);
$radioButton->create()->render();
}
/**
* @param $options
* @return $this
*/
public function setOptions($options)
{
$this->options = array_merge($options, $this->options);
return $this;
}
}

80
inputs/Select.php Executable file
View File

@ -0,0 +1,80 @@
<?php
namespace itguild\forms\form\inputs;
use itguild\forms\form\templates\Simple\SimpleTemplate;
use itguild\forms\form\traits\CreateOption;
use itguild\forms\form\traits\CreateParams;
class Select extends BaseInput
{
use CreateParams;
use CreateOption;
private $name;
private $options;
private $value;
private $paramsArray;
/**
* @param string $name
* @param array $options
* @param $value
* @param array $paramsArray
*/
public function __construct(string $name, array $options = [], $value = null, array $paramsArray = [])
{
$this->name = $name;
$this->options = $options;
$this->value = $value;
$this->paramsArray = $paramsArray;
$this->inputTemplate = new SimpleTemplate();
}
/**
* @return $this
*/
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
$optionsString = $this->createOption($this->options, $this->value);
$label = "";
$select = "<select name='$this->name' $paramsString>$optionsString</select>";
$this->createLabel();
$this->html = str_replace('{input}', $select, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}
/**
* @param string $name
* @param array $options
* @param $value
* @param array $paramsArray
* @return void
*/
public static function build(string $name, array $options = [], $value = null, array $paramsArray = []): void
{
$select = new self($name, $options, $value, $paramsArray);
$select->create()->render();
}
/**
* @param $options
* @return $this
*/
public function setOptions($options)
{
$this->options = array_merge($options, $this->options);
return $this;
}
}

60
inputs/TextArea.php Executable file
View File

@ -0,0 +1,60 @@
<?php
namespace itguild\forms\form\inputs;
use itguild\forms\form\templates\Simple\SimpleTemplate;
use itguild\forms\form\traits\CreateParams;
class TextArea extends BaseInput
{
use CreateParams;
private $name;
private $value;
private $paramsArray;
/**
* @param string $name
* @param string $value
* @param array $paramsArray
*/
public function __construct(string $name, string $value = '', array $paramsArray = [])
{
$this->name = $name;
$this->value = $value;
$this->paramsArray = $paramsArray;
$this->inputTemplate = new SimpleTemplate();
}
/**
* @return $this
*/
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
$label = "";
$textarea = "<textarea name='$this->name' $paramsString>$this->value</textarea>";
$this->createLabel();
$this->html = str_replace('{input}', $textarea, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public static function build(string $name, string $value, array $paramsArray = []): void
{
$textarea = new self($name, $value, $paramsArray);
$textarea->create()->render();
}
}

58
inputs/TextInput.php Executable file
View File

@ -0,0 +1,58 @@
<?php
namespace itguild\forms\form\inputs;
use itguild\forms\form\templates\Simple\SimpleTemplate;
use itguild\forms\form\traits\CreateParams;
class TextInput extends BaseInput
{
use CreateParams;
private string $name;
private array $paramsArray;
/**
* @param string $name
* @param array $paramsArray
*/
public function __construct(string $name, array $paramsArray = [])
{
$this->name = $name;
$this->paramsArray = $paramsArray;
$this->inputTemplate = new SimpleTemplate();
}
/**
* @return self
*/
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
$label = "";
$input = "<input name='$this->name' $paramsString >";
$this->createLabel();
$this->html = str_replace('{input}', $input, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}
/**
* @param string $name
* @param array $paramsArray
* @return void
*/
public static function build(string $name, array $paramsArray = []): void
{
$input = new self($name, $paramsArray);
$input->create()->render();
}
}

36
mappers/JsonInputMapper.php Executable file
View File

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

View File

@ -0,0 +1,36 @@
<?php
namespace itguild\forms\form\migrations;
use Illuminate;
use Illuminate\Database\Schema\Blueprint;
class FormsInputMigration extends Illuminate\Database\Migrations\Migration
{
public static function up(): void
{
Illuminate\Database\Capsule\Manager::schema()->create("form_input", function (Blueprint $table) {
$table->id("id");
$table->unsignedBigInteger('form_id');
$table->unsignedBigInteger('input_type_id');
$table->string('label');
$table->string('name');
$table->text('params');
$table->integer(11)->default(1);
$table->timestamps();
$table->index('form_id');
$table->index('input_type_id');
$table->foreign('form_id', 'fk_form_input_form')->references('id')->on('form');
$table->foreign('input_type_id', 'fk_form_input_input_type')->references('id')->on('input_type');
});
}
public static function down(): void
{
Illuminate\Database\Capsule\Manager::schema()->drop("form_input");
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace itguild\forms\form\migrations;
use Illuminate;
use itguild\forms\migrations\DB;
class FormsMigration
{
public static function up(): void
{
Illuminate\Database\Capsule\Manager::schema()->create("form", function ($table) {
$table->id('id');
$table->string('title');
$table->integer('status');
$table->text('params')->nullable();
$table->timestamps();
});
}
public static function get()
{
return DB::forms('form_input')-> get();
}
public static function down(): void
{
Illuminate\Database\Capsule\Manager::schema()->drop("form");
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace itguild\forms\form\migrations;
use Illuminate;
class FormsResMigration
{
public static function up(): void
{
Illuminate\Database\Capsule\Manager::schema()->create("form_res", function ($table) {
$table->id('id');
$table->unsignedBigInteger('form_id');
$table->json('data');
$table->timestamps();
$table->foreign('form_id', 'fk_form_form')->references('id')->on('form');
});
}
public static function down(): void
{
Illuminate\Database\Capsule\Manager::schema()->drop("form_res");
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace itguild\forms\form\migrations;
use Illuminate;
class InputsTypeMigration
{
public static function up(): void
{
Illuminate\Database\Capsule\Manager::schema()->create("input_type", function ($table) {
$table->id('id');
$table->string('type');
$table->string('name');
$table->integer('status');
});
}
public static function down(): void
{
Illuminate\Database\Capsule\Manager::schema()->drop("input_type");
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace itguild\forms\form\migrations;
use Illuminate;
class InputsValueMigrations
{
public static function up(): void
{
Illuminate\Database\Capsule\Manager::schema()->create("input_value", function ($table) {
$table->id('id');
$table->unsignedBigInteger('form_input_id');
$table->string('value');
$table->timestamps();
$table->foreign('form_input_id', 'fk_form_input')->references('id')->on('form_input');
});
}
public static function down(): void
{
Illuminate\Database\Capsule\Manager::schema()->drop("input_value");
}
}

30
migrations/UserMigration.php Executable file
View File

@ -0,0 +1,30 @@
<?php
namespace itguild\forms\form\migrations;
use Illuminate;
class UserMigration
{
public static function up(): void
{
Illuminate\Database\Capsule\Manager::schema()->create("user", function ($table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('userimage')->nullable();
$table->string('api_key')->nullable()->unique();
$table->rememberToken();
$table->timestamps();
});
}
public static function down(): void
{
Illuminate\Database\Capsule\Manager::schema()->drop("user");
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace itguild\forms\form\templates\Simple;
use itguild\forms\form\templates\Template;
class SimpleTemplate extends Template
{
/**
* @return string
*/
public static function getInputTemplate(): string
{
return "<div class='form-group'>{label}<br>{input}</div>";
}
}

11
templates/Template.php Executable file
View File

@ -0,0 +1,11 @@
<?php
namespace itguild\forms\form\templates;
abstract class Template
{
abstract static function getInputTemplate();
}

View File

@ -0,0 +1,37 @@
<?php
namespace itguild\forms\form\templates\bootstrap5;
use itguild\forms\form\templates\Template;
class Bootstrap5Template extends Template
{
/**
* @return string
*/
public static function getInputTemplate(): string
{
return "<div class='form-group'>{label}{input}</div>";
}
public static function getTextAreaTemplate(): string
{
return "<div class='form-group'>{label}{textarea}</div>";
}
public static function getSelectTemplate(): string
{
return "<div class='form-group'>{label}{select}</div>";
}
public static function getRadioTemplate(): string
{
return "<div class='form-group'>{label}{radio}</div>";
}
public static function getCheckBoxTemplate(): string
{
return "<div class='form-group'>{label}{checkbox}</div>";
}
public static function getButtonTemplate(): string
{
return "<div class='form-group'>{label}{button}</div>";
}
}

21
traits/CreateOption.php Executable file
View File

@ -0,0 +1,21 @@
<?php
namespace itguild\forms\form\traits;
trait CreateOption
{
/**
* @param array $options
* @param $value
* @return string
*/
public function createOption(array $options, $value = null): string
{
$optionsString = "";
foreach ($options as $val => $title){
$selected = (int)$value === $val ? "selected" : "";
$optionsString .= "<option $selected value='$val'>$title</option>";
}
return $optionsString;
}
}

24
traits/CreateParams.php Executable file
View File

@ -0,0 +1,24 @@
<?php
namespace itguild\forms\form\traits;
trait CreateParams
{
/**
* @param array $data
* @return string
*/
public function createParams(array $data = []): string
{
$paramsString = "";
foreach($data as $key => $param){
if(is_string($param)){
$paramsString .= $key . "='" . $param . "'";
}
}
return $paramsString;
}
}

32
widgets/BaseWidget.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace itguild\forms\form\widgets;
use itguild\forms\form\core\cg_view\CgView;
abstract class BaseWidget
{
protected CgView $cgView;
public function __construct()
{
$this->cgView = new CgView();
$this->cgView->viewPath = VIEW_PATH;
}
/**
* @return self
*/
public static function create(): BaseWidget
{
return new static();
}
/**
* @param array $data
* @return mixed
*/
abstract public function run(array $data = []);
}

View File

@ -0,0 +1,62 @@
<?php
/**
* @var \itguild\forms\form\ActiveForm $form
* @var \itguild\forms\app\DTO\FormDTO $dto
* @var \itguild\forms\form\core\cg_view\CgView $cgView
*/
namespace itguild\forms\form\widgets;
use Illuminate\Database\Eloquent\Collection;
use itguild\forms\app\models\InputTypeModel;
use itguild\forms\app\services\InputValueService;
use itguild\forms\form\ActiveForm;
class FormFieldBlockWidget extends BaseWidget
{
public function run(array $data = []): void
{
foreach ($this->prepare($data['fields']) as $field) {
$this->cgView->render('/admin/field_type/' . $field['view_name'] . ".php", $field);
}
}
private function prepare(Collection $rawFields): array
{
$fields = [];
$i = 0;
foreach ($rawFields as $field) {
$params = json_decode($field['params'], true);
$viewName = \itguild\forms\app\models\InputTypeModel::getViewNameByTypeId($field['input_type_id']);
if (!isset($fields[$field['name']])) {
$fields[$field['name']] = [
'name' => $field['name'],
'form' => new ActiveForm(),
'view_name' => $viewName,
'count' => $i,
];
}
if ($field['input_type_id'] == InputTypeModel::RADIO_TYPE) {
if (isset($fields[$field['name']]['value'])) {
$fields[$field['name']]['value'] .= "\n" . $params['value'];
} else {
$fields[$field['name']]['value'] = $params['value'];
}
} elseif ($field['input_type_id'] == InputTypeModel::SELECT_TYPE) {
$fields[$field['name']]['value'] = implode("\n", InputValueService::getInputValuesById($field['id']));
} else {
$fields[$field['name']]['value'] = $params['value'] ?? null;
$fields[$field['name']]['placeholder'] = $params['placeholder'] ?? null;
}
$i++;
}
return $fields;
}
}