Arrays > refactoring & more tests

This commit is contained in:
Meritoo
2019-02-23 19:41:37 +01:00
parent ede9a182b4
commit 924e492e11
4 changed files with 956 additions and 264 deletions

View File

@@ -2,6 +2,10 @@
Common and useful classes, methods, exceptions etc.
# 0.1.6
1. Arrays > refactoring & more tests
# 0.1.5
1. Tests > Date > one more test case

View File

@@ -1 +1 @@
0.1.5
0.1.6

View File

@@ -27,78 +27,114 @@ class Arrays
* Converts given array's column to string.
* Recursive call is made for multi-dimensional arrays.
*
* @param array $array Array data to be converted
* @param string|int $arrayColumnKey (optional) Column name
* @param string $separator (optional) Separator used in resultant string
* @return string
* @param array $array Data to be converted
* @param string|int $arrayColumnKey (optional) Column name. Default: "".
* @param string $separator (optional) Separator used between values. Default: ",".
* @return string|null
*/
public static function values2string(array $array, $arrayColumnKey = '', $separator = ',')
{
$effect = '';
if (!empty($array)) {
foreach ($array as $key => $value) {
if (!empty($effect) &&
(
empty($arrayColumnKey) || (!is_array($value) && $key === $arrayColumnKey)
)
) {
$effect .= $separator;
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return null;
}
$values = [];
foreach ($array as $key => $value) {
$appendMe = null;
if (is_array($value)) {
$effect .= self::values2string($value, $arrayColumnKey, $separator);
$appendMe = self::values2string($value, $arrayColumnKey, $separator);
} elseif (empty($arrayColumnKey)) {
$effect .= $value;
$appendMe = $value;
} elseif ($key === $arrayColumnKey) {
$effect .= $array[$arrayColumnKey];
}
}
$appendMe = $array[$arrayColumnKey];
}
return $effect;
/*
* Part to append is unknown?
* Let's go to next part
*/
if (null === $appendMe) {
continue;
}
$values[] = $appendMe;
}
/*
* No values found?
* Nothing to do
*/
if (empty($values)) {
return null;
}
return implode($separator, $values);
}
/**
* Converts given array to string with keys, e.g. abc=1&def=2 or abc="1" def="2"
*
* @param array $array Array data to be converted
* @param string $separator (optional) Separator used between name-value pairs in resultant string
* @param string $valuesKeysSeparator (optional) Separator used between name and value in resultant string
* @param string $valuesWrapper (optional) Wrapper used to wrap values, e.g. double-quote: key="value"
* @return string
* @param array $array Data to be converted
* @param string $separator (optional) Separator used between name-value pairs. Default: ",".
* @param string $valuesKeysSeparator (optional) Separator used between name and value. Default: "=".
* @param string $valuesWrapper (optional) Wrapper used to wrap values, e.g. double-quote: key="value".
* Default: "".
* @return string|null
*/
public static function valuesKeys2string($array, $separator = ',', $valuesKeysSeparator = '=', $valuesWrapper = '')
{
$effect = '';
public static function valuesKeys2string(
array $array,
$separator = ',',
$valuesKeysSeparator = '=',
$valuesWrapper = ''
) {
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return null;
}
$result = '';
if (is_array($array) && !empty($array)) {
foreach ($array as $key => $value) {
if (!empty($effect)) {
$effect .= $separator;
if (!empty($result)) {
$result .= $separator;
}
if (!empty($valuesWrapper)) {
$value = sprintf('%s%s%s', $valuesWrapper, $value, $valuesWrapper);
}
$effect .= $key . $valuesKeysSeparator . $value;
}
$result .= $key . $valuesKeysSeparator . $value;
}
return $effect;
return $result;
}
/**
* Converts given array's rows to csv string
*
* @param array $array Array data to be converted. It have to be an array that represents database table.
* @param string $separator (optional) Separator used in resultant string
* @return string
* @param array $array Data to be converted. It have to be an array that represents database table.
* @param string $separator (optional) Separator used between values. Default: ",".
* @return string|null
*/
public static function values2csv($array, $separator = ',')
public static function values2csv(array $array, $separator = ',')
{
if (is_array($array) && !empty($array)) {
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return null;
}
$rows = [];
$lineSeparator = "\n";
@@ -117,12 +153,11 @@ class Arrays
}
}
if (!empty($rows)) {
return implode($lineSeparator, $rows);
}
if (empty($rows)) {
return '';
}
return '';
return implode($lineSeparator, $rows);
}
/**
@@ -242,12 +277,20 @@ class Arrays
/**
* Returns breadcrumb (a path) to the last element of array
*
* @param array $array The array to get the breadcrumb
* @param string $separator (optional) Separator used to stick the elements
* @return string
* @param array $array Data to get the breadcrumb
* @param string $separator (optional) Separator used to stick the elements. Default: "/".
* @return string|null
*/
public static function getLastElementBreadCrumb($array, $separator = '/')
public static function getLastElementBreadCrumb(array $array, $separator = '/')
{
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return null;
}
$keys = array_keys($array);
$keysCount = count($keys);
@@ -350,7 +393,7 @@ class Arrays
return null;
}
$effect = '';
$result = '';
$counter = 0;
$arrayCount = count($array);
@@ -366,14 +409,14 @@ class Arrays
}
if (!empty($jsVariableName) && is_string($jsVariableName)) {
$effect .= sprintf('var %s = ', $jsVariableName);
$result .= sprintf('var %s = ', $jsVariableName);
}
$effect .= 'new Array(';
$result .= 'new Array(';
if ($preserveIndexes || $isMultiDimensional) {
$effect .= $arrayCount;
$effect .= ');';
$result .= $arrayCount;
$result .= ');';
}
foreach ($array as $index => $value) {
@@ -397,20 +440,20 @@ class Arrays
* autoGeneratedVariable[1] = new Array(...);
*/
if (1 === $counter) {
$effect .= "\n";
$result .= "\n";
}
$effect .= $value . "\n";
$effect .= sprintf('%s[%s] = %s;', $jsVariableName, Miscellaneous::quoteValue($index), $variable);
$result .= $value . "\n";
$result .= sprintf('%s[%s] = %s;', $jsVariableName, Miscellaneous::quoteValue($index), $variable);
if ($counter !== $arrayCount) {
$effect .= "\n";
$result .= "\n";
}
}
} elseif ($preserveIndexes) {
if (!empty($jsVariableName)) {
$index = Miscellaneous::quoteValue($index);
$effect .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value);
$result .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value);
}
} else {
$format = '%s';
@@ -419,29 +462,34 @@ class Arrays
$format .= ', ';
}
$effect .= sprintf($format, $value);
$result .= sprintf($format, $value);
}
}
if (!$preserveIndexes && !$isMultiDimensional) {
$effect .= ');';
$result .= ');';
}
return $effect;
return $result;
}
/**
* Quotes (adds quotes) to elements of an array that are strings
* Quotes (adds quotes) to elements that are strings and returns new array (with quoted elements)
*
* @param array $array The array to check for string values
* @return array
* @return array|null
*/
public static function quoteStrings($array)
public static function quoteStrings(array $array)
{
$effect = $array;
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return null;
}
if (is_array($array) && !empty($array)) {
$effect = [];
$result = [];
foreach ($array as $index => $value) {
if (is_array($value)) {
@@ -452,11 +500,10 @@ class Arrays
}
}
$effect[$index] = $value;
}
$result[$index] = $value;
}
return $effect;
return $result;
}
/**
@@ -495,6 +542,14 @@ class Arrays
*/
public static function getLastKey(array $array)
{
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return null;
}
$keys = array_keys($array);
return end($keys);
@@ -543,9 +598,9 @@ class Arrays
* @param bool $before (optional) If is set to true, all elements before given needle are removed. Otherwise - all
* after needle.
*/
public static function removeElements(&$array, $needle, $before = true)
public static function removeElements(array &$array, $needle, $before = true)
{
if (is_array($array) && !empty($array)) {
if (!empty($array)) {
if (!$before) {
$array = array_reverse($array, true);
}
@@ -587,7 +642,7 @@ class Arrays
* value will be used with it's key, because other will be overridden.
* Otherwise - values are preserved and keys assigned to that values are
* returned as an array.
* @return array
* @return array|null
*
* Example of $ignoreDuplicatedValues = false:
* - provided array
@@ -613,7 +668,7 @@ class Arrays
* Nothing to do
*/
if (empty($array)) {
return [];
return null;
}
$replaced = [];
@@ -724,8 +779,9 @@ class Arrays
* ]
*
* @param string $string The string to be converted
* @param string $separator (optional) Separator used between name-value pairs in the string
* @param string $valuesKeysSeparator (optional) Separator used between name and value in the string
* @param string $separator (optional) Separator used between name-value pairs in the string.
* Default: "|".
* @param string $valuesKeysSeparator (optional) Separator used between name and value in the string. Default: ":".
* @return array
*/
public static function string2array($string, $separator = '|', $valuesKeysSeparator = ':')
@@ -759,52 +815,52 @@ class Arrays
* Returns information if given keys exist in given array
*
* @param array $keys The keys to find
* @param array $array The array which maybe contains keys
* @param array $array The array that maybe contains keys
* @param bool $explicit (optional) If is set to true, all keys should exist in given array. Otherwise - not all.
* @return bool
*/
public static function areKeysInArray($keys, $array, $explicit = true)
public static function areKeysInArray(array $keys, array $array, $explicit = true)
{
$effect = false;
$result = false;
if (is_array($array) && !empty($array)) {
if (!empty($array)) {
$firstKey = true;
foreach ($keys as $key) {
$exists = array_key_exists($key, $array);
if ($firstKey) {
$effect = $exists;
$result = $exists;
$firstKey = false;
} elseif ($explicit) {
$effect = $effect && $exists;
$result = $result && $exists;
if (!$effect) {
if (!$result) {
break;
}
} else {
$effect = $effect || $exists;
$result = $result || $exists;
if ($effect) {
if ($result) {
break;
}
}
}
}
return $effect;
return $result;
}
/**
* Returns paths of the last elements
*
* @param array $array The array with elements
* @param string $separator (optional) Separator used in resultant strings. Default: ".".
* @param string $separator (optional) Separator used between elements. Default: ".".
* @param string $parentPath (optional) Path of the parent element. Default: "".
* @param string|array $stopIfMatchedBy (optional) Patterns of keys or paths that matched will stop the process
* of path building and including children of those keys or paths (recursive
* will not be used for keys in lower level of given array)
* @return array
* will not be used for keys in lower level of given array). Default: "".
* @return array|null
*
* Examples - $stopIfMatchedBy argument:
* a) "\d+"
@@ -820,7 +876,7 @@ class Arrays
* Nothing to do
*/
if (empty($array)) {
return [];
return null;
}
if (!empty($stopIfMatchedBy)) {
@@ -906,12 +962,13 @@ class Arrays
* first level only.
* @return bool
*/
public static function areAllKeysMatchedByPattern($array, $pattern, $firstLevelOnly = false)
public static function areAllKeysMatchedByPattern(array $array, $pattern, $firstLevelOnly = false)
{
/*
* It's not an array or it's empty array?
* No elements?
* Nothing to do
*/
if (!is_array($array) || (is_array($array) && empty($array))) {
if (empty($array)) {
return false;
}
@@ -955,10 +1012,10 @@ class Arrays
*
* @param array $array The array to check
* @param bool $firstLevelOnly (optional) If is set to true, all keys / indexes are checked. Otherwise - from the
* first level only.
* first level only (default behaviour).
* @return bool
*/
public static function areAllKeysIntegers($array, $firstLevelOnly = false)
public static function areAllKeysIntegers(array $array, $firstLevelOnly = false)
{
$pattern = '\d+';
@@ -996,9 +1053,17 @@ class Arrays
*/
public static function getValueByKeysPath(array $array, array $keys)
{
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return null;
}
$value = null;
if (!empty($array) && self::issetRecursive($array, $keys)) {
if (self::issetRecursive($array, $keys)) {
foreach ($keys as $key) {
$value = $array[$key];
array_shift($keys);
@@ -1042,9 +1107,16 @@ class Arrays
*/
public static function issetRecursive(array $array, array $keys)
{
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return false;
}
$isset = false;
if (!empty($array)) {
foreach ($keys as $key) {
$isset = isset($array[$key]);
@@ -1059,7 +1131,6 @@ class Arrays
break;
}
}
return $isset;
}
@@ -1113,11 +1184,18 @@ class Arrays
* @param string $keyName (optional) Name of key which will contain the position value
* @param int $startPosition (optional) Default, start value of the position for main / given array, not the
* children
* @return array
* @return array|null
*/
public static function setPositions(array $array, $keyName = self::POSITION_KEY_NAME, $startPosition = null)
{
if (!empty($array)) {
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return null;
}
$childPosition = 1;
if (null !== $startPosition) {
@@ -1130,7 +1208,6 @@ class Arrays
++$childPosition;
}
}
}
return $array;
}
@@ -1143,14 +1220,19 @@ class Arrays
*/
public static function trimRecursive(array $array)
{
$effect = $array;
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return [];
}
if (!empty($array)) {
$effect = [];
$result = [];
foreach ($array as $key => $value) {
if (is_array($value)) {
$effect[$key] = self::trimRecursive($value);
$result[$key] = self::trimRecursive($value);
continue;
}
@@ -1158,11 +1240,10 @@ class Arrays
$value = trim($value);
}
$effect[$key] = $value;
}
$result[$key] = $value;
}
return $effect;
return $result;
}
/**
@@ -1255,7 +1336,7 @@ class Arrays
*
* @param array $array The array with elements to implode
* @param string $separator Separator used to stick together elements of given array
* @return string
* @return string|null
*/
public static function implodeSmart(array $array, $separator)
{
@@ -1264,7 +1345,7 @@ class Arrays
* Nothing to do
*/
if (empty($array)) {
return '';
return null;
}
foreach ($array as &$element) {
@@ -1454,11 +1535,18 @@ class Arrays
* @param int|null $startIndex (optional) Index from which incrementation should be started. If not provided,
* the first index / key will be used.
* @param int $incrementStep (optional) Value used for incrementation. The step of incrementation.
* @return array
* @return array|null
*/
public static function incrementIndexes(array $array, $startIndex = null, $incrementStep = 1)
{
if (!empty($array)) {
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return null;
}
$valuesToIncrement = [];
/*
@@ -1498,7 +1586,6 @@ class Arrays
}
}
}
}
return $array;
}
@@ -1554,6 +1641,14 @@ class Arrays
*/
public static function getDimensionsCount(array $array)
{
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return 0;
}
$dimensionsCount = 1;
foreach ($array as $value) {
@@ -1577,7 +1672,7 @@ class Arrays
* Returns non-empty values, e.g. without "" (empty string), null or []
*
* @param array $values The values to filter
* @return array
* @return array|null
*/
public static function getNonEmptyValues(array $values)
{
@@ -1586,7 +1681,7 @@ class Arrays
* Nothing to do
*/
if (empty($values)) {
return [];
return null;
}
return array_filter($values, function ($value) {
@@ -1602,10 +1697,18 @@ class Arrays
*
* @param array $values The values to filter
* @param string $separator (optional) Separator used to implode the values. Default: ", ".
* @return string
* @return string|null
*/
public static function getNonEmptyValuesAsString(array $values, $separator = ', ')
{
/*
* No elements?
* Nothing to do
*/
if (empty($values)) {
return null;
}
$nonEmpty = self::getNonEmptyValues($values);
/*
@@ -1629,6 +1732,14 @@ class Arrays
*/
private static function getNeighbour(array $array, $element, $next = true)
{
/*
* No elements?
* Nothing to do
*/
if (empty($array)) {
return null;
}
$noNext = $next && self::isLastElement($array, $element);
$noPrevious = !$next && self::isFirstElement($array, $element);

View File

@@ -31,50 +31,95 @@ class ArraysTest extends BaseTestCase
static::assertHasNoConstructor(Arrays::class);
}
public function testValues2string()
/**
* @param string $description Description of test
* @param string $expected Expected array converted to string
* @param array $array Data to be converted
* @param string $arrayColumnKey (optional) Column name. Default: "".
* @param string $separator (optional) Separator used between values. Default: ",".
*
* @dataProvider provideArrayValues2string
*/
public function testValues2string($description, $expected, array $array, $arrayColumnKey = '', $separator = ',')
{
/*
* Simple array / string
*/
$simpleString = 'Lorem,ipsum,dolor,sit,amet';
$simpleStringWithDots = str_replace(',', '.', $simpleString);
self::assertEquals($simpleString, Arrays::values2string($this->simpleArray));
self::assertEquals('ipsum', Arrays::values2string($this->simpleArray, 1));
self::assertEquals($simpleStringWithDots, Arrays::values2string($this->simpleArray, '', '.'));
/*
* Complex array / string
*/
$complexString = 'sit,egestas,adipiscing,1234,,donec,quis,elit,iaculis,primis';
$complexStringWithDots = str_replace(',', '.', $complexString);
self::assertEquals($complexString, Arrays::values2string($this->complexArray));
self::assertEquals($complexStringWithDots, Arrays::values2string($this->complexArray, '', '.'));
/*
* Other cases
*/
self::assertEquals('', Arrays::values2string([]));
self::assertSame($expected, Arrays::values2string($array, $arrayColumnKey, $separator), $description);
}
public function testValuesKeys2string()
{
self::assertEquals('0=Lorem,1=ipsum,2=dolor,3=sit,4=amet', Arrays::valuesKeys2string($this->simpleArray));
self::assertEquals('0=Lorem;1=ipsum;2=dolor;3=sit;4=amet', Arrays::valuesKeys2string($this->simpleArray, ';'));
self::assertEquals('0=Lorem 1=ipsum 2=dolor 3=sit 4=amet', Arrays::valuesKeys2string($this->simpleArray, ' '));
/**
* @param string $description Description of test
* @param string $expected Expected array converted to string
* @param array $array Data to be converted
* @param string $separator (optional) Separator used between name-value pairs. Default: ",".
* @param string $valuesKeysSeparator (optional) Separator used between name and value. Default: "=".
* @param string $valuesWrapper (optional) Wrapper used to wrap values, e.g. double-quote: key="value".
* Default: "".
*
* @dataProvider provideArrayValuesKeysConverted2string
*/
public function testValuesKeys2string(
$description,
$expected,
array $array,
$separator = ',',
$valuesKeysSeparator = '=',
$valuesWrapper = ''
) {
self::assertSame(
$expected,
Arrays::valuesKeys2string($array, $separator, $valuesKeysSeparator, $valuesWrapper),
$description
);
self::assertEquals('0="Lorem" 1="ipsum" 2="dolor" 3="sit" 4="amet"', Arrays::valuesKeys2string($this->simpleArray, ' ', '=', '"'));
self::assertEquals('0="Lorem", 1="ipsum", 2="dolor", 3="sit", 4="amet"', Arrays::valuesKeys2string($this->simpleArray, ', ', '=', '"'));
self::assertSame(
'0=Lorem,1=ipsum,2=dolor,3=sit,4=amet',
Arrays::valuesKeys2string($this->simpleArray),
'Simple array'
);
self::assertSame(
'0=Lorem;1=ipsum;2=dolor;3=sit;4=amet',
Arrays::valuesKeys2string($this->simpleArray, ';'),
'Simple array (with custom separator)'
);
self::assertSame(
'0=Lorem 1=ipsum 2=dolor 3=sit 4=amet',
Arrays::valuesKeys2string($this->simpleArray, ' '),
'Simple array (with custom separator)'
);
self::assertSame(
'0="Lorem" 1="ipsum" 2="dolor" 3="sit" 4="amet"',
Arrays::valuesKeys2string($this->simpleArray, ' ', '=', '"'),
'Simple array (with custom separators)'
);
self::assertSame(
'0="Lorem", 1="ipsum", 2="dolor", 3="sit", 4="amet"',
Arrays::valuesKeys2string($this->simpleArray, ', ', '=', '"'),
'Simple array (with custom separators)'
);
}
public function testValues2csv()
/**
* @param string $description Description of test
* @param string $expected Expected array converted to csv string
* @param array $array Data to be converted. It have to be an array that represents database table.
* @param string $separator (optional) Separator used between values. Default: ",".
*
* @dataProvider provideArrayValues2csv
*/
public function testValues2csv($description, $expected, array $array, $separator = ',')
{
self::assertEquals('', Arrays::values2csv($this->simpleArray));
self::assertSame($expected, Arrays::values2csv($array, $separator), $description);
self::assertSame('', Arrays::values2csv($this->simpleArray), 'Simple array');
self::assertEquals("lorem,ipsum,dolor,sit,amet\n"
self::assertSame("lorem,ipsum,dolor,sit,amet\n"
. "consectetur,adipiscing,elit\n"
. 'donec,sagittis,fringilla,eleifend', Arrays::values2csv($this->twoDimensionsArray));
. 'donec,sagittis,fringilla,eleifend',
Arrays::values2csv($this->twoDimensionsArray),
'Two dimensions array'
);
}
public function testGetFirstKey()
@@ -93,6 +138,7 @@ class ArraysTest extends BaseTestCase
public function testGetLastKey()
{
self::assertNull(Arrays::getLastKey([]));
self::assertEquals(4, Arrays::getLastKey($this->simpleArray));
self::assertEquals('amet', Arrays::getLastKey($this->complexArray));
}
@@ -146,6 +192,7 @@ class ArraysTest extends BaseTestCase
public function testGetLastElementBreadCrumb()
{
self::assertNull(Arrays::getLastElementBreadCrumb([]));
self::assertEquals('4/amet', Arrays::getLastElementBreadCrumb($this->simpleArray));
self::assertEquals('2/3/eleifend', Arrays::getLastElementBreadCrumb($this->twoDimensionsArray));
self::assertEquals('amet/1/primis', Arrays::getLastElementBreadCrumb($this->complexArray));
@@ -279,6 +326,18 @@ letsTest[2] = value_2;';
self::assertEquals($effect, Arrays::array2JavaScript($this->twoDimensionsArray, 'letsTest', true));
}
/**
* @param string $description Description of test case
* @param array|null $expected Expected new array (with quoted elements)
* @param array $array The array to check for string values
*
* @dataProvider provideArrayToQuoteStrings
*/
public function testQuoteStrings($description, $expected, array $array)
{
self::assertSame($expected, Arrays::quoteStrings($array), $description);
}
public function testRemoveMarginalElement()
{
$array = $this->simpleArray;
@@ -343,7 +402,7 @@ letsTest[2] = value_2;';
public function testSetKeysAsValuesEmptyArray()
{
self::assertEquals([], Arrays::setKeysAsValues([]));
self::assertNull(Arrays::setKeysAsValues([]));
}
public function testSetKeysAsValuesSameKeysValues()
@@ -456,6 +515,7 @@ letsTest[2] = value_2;';
/*
* Negative cases
*/
self::assertFalse(Arrays::areKeysInArray([], []));
self::assertFalse(Arrays::areKeysInArray([null], $this->simpleArray));
self::assertFalse(Arrays::areKeysInArray([''], $this->simpleArray));
self::assertFalse(Arrays::areKeysInArray(['dolorrr'], $this->simpleArrayWithKeys));
@@ -520,7 +580,7 @@ letsTest[2] = value_2;';
public function testGetLastElementsPathsUsingEmptyArray()
{
self::assertSame([], Arrays::getLastElementsPaths([]));
self::assertNull(Arrays::getLastElementsPaths([]));
}
public function testGetLastElementsPathsUsingDefaults()
@@ -585,9 +645,9 @@ letsTest[2] = value_2;';
$pattern = '\d+';
/*
* Complex array with strings and integers as keys
* Empty array
*/
self::assertFalse(Arrays::areAllKeysMatchedByPattern($this->complexArray, $pattern));
self::assertFalse(Arrays::areAllKeysMatchedByPattern([], $pattern));
/*
* Simple array with integers as keys only
@@ -595,9 +655,9 @@ letsTest[2] = value_2;';
self::assertTrue(Arrays::areAllKeysMatchedByPattern($this->simpleArray, $pattern));
/*
* Empty array
* Complex array with strings and integers as keys
*/
self::assertFalse(Arrays::areAllKeysMatchedByPattern([], $pattern));
self::assertFalse(Arrays::areAllKeysMatchedByPattern($this->complexArray, $pattern));
$array = [
'a' => 'b',
@@ -638,6 +698,7 @@ letsTest[2] = value_2;';
public function testAreAllKeysIntegers()
{
self::assertFalse(Arrays::areAllKeysIntegers([]));
self::assertEquals(1, Arrays::areAllKeysIntegers($this->simpleArray));
self::assertEquals(2, Arrays::areAllKeysIntegers($this->simpleArray));
self::assertEquals('', Arrays::areAllKeysIntegers($this->complexArray));
@@ -885,8 +946,7 @@ letsTest[2] = value_2;';
/*
* Negative cases
*/
self::assertEmpty(Arrays::setPositions([]));
self::assertEquals([], Arrays::setPositions([]));
self::assertNull(Arrays::setPositions([]));
/*
* Positive case - 1-dimension array
@@ -983,7 +1043,7 @@ letsTest[2] = value_2;';
/*
* Negative cases
*/
self::assertEquals([], Arrays::trimRecursive([]));
self::assertSame([], Arrays::trimRecursive([]));
/*
* Positive cases
@@ -1125,7 +1185,7 @@ letsTest[2] = value_2;';
/*
* Empty array
*/
self::assertEmpty(Arrays::implodeSmart([], $separator));
self::assertNull(Arrays::implodeSmart([], $separator));
/*
* Simple, one-dimension array
@@ -1214,7 +1274,7 @@ letsTest[2] = value_2;';
/*
* Negative cases
*/
self::assertEquals([], Arrays::incrementIndexes([]));
self::assertNull(Arrays::incrementIndexes([]));
/*
* Positive cases
@@ -1454,7 +1514,7 @@ letsTest[2] = value_2;';
/*
* Basic cases
*/
self::assertEquals(1, Arrays::getDimensionsCount([]));
self::assertEquals(0, Arrays::getDimensionsCount([]));
self::assertEquals(1, Arrays::getDimensionsCount(['']));
/*
@@ -1487,6 +1547,11 @@ letsTest[2] = value_2;';
self::assertTrue(Arrays::isMultiDimensional($this->complexArray));
}
public function testGetNonEmptyValuesUsingEmptyArray()
{
self::assertNull(Arrays::getNonEmptyValues([]));
}
/**
* @param string $description Description of test case
* @param array $values The values to filter
@@ -1854,12 +1919,6 @@ letsTest[2] = value_2;';
{
$simpleObject = new SimpleToString('1234');
yield[
'An empty array (no values to filter)',
[],
[],
];
yield[
'All values are empty',
[
@@ -1955,7 +2014,7 @@ letsTest[2] = value_2;';
yield[
'An empty array (no values to filter)',
[],
'',
null,
];
yield[
@@ -2032,7 +2091,7 @@ letsTest[2] = value_2;';
'An empty array (no values to filter)',
[],
' | ',
'',
null,
];
yield[
@@ -2103,6 +2162,524 @@ letsTest[2] = value_2;';
];
}
public function provideArrayValuesKeysConverted2string()
{
yield[
'An empty array',
null,
[],
];
yield[
'Empty string and null as value',
'test_1=,test_2=,test_3=3',
[
'test_1' => null,
'test_2' => '',
'test_3' => '3',
],
];
yield[
'Empty string and null as value (with custom separators)',
'test_1="" test_2="" test_3="3"',
[
'test_1' => null,
'test_2' => '',
'test_3' => '3',
],
' ',
'=',
'"',
];
yield[
'Empty string as key',
'1=test_1,=test_2,3=test_3',
[
1 => 'test_1',
'' => 'test_2',
'3' => 'test_3',
],
];
yield[
'Empty string as key (with custom separators)',
'1 => "test_1"; => "test_2"; 3 => "test_3"',
[
1 => 'test_1',
'' => 'test_2',
'3' => 'test_3',
],
'; ',
' => ',
'"',
];
yield[
'Mixed types of keys and values',
'test_1=test test,test_2=2,test_3=3.45',
[
'test_1' => 'test test',
'test_2' => 2,
'test_3' => 3.45,
],
];
yield[
'Mixed types of keys and values (with custom separators)',
'test_1 --> *test test* | test_2 --> *2* | test_3 --> *3.45*',
[
'test_1' => 'test test',
'test_2' => 2,
'test_3' => 3.45,
],
' | ',
' --> ',
'*',
];
}
public function provideArrayValues2csv()
{
yield[
'An empty array',
null,
[],
];
yield[
'Empty string, and empty array and null as row',
"1,2,3\n5,6,",
[
'test_1' => '',
'test_2' => [],
'test_3' => null,
'test_4' => [
'aa' => 1,
'bb' => 2,
'cc' => 3,
],
[
'dd' => 5,
'ee' => 6,
'ff' => '',
],
],
];
yield[
'Empty string, and empty array and null as row (with custom separator)',
"1, 2, 3\n5, 6, ",
[
'test_1' => '',
'test_2' => [],
'test_3' => null,
'test_4' => [
'aa' => 1,
'bb' => 2,
'cc' => 3,
],
[
'dd' => 5,
'ee' => 6,
'ff' => '',
],
],
', ',
];
yield[
'Empty string as key, non-array as value',
"1,2,3\n5,6,",
[
'' => 'test_1',
1 => 'test_2',
'3' => [
'aa' => 1,
'bb' => 2,
'cc' => 3,
],
[
'dd' => 5,
'ee' => 6,
'ff' => '',
],
],
];
yield[
'Empty string as key, non-array as value (with custom separator)',
"1 | 2 | 3\n5 | 6 | ",
[
'' => 'test_1',
1 => 'test_2',
'3' => [
'aa' => 1,
'bb' => 2,
'cc' => 3,
],
[
'dd' => 5,
'ee' => 6,
'ff' => '',
],
],
' | ',
];
yield[
'Invalid structure, not like database table',
"1,2,3\n5,6\n7,8,9,10",
[
[
'aa' => 1,
'bb' => 2,
'cc' => 3,
],
[
'dd' => 5,
'ee' => 6,
],
[
7,
8,
9,
10,
],
],
];
yield[
'Invalid structure, not like database table (with custom separator)',
"1 <-> 2 <-> 3\n5 <-> 6\n7 <-> 8 <-> 9 <-> 10",
[
[
'aa' => 1,
'bb' => 2,
'cc' => 3,
],
[
'dd' => 5,
'ee' => 6,
],
[
7,
8,
9,
10,
],
],
' <-> ',
];
yield[
'Mixed types of keys and values',
"1,2,3.45\n5,6,\n7,8,9,,10",
[
[
'aa' => 1,
'bb' => 2,
'cc' => 3.45,
],
[
'dd' => 5,
'ee' => 6,
null,
],
[
7,
8,
'qq' => 9,
'',
10,
],
],
];
yield[
'Mixed types of keys and values (with custom separator)',
"1 // 2 // 3.45\n5 // 6 // \n7 // 8 // 9 // // 10",
[
[
'aa' => 1,
'bb' => 2,
'cc' => 3.45,
],
[
'dd' => 5,
'ee' => 6,
null,
],
[
7,
8,
'qq' => 9,
'',
10,
],
],
' // ',
];
}
public function provideArrayValues2string()
{
yield[
'An empty array',
null,
[],
];
yield[
'Simple array',
'Test 1,Test 2,Test 3',
[
1 => 'Test 1',
2 => 'Test 2',
3 => 'Test 3',
],
];
yield[
'Simple array (with custom separator)',
'Test 1.Test 2.Test 3',
[
1 => 'Test 1',
2 => 'Test 2',
3 => 'Test 3',
],
'',
'.',
];
yield[
'Simple array (concrete column)',
'Test 2',
[
1 => 'Test 1',
2 => 'Test 2',
3 => 'Test 3',
],
2,
];
yield[
'Simple array (concrete column with custom separator)',
'Test 2',
[
1 => 'Test 1',
2 => 'Test 2',
3 => 'Test 3',
],
2,
'.',
];
yield[
'Complex array',
'1,2,3,test 1,test 2,test 3,,test 4,,bbb,3.45',
[
[
1,
2,
3,
],
[
'test 1',
'test 2',
[
'test 3',
'',
'test 4',
],
],
[],
[
'a' => '',
'b' => 'bbb',
[],
'c' => 3.45,
],
],
];
yield[
'1st complex array (concrete column)',
'2,test 2,',
[
[
1,
2,
3,
],
[
'test 1',
'test 2',
[
'test 3',
'',
'test 4',
],
],
[],
[
'a' => '',
'b' => 'bbb',
[],
'c' => 3.45,
],
],
1,
];
yield[
'2nd complex array (concrete column)',
'bb,1234,0xb',
[
[
1,
2,
3,
],
[
'a' => 'aa',
'b' => 'bb',
'c' => 'cc',
],
[
'a',
'b',
'c',
],
[
'a' => '',
'b' => 1234,
],
[
'c' => 5678,
'b' => '0xb',
],
],
'b',
];
yield[
'3rd complex array (concrete column with custom separator)',
'bb - 1234 - 3xb - bbb',
[
[
1,
2,
3,
],
[
'a' => 'aa',
'b' => 'bb',
'c' => 'cc',
],
[
'a',
'b' => [],
'c',
],
[
'a' => '',
'b' => 1234,
],
[
'c' => 5678,
'b' => [
'b1' => '0xb',
'b2' => '1xb',
'b' => '3xb',
],
[
1,
2,
'a' => 'aaa',
'b' => 'bbb',
],
],
],
'b',
' - ',
];
}
public function provideArrayToQuoteStrings()
{
yield[
'An empty array',
null,
[],
];
yield[
'Simple array',
[
1,
2,
3,
'\'1\'',
'\'2\'',
],
[
1,
2,
3,
'1',
'2',
],
];
yield[
'Complex array',
[
123,
'\'456\'',
[
'x' => [
0,
'\'0\'',
1 => '\'1\'',
2 => 2,
],
'\'y\'',
],
444 => '\'\'',
[
[
[
'\'test\'',
],
],
],
],
[
123,
'456',
[
'x' => [
0,
'0',
1 => '1',
2 => 2,
],
'y',
],
444 => '',
[
[
[
'test',
],
],
],
],
];
}
/**
* {@inheritdoc}
*/