Commit f41c915b authored by Carsten  Rose's avatar Carsten Rose
Browse files

OnArray.php: Klasse FromArray wurde umbenannt zu OnArray. Neue Funktion sortKey() hinzugefuegt.

parent 24ff6f6e
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/10/16
* Time: 11:47 AM
*/
namespace qfq\helper;
require_once(__DIR__ . '/../../qfq/Constants.php');
class OnArray {
/**
* @param array $dataArray
* @param string $keyValueGlue
* @param string $rowGlue
* @return string
*/
public static function toString(array $dataArray, $keyValueGlue = '=', $rowGlue = '&') {
$dataString = '';
foreach ($dataArray as $key => $value) {
$dataString .= $key . $keyValueGlue . $value . $rowGlue;
}
$glueLength = strlen($rowGlue);
return substr($dataString, 0, strlen($dataString) - $glueLength);
}
/**
* Sort array by array keys
*
* @param array $a
*/
public static function sortKey(array &$a) {
$result = array();
$keys = array_keys($a);
sort($keys);
foreach ($keys as $key) {
$result[$key] = $a[$key];
}
$a = $result;
return;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/12/16
* Time: 9:28 AM
*/
namespace qfq\helper;
require_once(__DIR__ . '/../../qfq/helper/OnArray.php');
class OnArrayTest extends \PHPUnit_Framework_TestCase {
public function testSortByKey() {
$unsorted = [
'a' => 'z',
'1' => 'y',
'Aa' => 'y0',
'zZ' => 'X9',
'0000' => 'h'
];
$sorted = [
'0000' => 'h',
'1' => 'y',
'Aa' => 'y0',
'a' => 'z',
'zZ' => 'X9'
];
$expected = $unsorted;
\qfq\helper\OnArray::sortKey($expected);
$this->assertEquals($sorted, $expected);
$expected = array();
\qfq\helper\OnArray::sortKey($expected);
$this->assertEquals(array(), $expected);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment