-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
563537d
commit f3a23d3
Showing
4 changed files
with
197 additions
and
78 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Eldia\Promise; | ||
|
||
use ReflectionFunction; | ||
use ReflectionMethod; | ||
|
||
trait PromiseCallbacks | ||
{ | ||
|
||
/** call a callable instance */ | ||
protected function call(callable $callback, ...$params) | ||
{ | ||
return call_user_func($callback, ...$params); | ||
} | ||
|
||
|
||
/** get the first parameter type */ | ||
protected function getFirstParatemerType(callable $callback): mixed | ||
{ | ||
return $this->getParatemerTypes($callback)[0]; | ||
} | ||
|
||
|
||
/** get all the parameter types */ | ||
protected function getParatemerTypes(callable $callback): array | ||
{ | ||
$reflector = $this->getReflector($callback); | ||
|
||
$types = []; | ||
|
||
foreach ($reflector->getParameters() as $parameter) { | ||
$types[] = $parameter->getType()?->getName(); | ||
} | ||
|
||
|
||
return $types; | ||
} | ||
|
||
|
||
/** get the reflector for the given callable */ | ||
protected function getReflector(callable $callback) | ||
{ | ||
if (is_array($callback)) return new ReflectionMethod($callback[0], $callback[1]); | ||
|
||
return new ReflectionFunction($callback); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Eldia\Promise; | ||
|
||
use Throwable; | ||
|
||
trait PromiseErrors | ||
{ | ||
protected ?Throwable $error = null; | ||
|
||
|
||
/** determine wether the promise has error or not */ | ||
protected function hasError(): bool | ||
{ | ||
return !is_null($this->error); | ||
} | ||
|
||
|
||
/** add throwable error */ | ||
protected function error(Throwable $error) | ||
{ | ||
$this->error = $error; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Eldia\Promise; | ||
|
||
trait PromiseState | ||
{ | ||
protected readonly string $PENDING; | ||
protected readonly string $FULFILLED; | ||
protected readonly string $REJECTED; | ||
|
||
/** | ||
* holds the current promise state | ||
* | ||
* @var string | ||
*/ | ||
protected string $state; | ||
|
||
|
||
/** | ||
* initial stats values | ||
*/ | ||
protected function initState(): void | ||
{ | ||
$this->PENDING = 'pending'; | ||
$this->FULFILLED = 'fulfilled'; | ||
$this->REJECTED = 'rejected'; | ||
|
||
$this->pending(); | ||
} | ||
|
||
/** determine wether the promise still pending */ | ||
protected function isPending(): bool | ||
{ | ||
return $this->state === $this->PENDING; | ||
} | ||
|
||
/** determine wether the promise was fulfilled */ | ||
protected function isFulfilled(): bool | ||
{ | ||
return $this->state === $this->FULFILLED; | ||
} | ||
|
||
/** determine wether the promise was rejected */ | ||
protected function isRejected(): bool | ||
{ | ||
return $this->state === $this->REJECTED; | ||
} | ||
|
||
/** set the promise state to pending */ | ||
protected function pending(): void | ||
{ | ||
$this->state = $this->PENDING; | ||
} | ||
|
||
/** set the promise state to fulfilled */ | ||
protected function fulfilled(): void | ||
{ | ||
$this->state = $this->FULFILLED; | ||
} | ||
|
||
/** set the promise state to rejected */ | ||
protected function rejected(): void | ||
{ | ||
$this->state = $this->REJECTED; | ||
} | ||
} |