Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added data mapper #166

Closed
wants to merge 0 commits into from

Conversation

@dg
Copy link
Member

commented Aug 25, 2017

  • bug fix? no
  • new feature? yes
  • BC break? no

Added built-in support for mapping form values to object and vice versa.

Example

Form is representer by class ArticleFormData:

final class ArticleFormData
{
	use SmartObject;

	/** @var string */
	public $title;

	/** @var string */
	public $body;

	/** @var bool */
	public $draft = false;

	/** @var string */
	public $lang = 'en';
}

We setup mapping:

	function createComponentArticleForm()
	{
		$form = $this->formFactory->create();
		$form->setMappedType(ArticleFormData::class); // <-- 
		$form->addText('title')
			->setRequired();
		$form->addTextArea('body')
			->setRequired();
		$form->addCheckbox('draft');

And now are values returned as ArticleFormData object:

	function articleFormSubmitted(Form $form, ArticleFormData $data)
	{
		$this->facade->updateArticle($data, $this->getUser()->getId());
		$this->redirect('Go:west');
	}
}```

@dg dg changed the title added data mapper added data mapper [WIP] Aug 25, 2017

@dg dg force-pushed the dg:pull-mapper branch 2 times, most recently from 3903d7b to bbd247e Aug 25, 2017

foreach ($this->getComponents() as $name => $control) {
if ($control instanceof IControl && !$control->isOmitted()) {
$values[$name] = $control->getValue();
} elseif ($control instanceof self) {
if ($mappedClass && $control->mappedClass === self::DEFAULT_MAPPED_CLASS) {

This comment has been minimized.

Copy link
@milo

milo Aug 25, 2017

Member

For ArrayHash only to keep the compatibility? Or is there another reason?

This comment has been minimized.

Copy link
@dg

dg Aug 25, 2017

Author Member

I want to keep current functionality.

$this->setValues($values, $erase);
$this->setValues($data, $erase);
} elseif (is_object($data)) {
$this->mappedClass = get_class($data);

This comment has been minimized.

Copy link
@JanTvrdik

JanTvrdik Sep 9, 2017

Contributor

This seems like odd side-effect. Shouldn't the mappedClass be changed more explicitly?

if ($control instanceof IControl) {
$control->setValue($data[$name]);
} else {
$control->setValues($data[$name] ?: [], $erase);

This comment has been minimized.

Copy link
@JanTvrdik

JanTvrdik Sep 9, 2017

Contributor

The choice to use ?: instead of ?? is intentional?

} elseif ($erase) {
} elseif ($erase) {
if ($control instanceof IControl) {

This comment has been minimized.

Copy link
@JanTvrdik

JanTvrdik Sep 9, 2017

Contributor

I don't like those conditions. How about we split $components array into $controls, $containers and $dataKeys.

*/
public function getValues(bool $asArray = false)
{
$values = $asArray ? [] : new Nette\Utils\ArrayHash;
$mappedClass = $asArray ? null : $this->mappedClass;
$mapper = $this->mapper ?: new Mapper;

This comment has been minimized.

Copy link
@JanTvrdik

JanTvrdik Sep 9, 2017

Contributor

The choice to use ?: instead of ?? is intentional?

/** @var string|null */
private $mappedClass = self::DEFAULT_MAPPED_CLASS;
/** @var IMapper */

This comment has been minimized.

Copy link
@JanTvrdik

JanTvrdik Sep 9, 2017

Contributor

IMapper|null

@dg dg force-pushed the nette:master branch from 5f5aec3 to 9ea7e2e Feb 28, 2018

@dg dg force-pushed the nette:master branch from 5da3697 to 20559e9 Mar 20, 2018

@dg dg force-pushed the nette:master branch from 81a06eb to db99d41 May 10, 2018

@dg dg force-pushed the nette:master branch from 15ef159 to 1da7145 Jun 7, 2018

@dg dg force-pushed the nette:master branch from 95b9cfc to 177fbe3 Jun 28, 2018

@dg dg force-pushed the nette:master branch from 43a2ab1 to c2d4d93 Sep 13, 2018

@dg dg force-pushed the nette:master branch from 2da4206 to 5e0e910 Oct 25, 2018

@dg dg force-pushed the nette:master branch from 84b75ea to 0208ece Nov 23, 2018

@dg dg closed this Feb 8, 2019

@dg dg force-pushed the dg:pull-mapper branch from bbd247e to a95b430 Feb 8, 2019

@dg dg reopened this Feb 8, 2019

@dg dg closed this Feb 8, 2019

@dg dg deleted the dg:pull-mapper branch Feb 8, 2019

@dg dg changed the title added data mapper [WIP] added data mapper Apr 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.