HEX
Server: Apache
System: Linux srv-plesk28.ps.kz 5.14.0-284.18.1.el9_2.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jun 29 17:06:27 EDT 2023 x86_64
User: greencl1 (10085)
PHP: 8.1.33
Disabled: apache_setenv,dl,eval,exec,openlog,passthru,pcntl_exec,pcntl_fork,popen,posix_getpwuid,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,shell_exec,socket_create,socket_create_listen,socket_create_pair,syslog,system,socket_listen,stream_socket_server
Upload Files
File: /var/www/vhosts/greenclinic.kz/test.greenclinic.kz/vendor/october/rain/src/Html/Helper.php
<?php namespace October\Rain\Html;

/**
 * Methods that may be useful for processing HTML tasks
 *
 * @package october\html
 * @author Alexey Bobkov, Samuel Georges
 */
class Helper
{
    /**
     * Converts a HTML array string to an identifier string.
     * HTML: user[location][city]
     * Result: user-location-city
     * @param $string String to process
     * @return string
     */
    public static function nameToId($string)
    {
        return rtrim(str_replace('--', '-', str_replace(['[', ']'], '-', $string)), '-');
    }

    /**
     * Converts a HTML named array string to a PHP array. Empty values are removed.
     * HTML: user[location][city]
     * PHP:  ['user', 'location', 'city']
     * @param $string String to process
     * @return array
     */
    public static function nameToArray($string)
    {
        $result = [$string];

        if (strpbrk($string, '[]') === false) {
            return $result;
        }

        if (preg_match('/^([^\]]+)(?:\[(.+)\])+$/', $string, $matches)) {
            if (count($matches) < 2) {
                return $result;
            }

            $result = explode('][', $matches[2]);
            array_unshift($result, $matches[1]);
        }

        $result = array_filter($result, function($val) { return strlen($val); });

        return $result;
    }

    /**
     * Reduces the field name hierarchy depth by $level levels.
     * country[city][0][street][0] turns into country[city][0] when reduced by 1 level;
     * country[city][0][street][0] turns into country when reduced by 2 levels;
     * etc.
     *
     * @param string $fieldName
     * @param int $level
     * @return string
     */
    public static function reduceNameHierarchy($fieldName, $level)
    {
        $formName = self::nameToArray($fieldName);
        $sliceLength = count($formName) - $level * 2;

        if ($sliceLength <= 1) {
            return $formName[0];
        }

        $formName = array_slice($formName, 0, $sliceLength);
        $formNameFirst = array_shift($formName);

        return $formNameFirst.'['.implode('][', $formName).']';
    }
}