-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added DataClassGenerator & tag {formClassPrint}
- Loading branch information
Showing
5 changed files
with
236 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (https://nette.org) | ||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\Forms\Rendering; | ||
|
||
use Latte; | ||
use Nette\Forms\Container; | ||
use Nette\Forms\Controls; | ||
use Nette\Forms\Form; | ||
|
||
|
||
/** | ||
* Generates blueprint of form data class. | ||
*/ | ||
final class DataClassGenerator | ||
{ | ||
/** @var string */ | ||
public $classNameSuffix = 'FormData'; | ||
|
||
/** @var string */ | ||
public $baseClassName; | ||
|
||
/** @var bool */ | ||
public $propertyPromotion = false; | ||
|
||
/** @var bool */ | ||
public $useSmartObject = true; | ||
|
||
|
||
public function generateCode(Form $form): string | ||
{ | ||
$baseClassName = $this->baseClassName ?? preg_replace('~Form$~', '', ucwords((string) $form->getName())); | ||
return $this->generateClass($form, $baseClassName); | ||
} | ||
|
||
|
||
private function generateClass(Container $form, string $baseClass): string | ||
{ | ||
$code = ''; | ||
$props = []; | ||
foreach ($form->getComponents() as $name => $input) { | ||
if ($input instanceof Controls\BaseControl && $input->isOmitted()) { | ||
continue; | ||
} elseif ($input instanceof Controls\Checkbox) { | ||
$type = 'bool'; | ||
} elseif ($input instanceof Controls\MultiChoiceControl) { | ||
$type = 'array'; | ||
} elseif ($input instanceof Controls\ChoiceControl) { | ||
$type = 'string|int'; | ||
if (!$input->isRequired()) { | ||
$type .= '|null'; | ||
} | ||
} elseif ($input instanceof Controls\HiddenField || $input instanceof Controls\TextBase) { | ||
$type = 'string'; | ||
foreach ($input->getRules() as $rule) { | ||
if ($rule->validator === Form::INTEGER) { | ||
$type = 'int'; | ||
break; | ||
} | ||
} | ||
if (!$input->isRequired()) { | ||
$type = '?' . $type; | ||
} | ||
} elseif ($input instanceof Controls\UploadControl) { | ||
$type = '\Nette\Http\FileUpload'; | ||
if (!$input->isRequired()) { | ||
$type = '?' . $type; | ||
} | ||
} elseif ($input instanceof Container) { | ||
$type = $baseClass . ucwords($name); | ||
$code .= self::generateClass($input, $type); | ||
$type .= $this->classNameSuffix; | ||
} else { | ||
$type = ''; | ||
} | ||
|
||
$props[] = 'public ' . ($type ? $type . ' ' : '') . '$' . $name; | ||
} | ||
|
||
$class = $baseClass . $this->classNameSuffix; | ||
return "class $class\n" | ||
. "{\n" | ||
. ($this->useSmartObject ? "\tuse \\Nette\\SmartObject;\n\n" : '') | ||
. ($this->propertyPromotion | ||
? "\tpublic function __construct(\n" | ||
. ($props ? "\t\t" . implode(",\n\t\t", $props) . ",\n" : '') | ||
. "\t) {\n\t}\n" | ||
: ($props ? "\t" . implode(";\n\t", $props) . ";\n" : '') | ||
) | ||
. "}\n\n" . $code; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Forms\Form; | ||
use Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
$form = new Form('signForm'); | ||
$form->addText('name')->setRequired(); | ||
|
||
ob_start(); | ||
Nette\Bridges\FormsLatte\Runtime::renderFormClassPrint($form); | ||
$res = ob_get_clean(); | ||
|
||
Assert::match( | ||
'%A%class SignFormData | ||
{ | ||
use \Nette\SmartObject; | ||
public string $name; | ||
} | ||
%A%', | ||
$res | ||
); | ||
|
||
if (PHP_VERSION_ID >= 80000) { | ||
Assert::match( | ||
'%A%class SignFormData | ||
{ | ||
use \Nette\SmartObject; | ||
public function __construct( | ||
public string $name, | ||
) { | ||
} | ||
} | ||
%A%', | ||
$res | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Forms\Form; | ||
use Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
$form = new Form('signForm'); | ||
$form->addText('name')->setRequired(); | ||
$form->addInteger('age'); | ||
$form->addContainer('cont') | ||
->addText('name'); | ||
$form->addHidden('id'); | ||
$form->addCheckbox('agree'); | ||
$form->addSubmit('submit', 'Send'); | ||
|
||
$generator = new Nette\Forms\Rendering\DataClassGenerator; | ||
$res = $generator->generateCode($form); | ||
|
||
Assert::match( | ||
'class SignFormData | ||
{ | ||
use \Nette\SmartObject; | ||
public string $name; | ||
public ?int $age; | ||
public SignContFormData $cont; | ||
public ?string $id; | ||
public bool $agree; | ||
} | ||
class SignContFormData | ||
{ | ||
use \Nette\SmartObject; | ||
public ?string $name; | ||
} | ||
', | ||
$res | ||
); | ||
|
||
$generator->propertyPromotion = true; | ||
$generator->useSmartObject = false; | ||
$res = $generator->generateCode($form); | ||
|
||
Assert::match( | ||
'class SignFormData | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public ?int $age, | ||
public SignContFormData $cont, | ||
public ?string $id, | ||
public bool $agree, | ||
) { | ||
} | ||
} | ||
class SignContFormData | ||
{ | ||
public function __construct( | ||
public ?string $name, | ||
) { | ||
} | ||
} | ||
', | ||
$res | ||
); |