Last active
May 30, 2017 10:06
-
-
Save kirkbushell/aa60d05a39dba9288e25 to your computer and use it in GitHub Desktop.
Value object usage within Laravel Eloquent example
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 | |
// In model: Ensures values are consistent | |
class User extends Eloquent { | |
public function setEmailAttribute(Email $email) { | |
$this->attributes['email'] = $email; | |
} | |
} | |
// Value object | |
class Email { | |
public $email; | |
public function __construct($email) { | |
Assert::email($email); | |
$this->email = $email; | |
} | |
public function __toString() { | |
return $this->email; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx for the example. But since Email is a value object, the $email propriety needs to be private