Skip to content

Instantly share code, notes, and snippets.

@cherifGsoul
Last active March 1, 2018 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cherifGsoul/bdfa153d3307b82a55d020df5e1b677e to your computer and use it in GitHub Desktop.
Save cherifGsoul/bdfa153d3307b82a55d020df5e1b677e to your computer and use it in GitHub Desktop.
Value Object implementation pattern
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);
}
<?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