Skip to content

Instantly share code, notes, and snippets.

@mcsee
Last active December 24, 2023 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mcsee/91a2d630101ba6137f64195e76c1b266 to your computer and use it in GitHub Desktop.
Save mcsee/91a2d630101ba6137f64195e76c1b266 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 function __construct($userName, $friends, UserFeed $feed) {
$this->assertUsernameIsValid($userName);
$this->assertNoFriendDuplicates($friends);
$this->userName = $userName;
$this->friends = $friends;
$this->feed = $feed;
$this->assertNoFriendOfMyself($friends);
}
// Lots of protocol
}
// If you need to transfer to an external system you need
// to duplicate (and maintain) the structure
final class SocialNetworkProfileDTO {
private $userName; // duplicated to be synchronized
private $friends; // duplicated to be synchronized
private $feed; // duplicated to be synchronized
public function __construct() {
// Empty constructor without validations
}
// No protocol, just serializers
}
// If you need to transfer to an external system you create an anemic DTO
$janesProfileToTransfer = new SocialNetworkProfileDTO();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment