Last active
August 29, 2015 14:21
-
-
Save fesor/9011f6b0f56237450de4 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
class Translation { | |
public static function fromDTO($dto) { | |
$translation = new static($dto->locale); | |
$translation->address = $dto->address; | |
// ... | |
return $translation; | |
} | |
public static function createTranslationSetFromDTO($dto) { | |
if (!is_array($dto)) return []; | |
return array_map(function ($translationDTO) { | |
return static::fromDTO($translationDTO); | |
}, $dto); | |
} | |
} |
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 DTO { | |
private $data; | |
public function __construct($data) { | |
$this->data = $data; | |
} | |
public function __isset($name) { | |
return isset($this->data->$name)); | |
} | |
public function __get($name) { | |
if (!isset($this->data->$name)) return null; | |
if (is_object($this->data->$name)) { | |
return new static($this->data->$name); | |
} | |
if (!is_array($this->data->$name) { | |
return $this->data->$name; | |
} | |
$result = []; | |
foreach($this->data->$name as $item) { | |
if (is_object($this->data->$name)) { | |
$result[] = new static($this->data->$name); | |
} | |
} | |
return $result; | |
} | |
} |
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 | |
$advert | |
->setBathroom($dto->bathroom) | |
->setBedroom($dto->bedroom) | |
->setZipCode($dto->zipCode) | |
->setPrice(Money::fromDTO($dto->price)) | |
->setPoint(Point::fromDTO($dto->location)) | |
// создает ArrayCollection и заменяет | |
->translate(Translation::createTranslationSetFromDTO($dto->translations)); | |
$errors = $this->validator->validate($advert); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment