Created
April 8, 2015 08:38
-
-
Save mageekguy/a0df0ac63dcda686da0d to your computer and use it in GitHub Desktop.
the-4-rules-of-east-oriented-code-rule-4
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 | |
abstract class data | |
{ | |
private | |
$value | |
; | |
function __construct($value) | |
{ | |
if (! is_string($value)) | |
{ | |
throw new \domainException('Value should be a string'); | |
} | |
$this->value = $value; | |
} | |
function __toString() | |
{ | |
return $this->value; | |
} | |
} | |
final class street extends data | |
{ | |
} | |
final class postalCode extends data | |
{ | |
} | |
final class apartment extends data | |
{ | |
} | |
final class city extends data | |
{ | |
} | |
final class province extends data | |
{ | |
} | |
class address | |
{ | |
private | |
$street, | |
$postalCode, | |
$apartment, | |
$city, | |
$province, | |
$protectPrivacy | |
; | |
function __construct(apartment $apartment, street $street, city $city, province $province, postalCode $postalCode) | |
{ | |
$this->apartment = $apartment; | |
$this->street = $street; | |
$this->city = $city; | |
$this->province = $province; | |
$this->postalCode = $postalCode; | |
} | |
function enablePrivacyProtection() | |
{ | |
$address = clone $this; | |
$address->protectPrivacy = true; | |
return $address; | |
} | |
function streetIsAskedByTemplate(template $template) | |
{ | |
if (! $this->protectPrivacy) | |
{ | |
$template->addressHasStreet($this->street); | |
} | |
return $this; | |
} | |
function apartmentIsAskedByTemplate(template $template) | |
{ | |
if (! $this->protectPrivacy) | |
{ | |
$template->addressHasStreet($this->street); | |
} | |
return $this; | |
} | |
function postalCodeIsAskedByTemplate(template $template) | |
{ | |
if (! $this->protectPrivacy) | |
{ | |
$template->addressHasPostalCode($this->postalCode); | |
} | |
return $this; | |
} | |
function cityIsAskedByTemplate(template $template) | |
{ | |
$template->addressHasCity($this->city); | |
return $this; | |
} | |
function provinceIsAskedByTemplate(template $template) | |
{ | |
$template->addressHasProvince($this->province); | |
return $this; | |
} | |
} | |
class template | |
{ | |
private | |
$street, | |
$apartment, | |
$postalCode, | |
$city, | |
$province | |
; | |
function addressHasStreet(street $street) | |
{ | |
$this->street = $street; | |
return $this; | |
} | |
function addressHasApartment(apartment $apartment) | |
{ | |
$this->apartment = $apartment; | |
return $this; | |
} | |
function addressHasPostalCode(postalCode $postalCode) | |
{ | |
$this->postalCode = $postalCode; | |
return $this; | |
} | |
function addressHasCity(city $city) | |
{ | |
$this->city = $city; | |
return $this; | |
} | |
function addressHasProvince(province $province) | |
{ | |
$this->province = $province; | |
return $this; | |
} | |
function addressIs(address $address) | |
{ | |
$template = clone $this; | |
$template->street = ''; | |
$template->apartment = ''; | |
$template->postalCode = ''; | |
$template->city = ''; | |
$template->province = ''; | |
$address | |
->streetIsAskedByTemplate($template) | |
->apartmentIsAskedByTemplate($template) | |
->postalCodeIsAskedByTemplate($template) | |
->cityIsAskedByTemplate($template) | |
->provinceIsAskedByTemplate($template) | |
; | |
$string = ''; | |
if ($template->street) | |
{ | |
$string .= $template->street . PHP_EOL; | |
} | |
if ($template->apartment) | |
{ | |
$string .= $template->apartment . PHP_EOL; | |
} | |
if ($template->city) | |
{ | |
$string .= $template->city . PHP_EOL; | |
} | |
if ($template->province && $template->postalCode) | |
{ | |
$string .= $template->province . ' ' . $template->postalCode . PHP_EOL; | |
} | |
echo $string; | |
return $this; | |
} | |
} | |
(new template) | |
->addressIs(new address(new apartment('6'), new street('123 Main St.'), new city('Arlington'), new province('VA'), new postalCode('22222'))) | |
->addressIs( | |
(new address(new apartment('6'), new street('123 Main St.'), new city('Arlington'), new province('VA'), new postalCode('22222'))) | |
->enablePrivacyProtection() | |
) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment