Skip to content
Snippets Groups Projects

Ptps controller 6

Merged Адлан Шамавов requested to merge PTPS_Controller_6 into main
4 unresolved threads
Files
6
+ 76
0
@@ -4,6 +4,12 @@ declare(strict_types=1);
namespace App\Action;
use Exception;
use DateTimeImmutable;
use DateTime;
use DatePeriod;
use DateInterval;
class Functions
{
/**
@@ -44,4 +50,74 @@ class Functions
}
return null;
}
/**
* Удалить дубликаты, оставив только уникальные значения
* @param array $array
* @return array
*/
public function uniqElements(array $array): array
{
return array_unique($array, SORT_REGULAR);
}
/**
* Сгруппировать подразедлы в верхние разделы меню
* Дочерние элементы поместить в массив родителя с ключом submenu
* Значение под ключом depth определяет уровень раздела
* Массив $aMenu всегда начинается с элемента depth = 0,
* все последующие элементы с depth = 1 являются его дочерними
* элементами
* @param array $aMenu
* @return array
*/
public function prepareMenu(array $aMenu): array
{
$result = [];
foreach ($aMenu as $arr) {
if ($arr['depth'] == 0) {
$result[] = array(
'name' => $arr['name'],
'depth' => $arr['depth'],
'submenu' => []
);
continue;
}
$result[array_key_last($result)]['submenu'][] = array(
'name' => $arr['name'],
'depth' => $arr['depth'],
);
}
return $result;
}
/**
* Функция рассчитывает кол-во дней до нового года
* @param DateTimeImmutable $date дата от которой, необходимо рассчитать кол-во дней
* @return int
* @throws Exception
*/
public function howDaysToNy(DateTimeImmutable $date): int
{
$endYear = date("Y-12-31", date_timestamp_get($date));
$dateInterval = date_diff(new DateTimeImmutable($endYear), $date);
return (int)$dateInterval->format("%a") + 1;
}
/**
* Вернет все пятницы 13 в году
* @param int $year год, в котором необходимо произвести расчет
* @return DateTimeImmutable[]
* @throws Exception
*/
public function countFriday13(int $year): iterable
{
$startDate = new DateTime("$year-01-01 Friday");
++$year;
$endDate = new DateTime("$year-01-01");
$interval = new DateInterval('P7D');
foreach (new DatePeriod($startDate, $interval, $endDate) as $day) {
yield new DateTimeImmutable($day->format("Y-m-d"));
}
}
}
\ No newline at end of file
Loading