Last active
March 1, 2018 13:41
-
-
Save cherifGsoul/bdfa153d3307b82a55d020df5e1b677e to your computer and use it in GitHub Desktop.
Value Object implementation pattern
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
const emailAddress = (address) => { | |
let _address = address; | |
/** | |
* @todo put validation | |
**/ | |
let email = Object.assign({}, { | |
valueOf() { | |
return _address; | |
}, | |
address() { | |
return _address; | |
}, | |
toString() { | |
return _address; | |
}, | |
equals(other) { | |
return this.address() === other.address(); | |
} | |
}) | |
return Object.freeze(email); | |
} |
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 EmailAddress | |
{ | |
/** | |
* Undocumented variable | |
* | |
* @var [type] | |
*/ | |
private $address; | |
/** | |
* Undocumented function | |
* | |
* @param [type] $address | |
*/ | |
private function __construct($address) | |
{ | |
$this->setAddress($address); | |
} | |
/** | |
* Undocumented function | |
* | |
* @param [type] $address | |
* @return void | |
*/ | |
public static function fromString($address) | |
{ | |
return new self($address); | |
} | |
/** | |
* Undocumented function | |
* | |
* @return void | |
*/ | |
public function address() | |
{ | |
return $this->address; | |
} | |
/** | |
* Undocumented function | |
* | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return $this->address(); | |
} | |
/** | |
* Undocumented function | |
* | |
* @param [type] $address | |
* @return void | |
*/ | |
private function setAddress($address) | |
{ | |
if (false == Assertion::email($email)) { | |
throw new \Exception('Invalid email address'); | |
} | |
$this->address = $address; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment