This commit is contained in:
Kavalar 2024-07-25 12:33:47 +03:00
commit 3675ad134c
38 changed files with 1928 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
vendor

20
composer.json Executable file
View File

@ -0,0 +1,20 @@
{
"name": "itguild/forms",
"type": "library",
"autoload": {
"psr-4": {
"itguild\\forms\\": "src/"
}
},
"authors": [
{
"name": "Kavalar"
}
],
"require": {
"twbs/bootstrap": "5.0.2",
"itguild/php-cg-select-v2": "^0.1.0",
"vlucas/phpdotenv": "^5.6",
"rakit/validation": "^1.4"
}
}

602
composer.lock generated Normal file
View File

@ -0,0 +1,602 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "99168f8b78520188d5f655d53586261a",
"packages": [
{
"name": "graham-campbell/result-type",
"version": "v1.1.3",
"source": {
"type": "git",
"url": "https://github.com/GrahamCampbell/Result-Type.git",
"reference": "3ba905c11371512af9d9bdd27d99b782216b6945"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/GrahamCampbell/Result-Type/zipball/3ba905c11371512af9d9bdd27d99b782216b6945",
"reference": "3ba905c11371512af9d9bdd27d99b782216b6945",
"shasum": ""
},
"require": {
"php": "^7.2.5 || ^8.0",
"phpoption/phpoption": "^1.9.3"
},
"require-dev": {
"phpunit/phpunit": "^8.5.39 || ^9.6.20 || ^10.5.28"
},
"type": "library",
"autoload": {
"psr-4": {
"GrahamCampbell\\ResultType\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
}
],
"description": "An Implementation Of The Result Type",
"keywords": [
"Graham Campbell",
"GrahamCampbell",
"Result Type",
"Result-Type",
"result"
],
"support": {
"issues": "https://github.com/GrahamCampbell/Result-Type/issues",
"source": "https://github.com/GrahamCampbell/Result-Type/tree/v1.1.3"
},
"funding": [
{
"url": "https://github.com/GrahamCampbell",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/graham-campbell/result-type",
"type": "tidelift"
}
],
"time": "2024-07-20T21:45:45+00:00"
},
{
"name": "itguild/php-cg-select-v2",
"version": "v0.1",
"source": {
"type": "git",
"url": "https://git.itguild.info/apuc/php-cg-select-v2",
"reference": "30c7844d96efaaab35b125db533a064f2254e169"
},
"type": "library",
"autoload": {
"psr-4": {
"Itguild\\PhpCgSelectV2\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"ISC"
],
"authors": [
{
"name": "Kavalar",
"email": "apuc06@mail.ru"
}
],
"description": "Wrapper for CG-Select",
"time": "2023-06-07T22:35:51+00:00"
},
{
"name": "phpoption/phpoption",
"version": "1.9.3",
"source": {
"type": "git",
"url": "https://github.com/schmittjoh/php-option.git",
"reference": "e3fac8b24f56113f7cb96af14958c0dd16330f54"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/schmittjoh/php-option/zipball/e3fac8b24f56113f7cb96af14958c0dd16330f54",
"reference": "e3fac8b24f56113f7cb96af14958c0dd16330f54",
"shasum": ""
},
"require": {
"php": "^7.2.5 || ^8.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.2",
"phpunit/phpunit": "^8.5.39 || ^9.6.20 || ^10.5.28"
},
"type": "library",
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": false
},
"branch-alias": {
"dev-master": "1.9-dev"
}
},
"autoload": {
"psr-4": {
"PhpOption\\": "src/PhpOption/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Johannes M. Schmitt",
"email": "schmittjoh@gmail.com",
"homepage": "https://github.com/schmittjoh"
},
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
}
],
"description": "Option Type for PHP",
"keywords": [
"language",
"option",
"php",
"type"
],
"support": {
"issues": "https://github.com/schmittjoh/php-option/issues",
"source": "https://github.com/schmittjoh/php-option/tree/1.9.3"
},
"funding": [
{
"url": "https://github.com/GrahamCampbell",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/phpoption/phpoption",
"type": "tidelift"
}
],
"time": "2024-07-20T21:41:07+00:00"
},
{
"name": "rakit/validation",
"version": "v1.4.0",
"source": {
"type": "git",
"url": "https://github.com/rakit/validation.git",
"reference": "ff003a35cdf5030a5f2482299f4c93f344a35b29"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/rakit/validation/zipball/ff003a35cdf5030a5f2482299f4c93f344a35b29",
"reference": "ff003a35cdf5030a5f2482299f4c93f344a35b29",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"php": ">=7.0"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.2",
"phpunit/phpunit": "^6.5",
"squizlabs/php_codesniffer": "^3"
},
"type": "library",
"autoload": {
"psr-4": {
"Rakit\\Validation\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Muhammad Syifa",
"email": "emsifa@gmail.com"
}
],
"description": "PHP Laravel like standalone validation library",
"support": {
"issues": "https://github.com/rakit/validation/issues",
"source": "https://github.com/rakit/validation/tree/v1.4.0"
},
"time": "2020-08-27T05:07:01+00:00"
},
{
"name": "symfony/polyfill-ctype",
"version": "v1.30.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-ctype.git",
"reference": "0424dff1c58f028c451efff2045f5d92410bd540"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/0424dff1c58f028c451efff2045f5d92410bd540",
"reference": "0424dff1c58f028c451efff2045f5d92410bd540",
"shasum": ""
},
"require": {
"php": ">=7.1"
},
"provide": {
"ext-ctype": "*"
},
"suggest": {
"ext-ctype": "For best performance"
},
"type": "library",
"extra": {
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"autoload": {
"files": [
"bootstrap.php"
],
"psr-4": {
"Symfony\\Polyfill\\Ctype\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Gert de Pagter",
"email": "BackEndTea@gmail.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for ctype functions",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"ctype",
"polyfill",
"portable"
],
"support": {
"source": "https://github.com/symfony/polyfill-ctype/tree/v1.30.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2024-05-31T15:07:36+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.30.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "fd22ab50000ef01661e2a31d850ebaa297f8e03c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/fd22ab50000ef01661e2a31d850ebaa297f8e03c",
"reference": "fd22ab50000ef01661e2a31d850ebaa297f8e03c",
"shasum": ""
},
"require": {
"php": ">=7.1"
},
"provide": {
"ext-mbstring": "*"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"type": "library",
"extra": {
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"autoload": {
"files": [
"bootstrap.php"
],
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.30.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2024-06-19T12:30:46+00:00"
},
{
"name": "symfony/polyfill-php80",
"version": "v1.30.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php80.git",
"reference": "77fa7995ac1b21ab60769b7323d600a991a90433"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/77fa7995ac1b21ab60769b7323d600a991a90433",
"reference": "77fa7995ac1b21ab60769b7323d600a991a90433",
"shasum": ""
},
"require": {
"php": ">=7.1"
},
"type": "library",
"extra": {
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"autoload": {
"files": [
"bootstrap.php"
],
"psr-4": {
"Symfony\\Polyfill\\Php80\\": ""
},
"classmap": [
"Resources/stubs"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ion Bazan",
"email": "ion.bazan@gmail.com"
},
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"polyfill",
"portable",
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-php80/tree/v1.30.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2024-05-31T15:07:36+00:00"
},
{
"name": "twbs/bootstrap",
"version": "v5.0.2",
"source": {
"type": "git",
"url": "https://github.com/twbs/bootstrap.git",
"reference": "688bce4fa695cc360a0d084e34f029b0c192b223"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twbs/bootstrap/zipball/688bce4fa695cc360a0d084e34f029b0c192b223",
"reference": "688bce4fa695cc360a0d084e34f029b0c192b223",
"shasum": ""
},
"replace": {
"twitter/bootstrap": "self.version"
},
"type": "library",
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Mark Otto",
"email": "markdotto@gmail.com"
},
{
"name": "Jacob Thornton",
"email": "jacobthornton@gmail.com"
}
],
"description": "The most popular front-end framework for developing responsive, mobile first projects on the web.",
"homepage": "https://getbootstrap.com/",
"keywords": [
"JS",
"css",
"framework",
"front-end",
"mobile-first",
"responsive",
"sass",
"web"
],
"support": {
"issues": "https://github.com/twbs/bootstrap/issues",
"source": "https://github.com/twbs/bootstrap/tree/v5.0.2"
},
"time": "2021-06-22T18:29:16+00:00"
},
{
"name": "vlucas/phpdotenv",
"version": "v5.6.1",
"source": {
"type": "git",
"url": "https://github.com/vlucas/phpdotenv.git",
"reference": "a59a13791077fe3d44f90e7133eb68e7d22eaff2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/a59a13791077fe3d44f90e7133eb68e7d22eaff2",
"reference": "a59a13791077fe3d44f90e7133eb68e7d22eaff2",
"shasum": ""
},
"require": {
"ext-pcre": "*",
"graham-campbell/result-type": "^1.1.3",
"php": "^7.2.5 || ^8.0",
"phpoption/phpoption": "^1.9.3",
"symfony/polyfill-ctype": "^1.24",
"symfony/polyfill-mbstring": "^1.24",
"symfony/polyfill-php80": "^1.24"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.2",
"ext-filter": "*",
"phpunit/phpunit": "^8.5.34 || ^9.6.13 || ^10.4.2"
},
"suggest": {
"ext-filter": "Required to use the boolean validator."
},
"type": "library",
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": false
},
"branch-alias": {
"dev-master": "5.6-dev"
}
},
"autoload": {
"psr-4": {
"Dotenv\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
},
{
"name": "Vance Lucas",
"email": "vance@vancelucas.com",
"homepage": "https://github.com/vlucas"
}
],
"description": "Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.",
"keywords": [
"dotenv",
"env",
"environment"
],
"support": {
"issues": "https://github.com/vlucas/phpdotenv/issues",
"source": "https://github.com/vlucas/phpdotenv/tree/v5.6.1"
},
"funding": [
{
"url": "https://github.com/GrahamCampbell",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/vlucas/phpdotenv",
"type": "tidelift"
}
],
"time": "2024-07-20T21:52:34+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.3.0"
}

13
examples/active_form.php Normal file
View File

@ -0,0 +1,13 @@
<?php
require_once __DIR__ . "/vendor/autoload.php";
$form = new \itguild\forms\ActiveForm();
$form->beginForm("/some_action");
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "login", params: [
'class' => "form-control",
'placeholder' => 'Логин',
])
->setLabel("Логин")
->render();
$form->endForm();

