Dice\Dice::expand PHP Method

expand() private method

Looks for 'instance' array keys in $param and when found returns an object based on the value see {@link https:// r.je/dice.html#example3-1}
private expand ( mixed $param, array $share = [], boolean $createFromString = false ) : mixed
$param mixed Either a string or an array,
$share array Whether or not this class instance be shared, so that the same instance is passed around each time
$createFromString boolean
return mixed
    private function expand($param, array $share = [], $createFromString = false)
    {
        if (is_array($param) && isset($param['instance'])) {
            // Call or return the value sored under the key 'instance'
            // For ['instance' => ['className', 'methodName'] construct the instance before calling it
            $args = isset($param['params']) ? $this->expand($param['params']) : [];
            if (is_array($param['instance'])) {
                $param['instance'][0] = $this->expand($param['instance'][0], $share, true);
            }
            if (is_callable($param['instance'])) {
                return call_user_func($param['instance'], ...$args);
            } else {
                return $this->create($param['instance'], array_merge($args, $share));
            }
        } else {
            if (is_array($param)) {
                foreach ($param as $name => $value) {
                    $param[$name] = $this->expand($value, $share);
                }
            }
        }
        // 'instance' wasn't found, return the value unchanged
        return is_string($param) && $createFromString ? $this->create($param) : $param;
    }