iranianpep/botonomous

View on GitHub
src/Botonomous/utility/ArrayUtility.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

namespace Botonomous\utility;

/**
 * Class ArrayUtility.
 */
class ArrayUtility extends AbstractUtility
{
    /**
     * Filter an array based on $keepKeys.
     *
     * @param array $toFilter
     * @param array $keepKeys Includes the keys that need to be kept in $toFilter array
     *
     * @return array
     */
    public function filterArray(array $toFilter, array $keepKeys)
    {
        return array_intersect_key($toFilter, array_flip($keepKeys));
    }

    /**
     * Check if key is present in $search array and has got a value only if the value is string.
     *
     * @param       $key
     * @param array $search
     *
     * @return bool
     */
    public function arrayKeyValueExists(string $key, array $search): bool
    {
        if (!array_key_exists($key, $search)) {
            return false;
        }

        if (is_string($search[$key])) {
            return strlen(trim($search[$key])) > 0;
        }

        return true;
    }

    /**
     * Set a value in a nested array based on path.
     *
     * @param array $array The array to modify
     * @param array $path  The path in the array
     * @param mixed $value The value to set
     *
     * @return void
     */
    public function setNestedArrayValue(array &$array, array $path, &$value)
    {
        $current = &$array;
        foreach ($path as $key) {
            $current = &$current[$key];
        }

        $current = $value;
    }

    /**
     * Get a value in a nested array based on path
     * See https://stackoverflow.com/a/9628276/419887.
     *
     * @param array $array The array to modify
     * @param array $path  The path in the array
     *
     * @return mixed
     */
    public function getNestedArrayValue(array &$array, array $path)
    {
        $current = &$array;
        foreach ($path as $key) {
            $current = &$current[$key];
        }

        return $current;
    }

    /**
     * @param $array
     *
     * @return mixed
     */
    public function sortArrayByLength(array $array)
    {
        usort($array, function ($array1, $array2) {
            return strlen($array2) <=> strlen($array1);
        });

        return $array;
    }

    /**
     * Find the key for the max positive value.
     *
     * @param $array
     *
     * @return mixed
     */
    public function maxPositiveValueKey(array $array)
    {
        $maxValue = max($array);

        if ($maxValue > 0) {
            return array_search($maxValue, $array);
        }
    }
}