Skip to content

Instantly share code, notes, and snippets.

@fesor
Last active August 29, 2015 14:21
Show Gist options
  • Save fesor/9011f6b0f56237450de4 to your computer and use it in GitHub Desktop.
Save fesor/9011f6b0f56237450de4 to your computer and use it in GitHub Desktop.
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);
}
}
<?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;
}
}
<?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