Last active
August 29, 2015 14:13
-
-
Save mageekguy/03fe32a94fd679e7f55d to your computer and use it in GitHub Desktop.
East oriented body
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 | |
final class celsius | |
{ | |
private | |
$value | |
; | |
function __construct($value) | |
{ | |
$this->value = $value; | |
} | |
function __toString() | |
{ | |
return (string) $this->value; | |
} | |
function __get($property) | |
{ | |
return $property == 'asInteger' ? (int) $this->value : null; | |
} | |
} | |
abstract class part | |
{ | |
private | |
$alive | |
; | |
function __construct() | |
{ | |
$this->alive = true; | |
} | |
function bodyIsDead() | |
{ | |
$this->alive = false; | |
return $this; | |
} | |
function bodyCold(celsius $celsius) | |
{ | |
return $this; | |
} | |
function bodyHot(celsius $celsius) | |
{ | |
return $this; | |
} | |
} | |
final class head extends part | |
{ | |
} | |
final class leg extends part | |
{ | |
function bodyCold(celsius $celsius) | |
{ | |
return $this->shiver($celsius); | |
} | |
function bodyHot(celsius $celsius) | |
{ | |
return $this->sweat($celsius); | |
} | |
private function sweat(celsius $celsius) | |
{ | |
if ($this->alive) | |
{ | |
echo 'Sweat' . ($celsius->asInteger <= 35 ? '' : '++') . PHP_EOL; | |
} | |
return $this; | |
} | |
private function shiver() | |
{ | |
if ($this->alive) | |
{ | |
echo 'Shiver' . ($celsius->asInteger <= 12 ? '' : '++') . PHP_EOL; | |
} | |
return $this; | |
} | |
} | |
final class arm extends part | |
{ | |
function bodyCold(celsius $celsius) | |
{ | |
return $this->shiver($celsius); | |
} | |
function bodyHot(celsius $celsius) | |
{ | |
return $this->sweat($celsius); | |
} | |
private function sweat(celsius $celsius) | |
{ | |
if ($this->alive) | |
{ | |
echo 'Sweat' . ($celsius->asInteger <= 25 ? '' : '++') . PHP_EOL; | |
} | |
return $this; | |
} | |
private function shiver() | |
{ | |
if ($this->alive) | |
{ | |
echo 'Shiver' . ($celsius->asInteger <= 5 ? '' : '++') . PHP_EOL; | |
} | |
return $this; | |
} | |
} | |
abstract class body | |
{ | |
private | |
$parts | |
; | |
function __construct(part $part, part ...$parts) | |
{ | |
parent::__construct(); | |
array_unshift($parts, $part); | |
$this->parts = $parts; | |
} | |
function headShot() | |
{ | |
return $this->isDead(); | |
} | |
function temperatureIs(celsius $celsius) | |
{ | |
switch (true) | |
{ | |
case $celsius->asInteger < -50: | |
return $this->isDead(); | |
case $celsius->asInteger < 0: | |
return $this->cold($celsius); | |
case $celsius->asInteger > 30: | |
return $this->hot($celsius); | |
} | |
return $this; | |
} | |
private function isDead() | |
{ | |
foreach ($this->parts as $part) | |
{ | |
$part->bodyIsDead(); | |
} | |
return $this; | |
} | |
private function cold(celsius $celsius) | |
{ | |
foreach ($this->parts as $part) | |
{ | |
$part->bodyCold($celsius); | |
} | |
return $this; | |
} | |
private function hot(celsius $celsius) | |
{ | |
foreach ($this->parts as $part) | |
{ | |
$part->bodyHot($celsius); | |
} | |
return $this; | |
} | |
} | |
final class human extends body | |
{ | |
function __construct() | |
{ | |
parent::__construct(new head, new arm, new arm, new leg, new leg); | |
} | |
} | |
(new human)->temperatureIs(new celsius(32))->headShot()->temperatureIs(new celsius(35)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment