Compare commits

..

2 Commits

Author SHA1 Message Date
Clemens Schwaighofer 71e34848d9 Merge branch 'development' into feature/TTD-2662/function-add-to-ArrayCombined 2026-04-17 18:57:59 +09:00
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
12 changed files with 110 additions and 82 deletions
+3 -3
View File
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive"> <phive xmlns="https://phar.io/phive">
<phar name="phpunit" version="~9.6" installed="9.6.34" location="./tools/phpunit" copy="false"/> <phar name="phpunit" version="~9.6" installed="9.6.31" location="./tools/phpunit" copy="false"/>
<phar name="phpcbf" version="4" installed="4.0.1" location="./tools/phpcbf" copy="false"/> <phar name="phpcbf" version="4" installed="4.0.1" location="./tools/phpcbf" copy="false"/>
<phar name="phpcs" version="4" installed="4.0.1" location="./tools/phpcs" copy="false"/> <phar name="phpcs" version="4" installed="4.0.1" location="./tools/phpcs" copy="false"/>
<phar name="phpstan" version="^2.0" installed="2.1.54" location="./tools/phpstan" copy="false"/> <phar name="phpstan" version="^2.0" installed="2.1.33" location="./tools/phpstan" copy="false"/>
<phar name="phan" version="^5.4.3" installed="5.5.2" location="./tools/phan" copy="false"/> <phar name="phan" version="^5.4.3" installed="5.5.2" location="./tools/phan" copy="false"/>
<phar name="psalm" version="^5.26.1" installed="5.26.1" location="./tools/psalm" copy="false"/> <phar name="psalm" version="^5.26.1" installed="5.26.1" location="./tools/psalm" copy="false"/>
<phar name="phpdox" version="^0.12.0" installed="0.12.0" location="./tools/phpdox" copy="false"/> <phar name="phpdox" version="^0.12.0" installed="0.12.0" location="./tools/phpdox" copy="false"/>
<phar name="phpdocumentor" version="^3.4.2" installed="3.9.1" location="./tools/phpDocumentor" copy="false"/> <phar name="phpdocumentor" version="^3.4.2" installed="3.9.1" location="./tools/phpDocumentor" copy="false"/>
<phar name="php-cs-fixer" version="^3.34.1" installed="3.95.1" location="./tools/php-cs-fixer" copy="false"/> <phar name="php-cs-fixer" version="^3.34.1" installed="3.92.4" location="./tools/php-cs-fixer" copy="false"/>
</phive> </phive>
@@ -277,38 +277,6 @@ final class CoreLibsConvertJsonTest extends TestCase
); );
} }
/**
* Check if invalid flag throws exception
*
* @covers ::jsonValidate
* @testdox jsonValidate test exception thrown on invalid flag
*
* @return void
*/
public function testJsonValidateInvalidFlag(): void
{
$this->expectException("\InvalidArgumentException");
\CoreLibs\Convert\Json::jsonValidate('{"valid": "json"}', 5);
}
/**
* Check if valid flag throws no exception
*
* @covers ::jsonValidate
* @testdox jsonValidate test no exception on valid flag
*
* @return void
*/
public function testJsonValidateValidflag(): void
{
$this->assertTrue(
\CoreLibs\Convert\Json::jsonValidate('{"valid": "json"}', 0)
);
$this->assertTrue(
\CoreLibs\Convert\Json::jsonValidate('{"valid": "json"}', JSON_INVALID_UTF8_IGNORE)
);
}
/** /**
* Undocumented function * Undocumented function
* *
-5
View File
@@ -38,11 +38,6 @@ print "GETFILENAMEENDING: $file: " . File::getFilenameEnding($file) . "<br>";
$file = getcwd() . DIRECTORY_SEPARATOR . 'class_test.file.php'; $file = getcwd() . DIRECTORY_SEPARATOR . 'class_test.file.php';
print "GETLINESFROMFILE: $file: " . File::getLinesFromFile($file) . "<br>"; print "GETLINESFROMFILE: $file: " . File::getLinesFromFile($file) . "<br>";
print "MIMEINFO: $file: " . File::getMimeType($file) . "<br>"; print "MIMEINFO: $file: " . File::getMimeType($file) . "<br>";
try {
print "MIMEINFO: $file: " . File::getMimeType("does_not_exists.txt") . "<br>";
} catch (\Exception $e) {
print "Error: " . get_class($e) . ": " . $e->getMessage() . "<br>";
}
print "</body></html>"; print "</body></html>";
+2 -2
View File
@@ -15,7 +15,7 @@
] ]
}, },
"repositories": { "repositories": {
"git.egplusww.jp.Composer": { "packages.omnicomproduction.jp.Composer": {
"type": "composer", "type": "composer",
"url": "https://packages.omnicomproduction.jp/api/packages/Composer/composer" "url": "https://packages.omnicomproduction.jp/api/packages/Composer/composer"
} }
@@ -23,7 +23,7 @@
"require": { "require": {
"egrajp/smarty-extended": "^5.4", "egrajp/smarty-extended": "^5.4",
"php": ">=8.1", "php": ">=8.1",
"gullevek/dotenv": "^3.0", "gullevek/dotenv": "^2.0",
"psr/log": "^2.0 || ^3.0", "psr/log": "^2.0 || ^3.0",
"php-privacy/openpgp": "^2.1" "php-privacy/openpgp": "^2.1"
} }
+3 -9
View File
@@ -58,21 +58,15 @@ class File
* else returns '' for any other finfo read problem * else returns '' for any other finfo read problem
* *
* @param string $read_file File to read, relative or absolute path * @param string $read_file File to read, relative or absolute path
* @return string mime type * @return string
* @throws \UnexpectedValueException if file cannot be read or is not a file
* @throws \RangeException if we cannot get a mime type and throw exception is on
*/ */
public static function getMimeType(string $read_file): string public static function getMimeType(string $read_file): string
{ {
$finfo = new \finfo(FILEINFO_MIME_TYPE); $finfo = new \finfo(FILEINFO_MIME_TYPE);
if (!is_file($read_file) || !is_readable($read_file)) { if (!is_file($read_file)) {
throw new \UnexpectedValueException('[getMimeType] File not found: ' . $read_file); throw new \UnexpectedValueException('[getMimeType] File not found: ' . $read_file);
} }
$mime_type = $finfo->file($read_file); return $finfo->file($read_file) ?: '';
if ($mime_type === false) {
throw new \RangeException('[getMimeType] Cannot get mime type for: ' . $read_file);
}
return $mime_type;
} }
} }
@@ -811,6 +811,87 @@ class ArrayHandler
); );
return $in_array; 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__ // __END__
+2 -2
View File
@@ -133,7 +133,7 @@ class DateTime
} else { } else {
for ($i = 0, $iMax = count($timegroups); $i < $iMax; $i++) { for ($i = 0, $iMax = count($timegroups); $i < $iMax; $i++) {
$output = floor((float)$timestamp / $timegroups[$i]); $output = floor((float)$timestamp / $timegroups[$i]);
$timestamp = $timestamp % $timegroups[$i]; $timestamp = (float)$timestamp % $timegroups[$i];
// output has days|hours|min|sec // output has days|hours|min|sec
if ($output || $time_string) { if ($output || $time_string) {
$time_string .= $output . $labels[$i] . (($i + 1) != count($timegroups) ? ' ' : ''); $time_string .= $output . $labels[$i] . (($i + 1) != count($timegroups) ? ' ' : '');
@@ -453,7 +453,7 @@ class DateTime
} }
if (isset($matches[11]) && is_numeric($matches[11])) { if (isset($matches[11]) && is_numeric($matches[11])) {
// for milliseconds, we need to divide by 1000 and add them // for milliseconds, we need to divide by 1000 and add them
$timestamp += ((float)$matches[11] / 1000); $timestamp += (float)($matches[11] / 1000);
} }
if ($negative) { if ($negative) {
// cast to float so we can do a negative multiplication // cast to float so we can do a negative multiplication
+1 -1
View File
@@ -154,7 +154,7 @@ class Byte
if (!empty($matches[1])) { if (!empty($matches[1])) {
$number_negative = true; $number_negative = true;
} }
if (isset($matches[2])) { if (isset($matches[2]) && isset($matches[3])) {
// remove all non valid characters from the number // remove all non valid characters from the number
$number = preg_replace('/[^0-9\.]/', '', $matches[2]); $number = preg_replace('/[^0-9\.]/', '', $matches[2]);
// final clean up and convert to float // final clean up and convert to float
+6 -6
View File
@@ -957,9 +957,9 @@ class Color
* Undocumented function * Undocumented function
* *
* @param HWB $hwb * @param HWB $hwb
* @return LCH * @return Lch
*/ */
public static function hwbToLch(HWB $hwb): LCH public static function hwbToLch(HWB $hwb): Lch
{ {
return self::rgbToLch( return self::rgbToLch(
self::hwbToRgb($hwb) self::hwbToRgb($hwb)
@@ -1078,10 +1078,10 @@ class Color
/** /**
* OkLab to Lch (CIE) * OkLab to Lch (CIE)
* *
* @param Lab $lab * @param LAB $lab
* @return LCH * @return LCH
*/ */
public static function okLabToLch(Lab $lab): LCH public static function okLabToLch(LAB $lab): LCH
{ {
return self::labToLch( return self::labToLch(
self::okLabToLab($lab) self::okLabToLab($lab)
@@ -1092,9 +1092,9 @@ class Color
* Lch (CIE) to OkLab * Lch (CIE) to OkLab
* *
* @param LCH $lch * @param LCH $lch
* @return Lab * @return LAB
*/ */
public static function lchToOkLab(LCH $lch): Lab public static function lchToOkLab(LCH $lch): LAB
{ {
return self::labToOkLab( return self::labToOkLab(
self::lchToLab($lch) self::lchToLab($lch)
-4
View File
@@ -76,13 +76,9 @@ class Json
* @param string $json * @param string $json
* @param int $flags only JSON_INVALID_UTF8_IGNORE is currently allowed * @param int $flags only JSON_INVALID_UTF8_IGNORE is currently allowed
* @return bool * @return bool
* @throws \InvalidArgumentException if flags is invalid
*/ */
public static function jsonValidate(string $json, int $flags = 0): bool public static function jsonValidate(string $json, int $flags = 0): bool
{ {
if ($flags != 0 && $flags != JSON_INVALID_UTF8_IGNORE) {
throw new \InvalidArgumentException("For flag only 0 or JSON_INVALID_UTF8_IGNORE is allwed: " . $flags);
}
$json_valid = json_validate($json, flags:$flags); $json_valid = json_validate($json, flags:$flags);
self::$json_last_error = json_last_error(); self::$json_last_error = json_last_error();
return $json_valid; return $json_valid;
+6 -12
View File
@@ -2562,27 +2562,21 @@ class IO
// set field names // set field names
$this->cursor_ext[$query_hash]['field_names'] = []; $this->cursor_ext[$query_hash]['field_names'] = [];
for ($i = 0; $i < $this->cursor_ext[$query_hash]['num_fields']; $i++) { for ($i = 0; $i < $this->cursor_ext[$query_hash]['num_fields']; $i++) {
if ( $this->cursor_ext[$query_hash]['field_names'][] =
($field_name = $this->db_functions->__dbFieldName( $this->db_functions->__dbFieldName(
$this->cursor_ext[$query_hash]['cursor'], $this->cursor_ext[$query_hash]['cursor'],
$i $i
)) !== false );
) {
$this->cursor_ext[$query_hash]['field_names'][] = $field_name;
}
} }
$this->field_names = $this->cursor_ext[$query_hash]['field_names']; $this->field_names = $this->cursor_ext[$query_hash]['field_names'];
// field types // field types
$this->cursor_ext[$query_hash]['field_types'] = []; $this->cursor_ext[$query_hash]['field_types'] = [];
for ($i = 0; $i < $this->cursor_ext[$query_hash]['num_fields']; $i++) { for ($i = 0; $i < $this->cursor_ext[$query_hash]['num_fields']; $i++) {
if ( $this->cursor_ext[$query_hash]['field_types'][] =
($field_type = $this->db_functions->__dbFieldType( $this->db_functions->__dbFieldType(
$this->cursor_ext[$query_hash]['cursor'], $this->cursor_ext[$query_hash]['cursor'],
$i $i
)) !== false );
) {
$this->cursor_ext[$query_hash]['field_types'][] = $field_type;
}
} }
$this->field_types = $this->cursor_ext[$query_hash]['field_types']; $this->field_types = $this->cursor_ext[$query_hash]['field_types'];
// combined name => type // combined name => type
+5 -5
View File
@@ -178,11 +178,11 @@ class Elements
"/(href=\")?(\>)?\b($protRegex)([\w\.\-?&=+%#~,;\/]+)\b([\.\-?&=+%#~,;\/]*)(\|([^\||^#]+)(#([^\|]+))?\|)?/", "/(href=\")?(\>)?\b($protRegex)([\w\.\-?&=+%#~,;\/]+)\b([\.\-?&=+%#~,;\/]*)(\|([^\||^#]+)(#([^\|]+))?\|)?/",
function ($matches) { function ($matches) {
return self::createUrl( return self::createUrl(
$matches[1], $matches[1] ?? '',
$matches[2], $matches[2] ?? '',
$matches[3], $matches[3] ?? '',
$matches[4], $matches[4] ?? '',
$matches[5], $matches[5] ?? '',
$matches[7] ?? '', $matches[7] ?? '',
$matches[9] ?? '' $matches[9] ?? ''
); );