Functions.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Artful;
  4. use Closure;
  5. use Yansongda\Artful\Contract\DirectionInterface;
  6. use Yansongda\Artful\Contract\PackerInterface;
  7. use Yansongda\Artful\Direction\NoHttpRequestDirection;
  8. use Yansongda\Artful\Exception\ContainerException;
  9. use Yansongda\Artful\Exception\Exception;
  10. use Yansongda\Artful\Exception\InvalidParamsException;
  11. use Yansongda\Artful\Exception\ServiceNotFoundException;
  12. use Yansongda\Supports\Collection;
  13. use Yansongda\Supports\Str;
  14. function should_do_http_request(string $direction): bool
  15. {
  16. return NoHttpRequestDirection::class !== $direction
  17. && !in_array(NoHttpRequestDirection::class, class_parents($direction));
  18. }
  19. /**
  20. * @throws InvalidParamsException
  21. */
  22. function get_direction(mixed $direction): DirectionInterface
  23. {
  24. try {
  25. $direction = Artful::get($direction);
  26. $direction = is_string($direction) ? Artful::get($direction) : $direction;
  27. } catch (ContainerException|ServiceNotFoundException) {
  28. }
  29. if (!$direction instanceof DirectionInterface) {
  30. throw new InvalidParamsException(Exception::PARAMS_DIRECTION_INVALID, '参数异常: 配置的 `DirectionInterface` 未实现 `DirectionInterface`');
  31. }
  32. return $direction;
  33. }
  34. /**
  35. * @throws InvalidParamsException
  36. */
  37. function get_packer(mixed $packer): PackerInterface
  38. {
  39. try {
  40. $packer = Artful::get($packer);
  41. $packer = is_string($packer) ? Artful::get($packer) : $packer;
  42. } catch (ContainerException|ServiceNotFoundException) {
  43. }
  44. if (!$packer instanceof PackerInterface) {
  45. throw new InvalidParamsException(Exception::PARAMS_PACKER_INVALID, '参数异常: 配置的 `PackerInterface` 未实现 `PackerInterface`');
  46. }
  47. return $packer;
  48. }
  49. function filter_params(null|array|Collection $params, ?Closure $closure = null): Collection
  50. {
  51. $params = Collection::wrap($params);
  52. return $params->filter(static fn ($v, $k) => !Str::startsWith($k, '_') && !is_null($v) && (empty($closure) || $closure($k, $v)));
  53. }
  54. function get_radar_method(?Collection $payload): ?string
  55. {
  56. $string = $payload?->get('_method') ?? null;
  57. if (is_null($string)) {
  58. return null;
  59. }
  60. return strtoupper($string);
  61. }
  62. function get_radar_url(?Collection $payload): ?string
  63. {
  64. return $payload?->get('_url') ?? null;
  65. }
  66. function get_radar_body(?Collection $payload): mixed
  67. {
  68. return $payload?->get('_body') ?? null;
  69. }
  70. function get_radar_headers(?Collection $payload): mixed
  71. {
  72. return $payload?->get('_headers') ?? null;
  73. }