commit 3675ad134c6413ca1a6ea1c8a8cd12e674fce2e0 Author: Kavalar Date: Thu Jul 25 12:33:47 2024 +0300 first diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..331c58f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +vendor \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..c703dab --- /dev/null +++ b/composer.json @@ -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" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..a175496 --- /dev/null +++ b/composer.lock @@ -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" +} diff --git a/examples/active_form.php b/examples/active_form.php new file mode 100644 index 0000000..b163686 --- /dev/null +++ b/examples/active_form.php @@ -0,0 +1,13 @@ +beginForm("/some_action"); +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "login", params: [ + 'class' => "form-control", + 'placeholder' => 'Логин', +]) + ->setLabel("Логин") + ->render(); + +$form->endForm(); \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..26f5bb6 --- /dev/null +++ b/index.php @@ -0,0 +1,14 @@ +beginForm("/some_action"); +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "login", params: [ + 'class' => "form-control", + 'placeholder' => 'Логин', + ]) +->setLabel("Логин") +->render(); + +$form->endForm(); \ No newline at end of file diff --git a/src/ActiveForm.php b/src/ActiveForm.php new file mode 100755 index 0000000..e508ae0 --- /dev/null +++ b/src/ActiveForm.php @@ -0,0 +1,109 @@ +"; + } + + /** + * @return void + */ + public function endForm(): void + { + echo ""; + + } + + 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(); + } + +} \ No newline at end of file diff --git a/src/Form.php b/src/Form.php new file mode 100755 index 0000000..4634eb3 --- /dev/null +++ b/src/Form.php @@ -0,0 +1,110 @@ +"; + } + + /** + * @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 ""; + + } +} diff --git a/src/JsonForm.php b/src/JsonForm.php new file mode 100755 index 0000000..2e80e77 --- /dev/null +++ b/src/JsonForm.php @@ -0,0 +1,89 @@ +jsonData = json_decode($json, true); + + + } + + public function beginForm($params = []): string + { + $paramsString = $this->createParams($params); + return "
"; + } + + public function endForm(): string + { + return "
"; + + } + + 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; + } + +} diff --git a/src/builders/Builder.php b/src/builders/Builder.php new file mode 100755 index 0000000..a74ef88 --- /dev/null +++ b/src/builders/Builder.php @@ -0,0 +1,12 @@ +'; + print_r($content); + echo ''; + } + + /** + * @param $content + * @return void + */ + public static function dd($content) + { + echo '
';
+        print_r($content);
+        echo '
'; + die(); + } +} \ No newline at end of file diff --git a/src/exceptions/CreateFormException.php b/src/exceptions/CreateFormException.php new file mode 100644 index 0000000..6952c3d --- /dev/null +++ b/src/exceptions/CreateFormException.php @@ -0,0 +1,11 @@ +message = "Create form error: " . $msg; + } +} \ No newline at end of file diff --git a/src/exceptions/FieldTypeNotFound.php b/src/exceptions/FieldTypeNotFound.php new file mode 100644 index 0000000..7004684 --- /dev/null +++ b/src/exceptions/FieldTypeNotFound.php @@ -0,0 +1,13 @@ +message = "Тип поля $type не найден"; + } + +} \ No newline at end of file diff --git a/src/exceptions/FormNotFoundException.php b/src/exceptions/FormNotFoundException.php new file mode 100644 index 0000000..c1649f3 --- /dev/null +++ b/src/exceptions/FormNotFoundException.php @@ -0,0 +1,11 @@ + message = "Форма $formId не найдена"; + } +} \ No newline at end of file diff --git a/src/exceptions/FormResultNotFoundException.php b/src/exceptions/FormResultNotFoundException.php new file mode 100644 index 0000000..0cf5cff --- /dev/null +++ b/src/exceptions/FormResultNotFoundException.php @@ -0,0 +1,11 @@ + message = "Ответ формы $id не найдена"; + } +} \ No newline at end of file diff --git a/src/helpers/TranslitorHelper.php b/src/helpers/TranslitorHelper.php new file mode 100644 index 0000000..8190e71 --- /dev/null +++ b/src/helpers/TranslitorHelper.php @@ -0,0 +1,22 @@ +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 = ""; + } + else { + $this->labelString= $this->label->create()->fetch(); + } + } + } + + /** + * @param $template + * @return $this + */ + public function setTemplate($template): self + { + $this->inputTemplate = new $template(); + + return $this; + } + + +} \ No newline at end of file diff --git a/src/inputs/Button.php b/src/inputs/Button.php new file mode 100755 index 0000000..1f376b8 --- /dev/null +++ b/src/inputs/Button.php @@ -0,0 +1,60 @@ +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 = ""; + + $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(); + } + +} \ No newline at end of file diff --git a/src/inputs/Checkbox.php b/src/inputs/Checkbox.php new file mode 100755 index 0000000..c93c0bd --- /dev/null +++ b/src/inputs/Checkbox.php @@ -0,0 +1,61 @@ +name = $name; + $this->value = $value; + $this->paramsArray = $paramsArray; + $this->inputTemplate = new SimpleTemplate(); + } + + /** + * @return $this + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $checkBox = ""; + $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(); + + } +} \ No newline at end of file diff --git a/src/inputs/Hidden.php b/src/inputs/Hidden.php new file mode 100644 index 0000000..2bf8a78 --- /dev/null +++ b/src/inputs/Hidden.php @@ -0,0 +1,60 @@ +name = $name; + $this->value = $value; + $this->paramsArray = $paramsArray; + $this->inputTemplate = new SimpleTemplate(); + } + + /** + * @return $this + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $hidden = ""; + $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(); + + } +} \ No newline at end of file diff --git a/src/inputs/Label.php b/src/inputs/Label.php new file mode 100755 index 0000000..1702ac6 --- /dev/null +++ b/src/inputs/Label.php @@ -0,0 +1,59 @@ +title = $title; + $this->paramsArray = $paramsArray; + } + + /** + * @return $this + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $this->html = ""; + + + 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(); + } + +} \ No newline at end of file diff --git a/src/inputs/RadioButton.php b/src/inputs/RadioButton.php new file mode 100755 index 0000000..5bf3abb --- /dev/null +++ b/src/inputs/RadioButton.php @@ -0,0 +1,63 @@ +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 = "$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; + } +} \ No newline at end of file diff --git a/src/inputs/Select.php b/src/inputs/Select.php new file mode 100755 index 0000000..06d2f35 --- /dev/null +++ b/src/inputs/Select.php @@ -0,0 +1,84 @@ +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 = ""; + + $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; + } + + +} \ No newline at end of file diff --git a/src/inputs/TextArea.php b/src/inputs/TextArea.php new file mode 100755 index 0000000..8ee435f --- /dev/null +++ b/src/inputs/TextArea.php @@ -0,0 +1,62 @@ +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 = ""; + + $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(); + } + +} \ No newline at end of file diff --git a/src/inputs/TextInput.php b/src/inputs/TextInput.php new file mode 100755 index 0000000..44ba12f --- /dev/null +++ b/src/inputs/TextInput.php @@ -0,0 +1,62 @@ +name = $name; + $this->paramsArray = $paramsArray; + $this->inputTemplate = new SimpleTemplate(); + } + + /** + * @return self + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $label = ""; + $input = ""; + + $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(); + + } + +} \ No newline at end of file diff --git a/src/mappers/JsonInputMapper.php b/src/mappers/JsonInputMapper.php new file mode 100755 index 0000000..38316a5 --- /dev/null +++ b/src/mappers/JsonInputMapper.php @@ -0,0 +1,39 @@ + 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; + } +} \ No newline at end of file diff --git a/src/templates/Simple/SimpleTemplate.php b/src/templates/Simple/SimpleTemplate.php new file mode 100755 index 0000000..6f3ca28 --- /dev/null +++ b/src/templates/Simple/SimpleTemplate.php @@ -0,0 +1,17 @@ +{label}
{input}"; + } +} \ No newline at end of file diff --git a/src/templates/Template.php b/src/templates/Template.php new file mode 100755 index 0000000..3a64f52 --- /dev/null +++ b/src/templates/Template.php @@ -0,0 +1,11 @@ +{label}{input}"; + } + public static function getTextAreaTemplate(): string + { + return "
{label}{textarea}
"; + } + public static function getSelectTemplate(): string + { + return "
{label}{select}
"; + } + public static function getRadioTemplate(): string + { + return "
{label}{radio}
"; + } + public static function getCheckBoxTemplate(): string + { + return "
{label}{checkbox}
"; + } + public static function getButtonTemplate(): string + { + return "
{label}{button}
"; + } +} \ No newline at end of file diff --git a/src/traits/CreateOption.php b/src/traits/CreateOption.php new file mode 100755 index 0000000..0ba5071 --- /dev/null +++ b/src/traits/CreateOption.php @@ -0,0 +1,21 @@ + $title){ + $selected = (int)$value === $val ? "selected" : ""; + $optionsString .= ""; + } + return $optionsString; + } +} \ No newline at end of file diff --git a/src/traits/CreateParams.php b/src/traits/CreateParams.php new file mode 100755 index 0000000..e19cb0e --- /dev/null +++ b/src/traits/CreateParams.php @@ -0,0 +1,26 @@ + $param){ + if(is_string($param)){ + $paramsString .= $key . "='" . $param . "'"; + } + } + + return $paramsString; + } + +} \ No newline at end of file