From 6a613c73567e6cc0dd28b28333c9de604abb5e2f Mon Sep 17 00:00:00 2001 From: Daniel Lowrey Date: Wed, 9 Apr 2014 10:34:28 -0400 Subject: [PATCH] Add immutable Failure/Success futures --- lib/Failure.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/Success.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100755 lib/Failure.php create mode 100755 lib/Success.php diff --git a/lib/Failure.php b/lib/Failure.php new file mode 100755 index 0000000..513bfeb --- /dev/null +++ b/lib/Failure.php @@ -0,0 +1,59 @@ +error = $error; + } + + /** + * Pass the Future to the specified callback upon completion regardless of success or failure + * + * @param callable $onComplete + */ + public function onComplete(callable $onComplete) { + call_user_func($onComplete, $this); + } + + /** + * Has the Future completed (succeeded/failure is irrelevant)? + * + * @return bool + */ + public function isComplete() { + return TRUE; + } + + /** + * Has the Future value been successfully resolved? + * + * @return bool + */ + public function succeeded() { + return FALSE; + } + + /** + * Retrieve the value that successfully fulfilled the Future + * + * @throws \Exception + */ + public function getValue() { + throw $this->error; + } + + /** + * Retrieve the Exception responsible for Future resolution failure + * + * @return \Exception + */ + public function getError() { + return $this->error; + } +} diff --git a/lib/Success.php b/lib/Success.php new file mode 100755 index 0000000..b0d0560 --- /dev/null +++ b/lib/Success.php @@ -0,0 +1,59 @@ +value = $value; + } + + /** + * Pass the Future to the specified callback upon completion regardless of success or failure + * + * @param callable $onComplete + */ + public function onComplete(callable $onComplete) { + call_user_func($onComplete, $this); + } + + /** + * Has the Future completed (success is irrelevant)? + * + * @return bool + */ + public function isComplete() { + return TRUE; + } + + /** + * Has the Future value been successfully resolved? + * + * @return bool + */ + public function succeeded() { + return TRUE; + } + + /** + * Retrieve the value that successfully fulfilled the Future + * + * @return mixed + */ + public function getValue() { + return $this->value; + } + + /** + * Retrieve the Exception responsible for Future resolution failure + * + * @return NULL + */ + public function getError() { + return NULL; + } +}