Skip to content

Instantly share code, notes, and snippets.

@ramiror
Forked from mcsee/dto.php
Last active December 2, 2020 21:38
Show Gist options
  • Select an option

  • Save ramiror/69264236c54288d27edfa57335c54ad6 to your computer and use it in GitHub Desktop.

Select an option

Save ramiror/69264236c54288d27edfa57335c54ad6 to your computer and use it in GitHub Desktop.
<?
final class SocialNetworkProfile {
private $userName;
private $friends; // friends is a reference to a large collection
private $feed; // feed references the whole user feed
public __construct($userName , $friends, UserFeed $feed) {
$this->assertUsernameIsValid($userName);
$this->assertNoFriendDuplicates($friends);
$this->userName = $userName;
$this->friends = $friends;
$this->feed = $feed
$this->assertNoFriendofMylsef($friends);
}
// Lots of protocol
}
// If we need to transfer to an External system we need
// to duplicate (and maintain) the structure
final class SocialNetworkProfileDTO {
private $userName;
public __construct() {
// Empty construct without validations
}
// No protocol, just serializers
// If we need to transfer to an External system we create an anemic DTO
$janesProfileToTransfer = new SocialNetworkProfileDTO('jane');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment