Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • a.shamavov/tasks-php
1 result
Show changes
Commits on Source (38)
......@@ -37,5 +37,69 @@
];
print_r($func->search($array, 54));
?>
<h1>Function 3</h1>
<?php
$arr = [
['laravel', 'php'],
['codeigniter', 'php'],
['laravel', 'php'],
['c++', 'java'],
];
print_r($func->uniqElements($arr));
?>
<h1>Function 4</h1>
<?php
$aMenu = [
['name' => 'Смартфоны и гаджеты','depth' => 0,],
['name' => 'Смартфоны, мобильные телефоны','depth' => 1,],
['name' => 'Планшеты','depth' => 1,],
['name' => 'Наушники и гарнитуры','depth' => 1,],
['name' => 'Компьютеры и ноутбуки','depth' => 0,],
['name' => 'Ноутбуки и аксессуары','depth' => 1,],
['name' => 'Компьютеры и мониторы','depth' => 1,],
['name' => 'Компьютерные комплектующие','depth' => 1,],
['name' => 'Техника для дома','depth' => 0,],
['name' => 'Техника для уборки','depth' => 1,],
['name' => 'Товары для ухода за одеждой','depth' => 1,],
['name' => 'Аксессуары для техники','depth' => 1,],
['name' => 'Товары для дома и кухни','depth' => 0,],
['name' => 'Посуда','depth' => 1,],
];
print_r($func->prepareMenu($aMenu));
?>
<h1>Function 5</h1>
<?php
print "<h2>До НГ: </h2>";
try {
print $func->howDaysToNy(new DateTimeImmutable());
} catch (Exception $e) {
print $e->getMessage();
}
?>
<h1>Function 6</h1>
<?php
print "<h2>Пятницы 13: </h2>";
try {
foreach ($func->countFriday13(2024) as $date) {
print $date->format("Y-m-d l") . "\n";
}
} catch (Exception $e) {
print $e->getMessage();
}
?>
<h1>Function 7</h1>
<?php
print "<h2>Разница дней: </h2>";
print $func->diffDays(new DateTimeImmutable(), new DateTimeImmutable("2025-01-01"));
?>
</body>
</html>
\ No newline at end of file
......@@ -52,5 +52,134 @@ class Functions
}
return null;
}
/**
* Удалить дубликаты, оставив только уникальные значения
* @param array $array
* @return array
*/
public function uniqElements(array $array): array
{
return array_unique($array, SORT_REGULAR);
}
/**
* Выходной массив:
* Array (
* [0] => Array([0] => laravel, [1] => php)
* [1] => Array([0] => codeigniter, [1] => php)
* [3] => Array([0] => c++, [1] => java))
* )
*/
/**
* Сгруппировать подразедлы в верхние разделы меню
* Дочерние элементы поместить в массив родителя с ключом 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;
}
/**
* Выходные данные:
* $aMenu = [
* [
* 'name' => 'Смартфоны и гаджеты',
* 'depth' => 0,
* 'submenu' => [
* ['name' => 'Смартфоны, мобильные телефоны','depth' => 1,],
* ['name' => 'Планшеты','depth' => 1,],
* ['name' => 'Наушники и гарнитуры','depth' => 1,],],
* ],
* [
* 'name' => 'Компьютеры и ноутбуки',
* 'depth' => 0,
* 'submenu' => [
* ['name' => 'Ноутбуки и аксессуары','depth' => 1,],
* ['name' => 'Компьютеры и мониторы','depth' => 1,],
* ['name' => 'Компьютерные комплектующие','depth' => 1,],]],
* [
* 'name' => 'Техника для дома',
* 'depth' => 0,
* 'submenu' => [
* ['name' => 'Техника для уборки','depth' => 1,],
* ['name' => 'Товары для ухода за одеждой','depth' => 1,],
* ['name' => 'Аксессуары для техники','depth' => 1,],]
* ],
* [
* 'name' => 'Товары для дома и кухни',
* 'depth' => 0,
* 'submenu' => [
* ['name' => 'Посуда','depth' => 1,],]],
* ];
*/
/**
* Функция рассчитывает кол-во дней до нового года
* @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"));
}
}
/**
* Вернет кол-во дней между датами
* @param DateTimeImmutable $dateStart дата начала
* @param DateTimeImmutable $dateEnd дата окончания
* @return int
*/
public function diffDays(DateTimeImmutable $dateStart, DateTimeImmutable $dateEnd): int
{
$dateInterval = date_diff($dateStart, $dateEnd);
return (int)$dateInterval->format("%a") ;
}
}