Skip to content

Commit 699b8c5

Browse files
Merge pull request #3 from ccuffs/1
fix: year value treatment
2 parents 3eb68ad + d6cfb27 commit 699b8c5

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/UniversityRestaurantUFFS.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,44 @@ public function getMenuByCampus(string $link)
6868
) {
6969
$items = $row->getElementsByTagName('td');
7070
foreach ($items as $j => $item) {
71-
$menus[$week_string_dates[$j]][] = trim(preg_replace('/[\pZ\pC]/u', ' ', $item->textContent));
71+
$treated_item = trim(preg_replace('/[\pZ\pC]/u', ' ', $item->textContent));
72+
if ($treated_item != "") {
73+
$menus[$week_string_dates[$j]][] = $treated_item;
74+
}
7275
}
7376
}
7477
}
7578
}
7679
}
7780

81+
uksort($menus, function ($a, $b) {
82+
if (date_create_from_format("d/m/Y", $a) == date_create_from_format("d/m/Y", $b)) return 0;
83+
return ((date_create_from_format("d/m/Y", $a) < date_create_from_format("d/m/Y", $b)) ? -1 : 1);
84+
});
7885
return $menus;
7986
}
8087

8188
public function handleWeekDate(string $week)
8289
{
8390
preg_match_all("/(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/([0-9]{4}|[0-9]{2})/", $week, $matches);
84-
preg_match_all("/\s(0[1-9]|[1-2][0-9]|3[0-1])\s|\s([1-9])\s/", $week, $matches_prime);
85-
if ($matches_prime[0]) {
86-
$date2 = date_create_from_format("d/m/Y", $matches[0][0])->modify('+1 day');
87-
$date1 = clone $date2;
88-
$date1->modify("-5 days");
89-
} else {
90-
$date2 = date_create_from_format("d/m/Y", $matches[0][1])->modify('+1 day');
91-
$date1 = date_create_from_format("d/m/Y", $matches[0][0]);
92-
93-
if ($date2->diff($date1)->m > 0) {
91+
92+
if ($matches[0]) {
93+
$date2 = date_create_from_format("d/m/Y", end($matches[0]))->modify('+1 day');
94+
if(count($matches[0]) > 1){
95+
$date1 = date_create_from_format("d/m/Y", $matches[0][0]);
96+
} else {
9497
$date1 = clone $date2;
9598
$date1->modify("-5 days");
9699
}
100+
101+
}
102+
103+
if ($date1->format("Y") < 100) {
104+
$date1 = date_create_from_format("d/m/y", $date1->format("d/m/y"));
105+
}
106+
107+
if ($date2->format("Y") < 100) {
108+
$date2 = date_create_from_format("d/m/y", $date2->format("d/m/y"));
97109
}
98110

99111
$week_dates = new DatePeriod(

tests/test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
use CCUFFS\Scrap\UniversityRestaurantUFFS;
66

77
$ur = new UniversityRestaurantUFFS();
8-
print_r($ur->getMenuByWeekDay($ur->campus["chapeco"], 'seg'));
8+
9+
$list = $ur->getMenuByCampus($ur->campus["realeza"]);
10+
print_r($list);

0 commit comments

Comments
 (0)