Skip to content

Instantly share code, notes, and snippets.

@vertisan
Created April 2, 2019 21:39
Show Gist options
  • Save vertisan/47b3a089affbde931b8f51e4f4744054 to your computer and use it in GitHub Desktop.
Save vertisan/47b3a089affbde931b8f51e4f4744054 to your computer and use it in GitHub Desktop.
<?php
class FoobarData
{
private $name;
/**
* Create DTO, optionally extracting data from a model.
*
* @param Foobar|null $foobar
*/
public function __construct(?Foobar $foobar = null)
{
if ($foobar instanceof Foobar) {
$this->extract($foobar);
}
}
/**
* Fill model with data from the DTO.
*
* @param Foobar $foobar
* @return Foobar
*/
public function fill(Foobar $foobar): Foobar
{
$foobar
->setName($this->getName())
;
return $foobar;
}
/**
* Extract data from model into the DTO.
*
* @param Foobar $foobar
* @return self
*/
public function extract(Foobar $foobar): self
{
$this->setName($foobar->getName());
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment