Created
April 2, 2019 21:39
-
-
Save vertisan/47b3a089affbde931b8f51e4f4744054 to your computer and use it in GitHub Desktop.
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
<?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