generated from chevere/package-template
-
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
Showing
4 changed files
with
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
namespace Chevere\Parameter\Attributes; | ||
|
||
use Chevere\Parameter\Interfaces\ArgumentsInterface; | ||
use LogicException; | ||
use ReflectionFunction; | ||
use ReflectionMethod; | ||
use function Chevere\Parameter\parameterAttr; | ||
|
@@ -67,6 +68,9 @@ function genericAttr(string $name): GenericAttr | |
return parameterAttr($name, $caller); | ||
} | ||
|
||
/** | ||
* Get Arguments for an array parameter. | ||
*/ | ||
function arrayArguments(string $name): ArgumentsInterface | ||
{ | ||
$caller = debug_backtrace(0, 2)[1]; | ||
|
@@ -95,7 +99,6 @@ function arrayArguments(string $name): ArgumentsInterface | |
|
||
/** | ||
* Validates argument `$name` against attribute rules. | ||
* @throws LogicException | ||
*/ | ||
function validate(?string $name = null): void | ||
{ | ||
|
@@ -120,3 +123,23 @@ function validate(?string $name = null): void | |
} | ||
$parameters->get($name)->__invoke($arguments[$name]); | ||
} | ||
|
||
/** | ||
* Validates `$var` against the return attribute. | ||
* | ||
* @return mixed The validated `$var`. | ||
*/ | ||
function returnAttr(mixed $var): mixed | ||
{ | ||
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]; | ||
$class = $caller['class'] ?? null; | ||
$method = $caller['function']; | ||
$reflection = $class | ||
? new ReflectionMethod($class, $method) | ||
: new ReflectionFunction($method); | ||
/** @var ReflectionAttribute<ReturnAttr> $attribute */ | ||
$attribute = $reflection->getAttributes(ReturnAttr::class)[0] | ||
Check failure on line 141 in src/Attributes/functions.php GitHub Actions / PHP 8.1 test on ubuntu-latest
Check failure on line 141 in src/Attributes/functions.php GitHub Actions / PHP 8.2 test on ubuntu-latest
Check failure on line 141 in src/Attributes/functions.php GitHub Actions / PHP 8.3 test on ubuntu-latest
|
||
?? throw new LogicException('No return attribute found'); | ||
|
||
return $attribute->newInstance()->__invoke($var); | ||
Check failure on line 144 in src/Attributes/functions.php GitHub Actions / PHP 8.1 test on ubuntu-latest
Check failure on line 144 in src/Attributes/functions.php GitHub Actions / PHP 8.2 test on ubuntu-latest
|
||
} |
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
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