-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Container::getValues($obj) mapping to PHP 8 constructor
- Loading branch information
Showing
3 changed files
with
116 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,103 @@ | ||
<?php | ||
|
||
/** | ||
* @phpVersion 8.0 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Forms\Form; | ||
use Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
class FormDataConstruct | ||
{ | ||
public function __construct( | ||
public string $title, | ||
public FormFirstLevelConstruct $first, | ||
...$extra, | ||
) { | ||
} | ||
} | ||
|
||
|
||
class FormFirstLevelConstruct | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public ?FormSecondLevelConstruct $second = null, | ||
public int|null $age = null, | ||
) { | ||
} | ||
} | ||
|
||
|
||
class FormSecondLevelConstruct | ||
{ | ||
public function __construct( | ||
public string $city, | ||
) { | ||
} | ||
} | ||
|
||
|
||
function hydrate(string $class, array $data) | ||
{ | ||
return new $class(...$data); | ||
} | ||
|
||
|
||
function createForm(): Form | ||
{ | ||
$form = new Form; | ||
$form->addText('title'); | ||
|
||
$first = $form->addContainer('first'); | ||
$first->addText('name'); | ||
$first->addInteger('age'); | ||
|
||
$second = $first->addContainer('second'); | ||
$second->addText('city'); | ||
return $form; | ||
} | ||
|
||
|
||
test('getValues(...arguments...)', function () { | ||
$form = createForm(); | ||
|
||
$form->setValues([ | ||
'title' => 'new1', | ||
'first' => [ | ||
'name' => 'new2', | ||
], | ||
]); | ||
|
||
Assert::equal(new FormDataConstruct( | ||
title: 'new1', | ||
first: new FormFirstLevelConstruct( | ||
name: 'new2', | ||
age: null, | ||
second: new FormSecondLevelConstruct( | ||
city: '', | ||
), | ||
), | ||
), $form->getValues(FormDataConstruct::class)); | ||
|
||
$form->setMappedType(FormDataConstruct::class); | ||
$form['first']->setMappedType(FormFirstLevelConstruct::class); | ||
$form['first-second']->setMappedType(FormSecondLevelConstruct::class); | ||
|
||
Assert::equal(new FormDataConstruct( | ||
title: 'new1', | ||
first: new FormFirstLevelConstruct( | ||
name: 'new2', | ||
age: null, | ||
second: new FormSecondLevelConstruct( | ||
city: '', | ||
), | ||
), | ||
), $form->getValues()); | ||
}); |