14
index.php Normal file
View File

@ -0,0 +1,14 @@
<?php
require_once __DIR__ . "/vendor/autoload.php";
$form = new \itguild\forms\ActiveForm();
$form->beginForm("/some_action");
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "login", params: [
'class' => "form-control",
'placeholder' => 'Логин',
])
->setLabel("Логин")
->render();
$form->endForm();

109
src/ActiveForm.php Executable file
View File

@ -0,0 +1,109 @@
<?php
namespace itguild\forms;
use itguild\forms\builders\ButtonBuilder;
use itguild\forms\builders\CheckBoxBuilder;
use itguild\forms\builders\HiddenBuilder;
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\Hidden;
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
{
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
src/Form.php Executable file
View File

@ -0,0 +1,110 @@
<?php
namespace itguild\forms;
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\Select;
use itguild\forms\inputs\TextInput;
use itguild\forms\inputs\Label;
use itguild\forms\inputs\RadioButton;
use itguild\forms\inputs\TextArea;
use itguild\forms\inputs\Button;
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>";
}
}

89
src/JsonForm.php Executable file
View File

@ -0,0 +1,89 @@
<?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;
use itguild\forms\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
src/builders/Builder.php Executable 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;
}

18
src/builders/ButtonBuilder.php Executable file
View File

