master

laravel/framework

Last updated at: 29/12/2023 09:20

Util.php

TLDR

This file, Util.php, is a part of the Illuminate\Container namespace in the Demo Projects project. It contains a class named Util with several static methods. These methods perform various utility operations related to arrays, values, and reflection parameters.

Methods

arrayWrap

This method takes a value and wraps it in an array if it is not already an array and not null. It is inspired by the Arr::wrap() method in the Illuminate\Support package. The method is declared as public static.

unwrapIfClosure

This method takes a value and unpacks it if it is an instance of Closure. If the value is a closure, it is called with the given arguments and returned. Otherwise, the value itself is returned. This method is similar to the global value() helper in the Illuminate\Support package. The method is declared as public static.

getParameterClassName

This method takes a ReflectionParameter object and retrieves the class name of its type if possible. It checks if the type is not a built-in type and then determines the class name based on the type's name. If the parameter is declared in a class and the type is 'self', the class name of the declaring class is returned. If the type is 'parent' and the declaring class has a parent class, the parent class name is returned. Otherwise, the type's name is returned. This method is similar to the Reflector::getParameterClassName() method in the Illuminate\Support package. The method is declared as public static.

Classes

Class: Util

This class contains static methods that provide utility operations. It is intended for internal use (marked with @internal). The class is located in the Illuminate\Container namespace and serves as a helper class for container-related functionality.

<?php

namespace Illuminate\Container;

use Closure;
use ReflectionNamedType;

/**
 * @internal
 */
class Util
{
    /**
     * If the given value is not an array and not null, wrap it in one.
     *
     * From Arr::wrap() in Illuminate\Support.
     *
     * @param  mixed  $value
     * @return array
     */
    public static function arrayWrap($value)
    {
        if (is_null($value)) {
            return [];
        }

        return is_array($value) ? $value : [$value];
    }

    /**
     * Return the default value of the given value.
     *
     * From global value() helper in Illuminate\Support.
     *
     * @param  mixed  $value
     * @param  mixed  ...$args
     * @return mixed
     */
    public static function unwrapIfClosure($value, ...$args)
    {
        return $value instanceof Closure ? $value(...$args) : $value;
    }

    /**
     * Get the class name of the given parameter's type, if possible.
     *
     * From Reflector::getParameterClassName() in Illuminate\Support.
     *
     * @param  \ReflectionParameter  $parameter
     * @return string|null
     */
    public static function getParameterClassName($parameter)
    {
        $type = $parameter->getType();

        if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) {
            return null;
        }

        $name = $type->getName();

        if (! is_null($class = $parameter->getDeclaringClass())) {
            if ($name === 'self') {
                return $class->getName();
            }

            if ($name === 'parent' && $parent = $class->getParentClass()) {
                return $parent->getName();
            }
        }

        return $name;
    }
}