Util.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Illuminate\Container;
  3. use Closure;
  4. use ReflectionNamedType;
  5. /**
  6. * @internal
  7. */
  8. class Util
  9. {
  10. /**
  11. * If the given value is not an array and not null, wrap it in one.
  12. *
  13. * From Arr::wrap() in Illuminate\Support.
  14. *
  15. * @param mixed $value
  16. * @return array
  17. */
  18. public static function arrayWrap($value)
  19. {
  20. if (is_null($value)) {
  21. return [];
  22. }
  23. return is_array($value) ? $value : [$value];
  24. }
  25. /**
  26. * Return the default value of the given value.
  27. *
  28. * From global value() helper in Illuminate\Support.
  29. *
  30. * @param mixed $value
  31. * @param mixed ...$args
  32. * @return mixed
  33. */
  34. public static function unwrapIfClosure($value, ...$args)
  35. {
  36. return $value instanceof Closure ? $value(...$args) : $value;
  37. }
  38. /**
  39. * Get the class name of the given parameter's type, if possible.
  40. *
  41. * From Reflector::getParameterClassName() in Illuminate\Support.
  42. *
  43. * @param \ReflectionParameter $parameter
  44. * @return string|null
  45. */
  46. public static function getParameterClassName($parameter)
  47. {
  48. $type = $parameter->getType();
  49. if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) {
  50. return null;
  51. }
  52. $name = $type->getName();
  53. if (! is_null($class = $parameter->getDeclaringClass())) {
  54. if ($name === 'self') {
  55. return $class->getName();
  56. }
  57. if ($name === 'parent' && $parent = $class->getParentClass()) {
  58. return $parent->getName();
  59. }
  60. }
  61. return $name;
  62. }
  63. }