1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 05:11:42 +01:00

Add immutable Failure/Success futures

This commit is contained in:
Daniel Lowrey 2014-04-09 10:34:28 -04:00
parent 89c78dab6b
commit 6a613c7356
2 changed files with 118 additions and 0 deletions

59
lib/Failure.php Executable file
View File

@ -0,0 +1,59 @@
<?php
namespace Alert;
/**
* A placeholder for a resolved failure
*/
class Failure implements Future {
private $error;
public function __construct(\Exception $error) {
$this->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;
}
}

59
lib/Success.php Executable file
View File

@ -0,0 +1,59 @@
<?php
namespace Alert;
/**
* A placeholder for a successfully resolved value
*/
class Success implements Future {
private $value;
public function __construct($value = NULL) {
$this->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;
}
}