PHP 8.1 base version update

This commit is contained in:
Clemens Schwaighofer
2023-02-28 18:04:42 +09:00
parent 1d220f25eb
commit 0a80abe8a4
30 changed files with 528 additions and 480 deletions

View File

@@ -21,12 +21,12 @@ class ArrayHandler
* the needle can be found in the
* haystack array
*/
public static function arraySearchRecursive($needle, array $haystack, ?string $key_search_for = null): array
{
public static function arraySearchRecursive(
string|int $needle,
array $haystack,
?string $key_search_for = null
): array {
$path = [];
if (!is_array($haystack)) {
$haystack = [];
}
if (
$key_search_for != null &&
array_key_exists($key_search_for, $haystack) &&
@@ -72,7 +72,7 @@ class ArrayHandler
*
* @param string|int $needle needle (search for)
* @param array<mixed> $haystack haystack (search in)
* @param string|int $key_search_for the key to look for in
* @param string|int|null $key_search_for the key to look for in
* @param bool $old [true], if set to false will
* return new flat layout
* @param array<mixed>|null $path recursive call for previous path
@@ -80,9 +80,9 @@ class ArrayHandler
* the element was found
*/
public static function arraySearchRecursiveAll(
$needle,
string|int $needle,
array $haystack,
$key_search_for,
string|int|null $key_search_for,
bool $old = true,
?array $path = null
): ?array {
@@ -101,10 +101,6 @@ class ArrayHandler
$path['work'] = [];
}
}
// should not be needed because it would trigger a php mehtod error
if (!is_array($haystack)) {
$haystack = [];
}
// go through the array,
foreach ($haystack as $_key => $_value) {
@@ -152,17 +148,18 @@ class ArrayHandler
* array search simple. looks for key, value combination, if found, returns true
* on default does not strict check, so string '4' will match int 4 and vica versa
*
* @param array<mixed> $array search in as array
* @param string|int $key key (key to search in)
* @param string|int $value value (what to find)
* @param bool $strict [false], if set to true, will strict check key/value
* @return bool true on found, false on not found
* @param array<mixed> $array search in as array
* @param string|int $key key (key to search in)
* @param string|int|bool $value value (what to find)
* @param bool $strict [false], if set to true, will strict check key/value
* @return bool true on found, false on not found
*/
public static function arraySearchSimple(array $array, $key, $value, bool $strict = false): bool
{
if (!is_array($array)) {
$array = [];
}
public static function arraySearchSimple(
array $array,
string|int $key,
string|int|bool $value,
bool $strict = false
): bool {
foreach ($array as $_key => $_value) {
// if value is an array, we search
if (is_array($_value)) {
@@ -189,9 +186,9 @@ class ArrayHandler
* bool key flag: true: handle keys as string or int
* default false: all keys are string
*
* @return array<mixed>|bool merged array
* @return array<mixed>|false merged array
*/
public static function arrayMergeRecursive()
public static function arrayMergeRecursive(): array|false
{
// croak on not enough arguemnts (we need at least two)
if (func_num_args() < 2) {
@@ -264,10 +261,10 @@ class ArrayHandler
* @param array<mixed> $needle elements to search for
* @param array<mixed> $haystack array where the $needle elements should
* be searched int
* @return array<mixed>|bool either the found elements or
* @return array<mixed>|false either the found elements or
* false for nothing found or error
*/
public static function inArrayAny(array $needle, array $haystack)
public static function inArrayAny(array $needle, array $haystack): array|false
{
$found = [];
foreach ($needle as $element) {
@@ -291,8 +288,12 @@ class ArrayHandler
* @param bool $set_only flag to return all (default), or set only
* @return array<mixed> associative array
*/
public static function genAssocArray(array $db_array, $key, $value, bool $set_only = false): array
{
public static function genAssocArray(
array $db_array,
string|int|bool $key,
string|int|bool $value,
bool $set_only = false
): array {
$ret_array = [];
// do this to only run count once
for ($i = 0, $iMax = count($db_array); $i < $iMax; $i++) {
@@ -385,11 +386,8 @@ class ArrayHandler
* and will be pushed up
* @return array<mixed> modified, flattened array
*/
public static function arrayFlatForKey(array $array, $search): array
public static function arrayFlatForKey(array $array, string|int $search): array
{
if (!is_array($array)) {
$array = [];
}
foreach ($array as $key => $value) {
// if it is not an array do just nothing
if (!is_array($value)) {

View File

@@ -74,7 +74,7 @@ class DateTime
* @return string formated date+time in Y-M-D h:m:s ms
*/
public static function dateStringFormat(
$timestamp,
int|float $timestamp,
bool $show_micro = false,
bool $micro_as_float = false
): string {
@@ -100,8 +100,10 @@ class DateTime
* @param bool $show_micro show micro seconds, default true
* @return string interval formatted string or string as is
*/
public static function timeStringFormat($timestamp, bool $show_micro = true): string
{
public static function timeStringFormat(
string|int|float $timestamp,
bool $show_micro = true
): string {
// check if the timestamp has any h/m/s/ms inside, if yes skip
if (!preg_match("/(h|m|s|ms)/", (string)$timestamp)) {
list($timestamp, $ms) = array_pad(explode('.', (string)round((float)$timestamp, 4)), 2, null);
@@ -157,7 +159,7 @@ class DateTime
* @param string|int|float $timestring formatted interval
* @return string|int|float converted float interval, or string as is
*/
public static function stringToTime($timestring)
public static function stringToTime(string|int|float $timestring): string|int|float
{
$timestamp = 0;
if (preg_match("/(d|h|m|s|ms)/", (string)$timestring)) {
@@ -247,9 +249,9 @@ class DateTime
* @param string $date a date string in the format YYYY-MM-DD
* @return bool true if valid date, false if date not valid
*/
public static function checkDate($date): bool
public static function checkDate(string $date): bool
{
if (!$date) {
if (empty($date)) {
return false;
}
list ($year, $month, $day) = array_pad(
@@ -324,7 +326,7 @@ class DateTime
* @return int|bool false on error
* or int -1 (s<e)/0 (s=e)/1 (s>e) as difference
*/
public static function compareDate($start_date, $end_date)
public static function compareDate(string $start_date, string $end_date): int|bool
{
// pre check for empty or wrong
if ($start_date == '--' || $end_date == '--' || !$start_date || !$end_date) {
@@ -367,7 +369,7 @@ class DateTime
* @return int|bool false for error
* or -1 (s<e)/0 (s=e)/1 (s>e) as difference
*/
public static function compareDateTime($start_datetime, $end_datetime)
public static function compareDateTime(string $start_datetime, string $end_datetime): int|bool
{
// pre check for empty or wrong
if ($start_datetime == '--' || $end_datetime == '--' || !$start_datetime || !$end_datetime) {
@@ -402,8 +404,11 @@ class DateTime
* @param bool $return_named return array type, false (default), true for named
* @return array<mixed> 0/overall, 1/weekday, 2/weekend
*/
public static function calcDaysInterval($start_date, $end_date, bool $return_named = false): array
{
public static function calcDaysInterval(
string $start_date,
string $end_date,
bool $return_named = false
): array {
// pos 0 all, pos 1 weekday, pos 2 weekend
$days = [];
// if anything invalid, return 0,0,0