Migrations\Shell\Task\SeedTask::prettifyArray PHP Метод

prettifyArray() защищенный Метод

Prettify var_export of an array output
protected prettifyArray ( array $array, integer $tabCount = 3, string $indentCharacter = " " ) : string
$array array Array to prettify
$tabCount integer Initial tab count
$indentCharacter string Desired indent for the code.
Результат string
    protected function prettifyArray($array, $tabCount = 3, $indentCharacter = "    ")
    {
        $content = var_export($array, true);
        $lines = explode("\n", $content);
        $inString = false;
        foreach ($lines as $k => &$line) {
            if ($k === 0) {
                // First row
                $line = '[';
                continue;
            }
            if ($k === count($lines) - 1) {
                // Last row
                $line = str_repeat($indentCharacter, --$tabCount) . ']';
                continue;
            }
            $line = ltrim($line);
            if (!$inString) {
                if ($line === '),') {
                    //Check for closing bracket
                    $line = '],';
                    $tabCount--;
                } elseif (preg_match("/^\\d+\\s\\=\\>\\s\$/", $line)) {
                    // Mark '0 =>' kind of lines to remove
                    $line = false;
                    continue;
                }
                //Insert tab count
                $line = str_repeat($indentCharacter, $tabCount) . $line;
            }
            $length = strlen($line);
            for ($j = 0; $j < $length; $j++) {
                if ($line[$j] === '\\') {
                    //skip character right after an escape \
                    $j++;
                } elseif ($line[$j] === '\'') {
                    //check string open/end
                    $inString = !$inString;
                }
            }
            //check for opening bracket
            if (!$inString && trim($line) === 'array (') {
                $line = str_replace('array (', '[', $line);
                $tabCount++;
            }
        }
        unset($line);
        // Remove marked lines
        $lines = array_filter($lines, function ($line) {
            return $line !== false;
        });
        return implode("\n", $lines);
    }