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