<?php

namespace OxygenApp\Utils;
if (!defined('ABSPATH')) exit;


class Utils {
    static function addBeforeLastElement(array $array, $newKey, $newValue): array {
        if (!is_array($array) || empty($array)) {
            return $array;
        }
        $beforeLast = array_slice($array, 0, -1, true);
        $last = array_slice($array, -1, null, true);
        $newElement = [$newKey => $newValue];
        return array_merge($beforeLast, $newElement, $last);
    }

    static function generateUniqueId(int $length = 7): string {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';

        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[random_int(0, $charactersLength - 1)];
        }

        return $randomString;
    }
}