Убрано дублирование классов. Helpers уехали из Core. Классы во внешних директориях переехали к остальным.

This commit is contained in:
2023-08-14 18:15:05 +03:00
parent 81a8161d32
commit 0d2b4aba63
114 changed files with 12919 additions and 13151 deletions
+71
View File
@@ -0,0 +1,71 @@
<?php
namespace Helper;
class ArraySorter
{
/**
* Groups an array by a given key.
*
* Groups an array into arrays by a given key, or set of keys, shared between all array members.
*
* Based on {@author Jake Zatecky}'s {@link https://github.com/jakezatecky/array_group_by array_group_by()} function.
* This variant allows $key to be closures.
*
* @param array $array The array to have grouping performed on.
* @param mixed $key,... The key to group or split by. Can be a _string_,
* an _integer_, a _float_, or a _callable_.
*
* If the key is a callback, it must return
* a valid key from the array.
*
* If the key is _NULL_, the iterated element is skipped.
*
* ```
* string|int callback ( mixed $item )
* ```
*
* @return array|null Returns a multidimensional array or `null` if `$key` is invalid.
*/
public static function groupBy(array $array, $key): ?array
{
if (!is_string($key) && !is_int($key) && !is_float($key) && !is_callable($key)) {
return null;
}
$func = (!is_string($key) && is_callable($key) ? $key : null);
$key2 = $key;
// Load the new array, splitting by the target key
$grouped = [];
foreach ($array as $value) {
$key = null;
if (is_callable($func)) {
$key = call_user_func($func, $value);
} elseif (is_object($value) && property_exists($value, $key2)) {
$key = $value->{$key2};
} elseif (isset($value[$key2])) {
$key = $value[$key2];
}
if ($key === null) {
continue;
}
$grouped[$key][] = $value;
}
// Recursively build a nested grouping if more parameters are supplied
// Each grouped array value is grouped according to the next sequential key
if (func_num_args() > 2) {
$args = func_get_args();
foreach ($grouped as $key => $value) {
$params = array_merge([$value], array_slice($args, 2, func_num_args()));
$grouped[$key] = call_user_func_array('array_group_by', $params);
}
}
return $grouped;
}
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace Helper;
class Comparsion
{
public static function minimax($value, $minimum, $maximum)
{
if ($value < $minimum) {
$value = $minimum;
}
if ($value > $maximum) {
$value = $maximum;
}
return $value;
}
}
+61
View File
@@ -0,0 +1,61 @@
<?php
namespace Helper;
class Conversion
{
/** Превращает строку data ('a=1|b=2|c=3') из БД в массив [a=>1, b=>2, c=>3].
* @param string $dataString
* @return array
*/
public static function dataStringToArray(string $dataString): array
{
if (mb_substr($dataString, -1) === '|') {
$dataString = rtrim($dataString, '|');
}
$arr = json_decode(str_replace(['=', '|'], ['":', ',"'], '{"' . $dataString . '}'), true);
return $arr ?: [];
}
/** Превращает массив [a=>1, b=>2, c=>3] в строку data ('a=1|b=2|c=3') для БД.
* @param array $dataArray
* @return string
*/
public static function arrayToDataString(array $dataArray): string
{
$str = json_encode($dataArray);
return $str ? str_replace(['":', ',"', '{"', '}'], ['=', '|'], $str) : '';
}
/** Превращает количество секунд в человекопонятное Х мес. Х дн. Х ч. Х мин. Х сек.,
* используемое обычно для отображения игровых таймаутов.
* @param int|string $seconds
* @return string
*/
public static function secondsToTimeout($seconds): string
{
$seconds = (int)$seconds;
$time = new \DateTime();
$time->setTimestamp($seconds);
$sec = intval($time->format('s'));
$min = intval($time->format('i'));
$hr = intval($time->format('G'));
$day = intval($time->format('j'));
$month = intval($time->format('n'));
$timeout = '';
if ($month > 1) {
$timeout .= $month . ' мес. ';
}
if ($day > 1) {
$timeout .= $day . ' дн. ';
}
if ($hr) {
$timeout .= $hr . ' ч. ';
}
if ($sec && !$min) {
$timeout .= $sec . ' сек. ';
} elseif ($min) {
$timeout .= $min . ' мин. ';
}
return $timeout;
}
}
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace Helper;
/** All raw mathematics in one place. */
class Math
{
/**
* @param float|int $total
* @param int|null $number
* @return float
*/
public static function getPercentage($total, ?int $number): float
{
if (is_null($number)) {
return 0;
}
return $total > 0 ? round(($number * 100) / $total, 2) : 0;
}
public static function get100Percentage($total, ?int $number)
{
if (is_null($number)) {
return 0;
}
return min(self::getPercentage($total, $number), 100);
}
/** Number-20% and Number+20% */
public static function get20PercentRange($number): array
{
return [
'min' => $number * ((100 - 20) / 100),
'max' => $number * ((100 + 20) / 100),
];
}
public static function addPercent($num, $percent)
{
return $num + (($percent / 100) * $num);
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace Helper;
class Table
{
public static function get($rows, $class = '', $fill = false): string
{
$c = '';
$maxRows = sizeof(max($rows));
foreach ($rows as $row) {
if ($fill && sizeof($row) < $maxRows) {
$row = array_merge($row, array_fill(0, $maxRows - sizeof($row), ''));
}
$c .= '<tr><td>' . implode('</td><td>', $row) . '</td></tr>';
}
return (!empty($class) ? "<table class='$class'>" : '<table>') . $c . '</table>' . PHP_EOL;
}
}