| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Container; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use ReflectionNamedType; |
| 7 |
|
| 8 |
/** |
| 9 |
* @internal |
| 10 |
*/ |
| 11 |
class 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 FluentForm\Framework\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 FluentForm\Framework\Support. |
| 34 |
* |
| 35 |
* @param mixed $value |
| 36 |
* @return mixed |
| 37 |
*/ |
| 38 |
public static function unwrapIfClosure($value) |
| 39 |
{ |
| 40 |
return $value instanceof Closure ? $value() : $value; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get the class name of the given parameter's type, if possible. |
| 45 |
* |
| 46 |
* From Reflector::getParameterClassName() in FluentForm\Framework\Support. |
| 47 |
* |
| 48 |
* @param \ReflectionParameter $parameter |
| 49 |
* @return string|null |
| 50 |
*/ |
| 51 |
public static function getParameterClassName($parameter) |
| 52 |
{ |
| 53 |
$type = $parameter->getType(); |
| 54 |
|
| 55 |
if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) { |
| 56 |
return null; |
| 57 |
} |
| 58 |
|
| 59 |
$name = $type->getName(); |
| 60 |
|
| 61 |
if (! is_null($class = $parameter->getDeclaringClass())) { |
| 62 |
if ($name === 'self') { |
| 63 |
return $class->getName(); |
| 64 |
} |
| 65 |
|
| 66 |
if ($name === 'parent' && $parent = $class->getParentClass()) { |
| 67 |
return $parent->getName(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
return $name; |
| 72 |
} |
| 73 |
} |
| 74 |
|