<?php
// Copyright 1999-2017. Plesk International GmbH. All rights reserved.

namespace Plesk\MigrationLibV8\Utils;

/**
 * Collection of methods useful to work with PHP arrays
 */
class ArrayUtils
{
    /**
     * Get array value by key that is supposed to be array. If key does not exist in the array - return empty array.
     *
     * @param $array array
     * @param $key string
     * @return array
     */
    public static function getElementDefaultArray($array, $key)
    {
        if (is_array($array) && array_key_exists($key, $array)) {
            return $array[$key];
        } else {
            return [];
        }
    }

    /**
     * Get array value by key. If key does not exists in the array - return null.
     *
     * @param $array array
     * @param $key string
     * @return mixed|null
     */
    public static function getElementDefaultNull($array, $key)
    {
        if (is_array($array) && array_key_exists($key, $array)) {
            return $array[$key];
        } else {
            return null;
        }
    }

    /**
     * Get array value by key. If key does not exists in the array - return default value.
     *
     * @param $array array
     * @param $key string
     * @param $default mixed default value
     * @return mixed|null
     */
    public static function getElement($array, $key, $default)
    {
        if (is_array($array) && array_key_exists($key, $array)) {
            return $array[$key];
        } else {
            return $default;
        }
    }

    /**
     * Remove specified keys from array
     *
     * For example, if array was [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'], and function was called with
     * [1, 2] as keys, it returns [0 => 'a', 3 => 'd']
     *
     * @param $array array
     * @param $keys string[]
     * @return array
     */
    public static function removeKeys($array, $keys)
    {
        foreach ($keys as $keyToRemove) {
            unset($array[$keyToRemove]);
        }
        return $array;
    }

    /**
     * Remove specified keys from array and compress keys, so they are correct sequence
     *
     * For example, if array was [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'], and function was called with
     * [1, 2] as keys, it returns [0 => 'a', 1 => 'd']
     *
     * @param $array array
     * @param $keys string[]
     * @return array
     */
    public static function removeKeysAndCompress($array, $keys)
    {
        foreach ($keys as $keyToRemove) {
            unset($array[$keyToRemove]);
        }
        return array_values($array);
    }

    /**
     * Remove specified key from array if value is an empty sequence.
     *
     * @param $array array
     * @param $key string
     * @return array|void
     */
    public static function removeKeyIfEmptyArray($array, $key)
    {
        if (!is_array($array) || !array_key_exists($key, $array)) {
            return $array;
        }
        if (count($array[$key]) > 0) {
            return $array;
        }
        unset($array[$key]);
        return $array;
    }

    /**
     * Remove keys from array except for specified ones.
     *
     * @param $array array
     * @param $exceptKeys string[]
     * @return array
     */
    public static function removeExceptKeys($array, $exceptKeys)
    {
        $keysToRemove = array_diff(array_keys($array), $exceptKeys);
        foreach ($keysToRemove as $key) {
            unset($array[$key]);
        }
        return $array;
    }

    /**
     * Returns array which does not contain null elements.
     *
     * @param $array array
     * @return array
     */
    public static function filterNullValues($array)
    {
        $filteredArray = [];
        foreach ($array as $key => $value) {
            if (!is_null($value)) {
                $filteredArray[$key] = $value;
            }
        }
        return $filteredArray;
    }

    /**
     * Group value by id for array of arrays.
     *
     * For example, function call on array [['id' => 10, 'name' => 'test1.tld'], ['id' => 20, 'name' => 'test2.tld']]
     * with id key 'id' and value key 'name' will result in [10 => 'test1.tld', 20 => 'test2.tld']
     *
     * @param $array array array which consists of item-arrays
     * @param $idKey string key to use to get identifier of each item
     * @param $valueKey string key to use to get value of each item
     * @return array
     */
    public static function groupValueById($array, $idKey, $valueKey)
    {
        $grouped = [];
        foreach ($array as $item) {
            $grouped[$item[$idKey]] = $item[$valueKey];
        }
        return $grouped;
    }
}
