Compare commits

...

4 Commits

Author SHA1 Message Date
Clemens Schwaighofer
f275180571 Add bew array handler functions for various array interfaces
get random entry, remove array entry by value, add string to each entry in array, create sort by array key
2026-03-25 18:51:28 +09:00
Clemens Schwaighofer
188f0b57d0 Clean up .gitignore and add coverage option to phpunit.sh 2026-02-27 10:58:05 +09:00
Clemens Schwaighofer
b84fb5cef5 Comemit message fix for function param (phpdoc) 2026-02-17 18:16:16 +09:00
Clemens Schwaighofer
9a28c86991 json test file update 2026-01-22 11:51:32 +09:00
9 changed files with 112 additions and 11 deletions

6
.gitignore vendored
View File

@@ -1,10 +1,8 @@
.libs
node_modules/
composer.lock
vendor/
**/composer.lock
**/vendor/
tools/
www/composer.lock
www/vendor
**/.env
**/.target
package-lock.json

View File

@@ -19,6 +19,7 @@ Available options:
-v, --verbose Enable verbose output for PHPunit
-c, --composer Use composer version and not the default phives bundle
-p, --php VERSION Chose PHP version in the form of "N.N", if not found will exit
-C, --coverage Generate code coverage report in text format (default: disabled)
EOF
exit
}
@@ -47,6 +48,7 @@ opt_verbose="";
php_version="";
no_php_version=0;
use_composer=0;
opt_generate_coverage="";
while [ -n "${1-}" ]; do
case "${1}" in
-t | --testdox)
@@ -59,6 +61,10 @@ while [ -n "${1-}" ]; do
use_composer=1;
shift
;;
-C | --coverage)
opt_generate_coverage="--coverage-text";
shift
;;
-p | --php)
php_version="${2-}";
shift
@@ -100,9 +106,14 @@ if [ "${use_composer}" -eq 1 ]; then
else
PHPUNIT_CALL+=("${BASE_PATH}tools/phpunit");
fi;
if [ -n "${opt_generate_coverage}" ]; then
echo "Will run coverage report";
export XDEBUG_MODE=coverage
fi;
PHPUNIT_CALL+=(
"${opt_testdox}"
"${opt_verbose}"
"${opt_generate_coverage}"
"-c" "${PHPUNIT_CONFIG}"
"${BASE_PATH}4dev/tests/"
);

View File

@@ -45,7 +45,6 @@ use CoreLibs\DB\Support\ConvertPlaceholder;
* Test class for DB\IO + DB\SQL\PgSQL
* This will only test the PgSQL parts
* @coversDefaultClass \CoreLibs\DB\IO
* @coversDefaultClass \CoreLibs\DB\SQL\PgSQL
* @testdox \CoreLibs\DB\IO method tests for SQL\PgSQL
*/
final class CoreLibsDBIOTest extends TestCase

View File

@@ -1,7 +1,7 @@
<?php
$set = 0;
foreach (['/../../www', '/../www', '/../..', '/..', '/../../src', '/../src'] as $src) {
foreach (['/../../www', '/../www', '/../../src', '/../src', '/../..', '/..'] as $src) {
if (is_file(dirname(__DIR__) . $src . '/vendor/autoload.php')) {
require dirname(__DIR__) . $src . '/vendor/autoload.php';
$set = 1;

View File

@@ -10,4 +10,14 @@
<directory>4dev/tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./www/lib/CoreLibs</directory>
</include>
</coverage>
<!-- <source>
<include>
<directory>./www/lib/CoreLibs</directory>
</include>
</source> -->
</phpunit>

View File

@@ -59,8 +59,9 @@ $output = $json_class::jsonConvertToArray($json);
print "J/S::E-JSON: $json: " . DgS::printAr($output) . "<br>";
print "J/S::E-JSON ERROR: " . $json_class::jsonGetLastError() . ": " . $json_class::jsonGetLastError(true) . "<br>";
echo "<hr>";
$json = '{"valid":"json","invalid":"\xB1\x31"}';
$json = '{"valid":"json","invalid":"abc\x80def"}';
// $json = '{"valid":"json","invalid":"abc\x80def"}';
$output_no_flag = Json::jsonConvertToArray($json);
print "No Flag JSON: $json: " . DgS::printAr($output_no_flag) . "<br>";
print "No Flag JSON ERROR: " . Json::jsonGetLastError() . ": " . Json::jsonGetLastError(true) . "<br>";
@@ -73,6 +74,7 @@ $output_raw = json_decode($json, true, flags:JSON_INVALID_UTF8_SUBSTITUTE);
print "No Flag JSON RAW (F-2): $json: " . DgS::printAr($output_raw) . "<br>";
$output_raw = json_decode($json, true);
print "No Flag JSON RAW: $json: " . DgS::printAr($output_raw) . "<br>";
echo "<hr>";
// $json = '{"foo": "bar"}';

View File

@@ -15,9 +15,9 @@
]
},
"repositories": {
"git.egplusww.jp.Composer": {
"packages.omnicomproduction.jp.Composer": {
"type": "composer",
"url": "https://git.egplusww.jp/api/packages/Composer/composer"
"url": "https://packages.omnicomproduction.jp/api/packages/Composer/composer"
}
},
"require": {

View File

@@ -811,6 +811,87 @@ class ArrayHandler
);
return $in_array;
}
/**
* TODO: move to CoreLibs ArrayCombined
* return one random entry from an array (value)
* the return entry is null if the incoming array is empty
* the array can also be an array of arrays
*
* @param array<mixed> $array
* @return string|int|float|bool|array<mixed>|null
*/
public static function getRandomEntryFromArray(array $array): string|int|float|bool|array|null
{
if (empty($array)) {
return null;
}
$random_index = array_rand($array);
return $array[$random_index];
}
/**
* TODO: move to CoreLibs ArrayCombined
* find and remove one value
* on default removes only the first entry
* on default does not strict compare
*
* @param array<mixed> $array
* @param string|int|float|bool|array<mixed> $value
* @param bool $strict [false]
* @return array<mixed>
*/
public static function removeArrayEntryByValue(
array $array,
string|int|float|bool|array $value,
bool $strict = false,
): array {
return array_filter($array, function ($element) use ($value, $strict) {
return $strict ?
$element !== $value :
$element != $value;
});
}
/**
* TODO: move to CoreLibs ArrayCombined
* add a string to each element in an array, default is at the end of the value,
* can be switched with the prefix flag
*
* @param array<string> $array
* @param string $string_to_add
* @param bool $prefix [false]
* @return array<string>
*/
public static function addStringToEachValueInArray(array $array, string $string_to_add, bool $prefix = false): array
{
return array_map(function ($value) use ($string_to_add, $prefix) {
return $prefix ?
$string_to_add . $value :
$value . $string_to_add;
}, $array);
}
/**
* sort by key any array, even if it is nested
*
* @param array<mixed> $data
* @return array<mixed>
*/
public static function createSortedArrayByKey(array $data): array
{
// Sort by keys
ksort($data);
// Recursively sort nested arrays
foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = self::createSortedArrayByKey($value);
}
}
return $data;
}
}
// __END__

View File

@@ -134,8 +134,8 @@ class Support
* Convert bool value to string value. Short name alias for printBool
*
* @param bool $bool Bool value to be transformed
* @param string $true [='true'] Override default string 'true'
* @param string $false [=false'] Override default string 'false'
* @param string $true [=true] Override default string 'true'
* @param string $false [=false] Override default string 'false'
* @return string $true or $false string for true/false bool
*/
public static function prBl(