@ -0,0 +1,18 @@
<?php
namespace itguild\forms\builders;
use itguild\forms\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, typeInput: $typeInput, paramsArray: $params);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace itguild\forms\builders;
use itguild\forms\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,17 @@
<?php
namespace itguild\forms\builders;
use itguild\forms\inputs\Checkbox;
use itguild\forms\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);
}
}

14
src/builders/LabelBuilder.php Executable file
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);
}
}

21
src/builders/SelectBuilder.php Executable file
View File

@ -0,0 +1,21 @@
<?php
namespace itguild\forms\builders;
use itguild\forms\debug\Debug;
use itguild\forms\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);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace itguild\forms\builders;
use itguild\forms\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);
}
}

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);
}
}

29
src/debug/Debug.php Executable file
View File

@ -0,0 +1,29 @@
<?php
namespace itguild\forms\debug;
class Debug
{
/**
* @param $content
* @return void
*/
public static function prn($content)
{
echo '<pre style="background: lightgray; border: 1px solid black; padding: 2px">';
print_r($content);
echo '</pre>';
}
/**
* @param $content
* @return void
*/
public static function dd($content)
{
echo '<pre style="background: lightgray; border: 1px solid black; padding: 2px">';
print_r($content);
echo '</pre>';
die();
}
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,22 @@
<?php
namespace itguild\forms\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
src/inputs/BaseInput.php Executable file
View File

@ -0,0 +1,66 @@
<?php
namespace itguild\forms\inputs;
use itguild\forms\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;
}
}

60
src/inputs/Button.php Executable file
View File

@ -0,0 +1,60 @@
<?php
namespace itguild\forms\inputs;
use itguild\forms\templates\Simple\SimpleTemplate;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
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();
}
}

61
src/inputs/Checkbox.php Executable file
View File

@ -0,0 +1,61 @@
<?php
namespace itguild\forms\inputs;
use itguild\forms\templates\Simple\SimpleTemplate;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
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
src/inputs/Hidden.php Normal file
View File

@ -0,0 +1,60 @@
<?php
namespace itguild\forms\inputs;
use itguild\forms\templates\Simple\SimpleTemplate;
use itguild\forms\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();
}
}

59
src/inputs/Label.php Executable file
View File

@ -0,0 +1,59 @@
<?php
namespace itguild\forms\inputs;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
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
src/inputs/RadioButton.php Executable file
View File

@ -0,0 +1,63 @@
<?php
namespace itguild\forms\inputs;
use itguild\forms\templates\Simple\SimpleTemplate;
use itguild\forms\traits\CreateOption;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
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;
}
}

84
src/inputs/Select.php Executable file
View File

@ -0,0 +1,84 @@
<?php
namespace itguild\forms\inputs;
use itguild\forms\debug\Debug;
use itguild\forms\templates\Template;
use itguild\forms\traits\CreateOption;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
use itguild\forms\templates\Simple\SimpleTemplate;
use itguild\forms\templates\bootstrap5\Bootstrap5Template;
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;
}
}

62
src/inputs/TextArea.php Executable file
View File

@ -0,0 +1,62 @@
<?php
namespace itguild\forms\inputs;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
use itguild\forms\templates\bootstrap5\Bootstrap5Template;
use itguild\forms\templates\Simple\SimpleTemplate;
use itguild\forms\templates\Template;
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();
}
}

62
src/inputs/TextInput.php Executable file
View File

@ -0,0 +1,62 @@
<?php
namespace itguild\forms\inputs;
use itguild\forms\debug\Debug;
use itguild\forms\inputs\BaseInput;
use itguild\forms\templates\bootstrap5\Bootstrap5Template;
use itguild\forms\templates\Simple\SimpleTemplate;
use itguild\forms\templates\Template;
use itguild\forms\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();
}
}

39
src/mappers/JsonInputMapper.php Executable file
View File

@ -0,0 +1,39 @@
<?php
namespace itguild\forms\mappers;
use itguild\forms\builders\Builder;
use itguild\forms\builders\ButtonBuilder;
use itguild\forms\builders\CheckBoxBuilder;
use itguild\forms\builders\HiddenBuilder;
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,
"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,17 @@
<?php
namespace itguild\forms\templates\Simple;
use itguild\forms\templates\Template;
class SimpleTemplate extends Template
{
/**
* @return string
*/
public static function getInputTemplate(): string
{
return "<div class='form-group'>{label}<br>{input}</div>";
}
}

11
src/templates/Template.php Executable file
View File

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

View File

@ -0,0 +1,37 @@
<?php
namespace itguild\forms\templates\bootstrap5;
use itguild\forms\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
src/traits/CreateOption.php Executable file
View File

@ -0,0 +1,21 @@
<?php
namespace itguild\forms\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;
}
}

26
src/traits/CreateParams.php Executable file
View File

@ -0,0 +1,26 @@
<?php
namespace itguild\forms\traits;
use itguild\forms\debug\Debug;
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;
}
}