Introduce new Monday type and turnBackTime function to simplify working with Mondays
authorCameron Ball <cameron@cameron1729.xyz>
Sun, 6 Jan 2019 09:25:59 +0000 (17:25 +0800)
committerCameron Ball <cameron@cameron1729.xyz>
Sun, 6 Jan 2019 18:34:20 +0000 (02:34 +0800)
src/common.php
src/purjolok.php
src/tasks.php
src/unfinished.php

index e71dc5a..7a15a6c 100644 (file)
@@ -6,6 +6,48 @@ require_once(AUTOLOAD_PATH);
 use Telegram\Bot\Api as TelegramAPI;
 use Telegram\Bot\Objects\Update as TelegramUpdate;
 
+final class Monday {
+    private $year;
+    private $month;
+    private $season;
+    private $weekNum;
+    private $dayNum;
+
+    public function __construct(
+        int $year,
+        int $month,
+        string $season,
+        int $weekNum,
+        int $dayNum
+    ) {
+        $this->year = $year;
+        $this->month = $month;
+        $this->season = $season;
+        $this->weekNum = $weekNum;
+        $this->dayNum = $dayNum;
+    }
+
+    public function __get($property) {
+        return $this->$property;
+    }
+}
+
+// Returns a the closest previous Monday given a date.
+// weekNumber is 1-5 (mondays in months are considered the start of a week, so if a month has 5 mondays it has 5 weeks)
+// dayNumber is the day of the month
+function turnBackTime(DateTimeImmutable $date) {
+    $y = (int) $date->format('Y');
+    $m = (int) $date->format('n');
+    $d = (int) $date->format('d');
+    return new Monday(
+        getYearWeekBeginsIn($y, $m, $d),
+        getMonthWeekBeginsIn($y, $m, $d),
+        getSeason(getMonthWeekBeginsIn($y, $m, $d)),
+        getWeekNumber($y, $m, $d),
+        getDayNumber($y, $m, $d)
+    );
+}
+
 function getTelegram(): TelegramAPI {
     STATIC $tg;
     return $tg = $tg ?? new TelegramAPI(BOT_TOKEN);
index 9f4cacd..dcc2eac 100644 (file)
@@ -16,23 +16,13 @@ if(getTelegram()->getWebHookUpdates()->get('message') && !canChatWith(getTelegra
 $message = getTelegram()->getWebHookUpdates()->get('message') ? getTelegram()->getWebHookUpdates()->get('message')->get('text') : '';
 
 if ($between = between(reveal($message ?? ''), '[taskid]')) {
-    // The actual date/time when the script is called
-    $currentYear = (int)(new DateTimeImmutable())->format('Y');
-    $currentMonth = (int)(new DateTimeImmutable())->format('n');
-    $currentDay = (int)(new DateTimeImmutable())->format('d');
-
-    // The years/months/weeks that we are "rewinding" to, for calculations
-    $yearForThisWeek = getYearWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-    $monthForThisWeek = getMonthWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-    $seasonForThisWeek = getSeason($monthForThisWeek);
-    $weekNum = getWeekNumber($currentYear, $currentMonth, $currentDay);
-
-    $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
+    $monday = turnBackTime(new DateTimeImmutable());
+    $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum);
     $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
     $tasksForTheWeek = getTasksForTheWeek(
-        $yearForThisWeek,
-        $monthForThisWeek,
-        $weekNum,
+        $monday->year,
+        $monday->month,
+        $monday->weekNum,
         require 'taskMatrix.php'
     );
 
@@ -235,25 +225,15 @@ getTelegram()->addCommand(
         protected $description = 'List tasks for this week';
 
         public function handle($arguments) {
-            // The actual date/time when the script is called
-            $currentYear = (int)(new DateTimeImmutable())->format('Y');
-            $currentMonth = (int)(new DateTimeImmutable())->format('n');
-            $currentDay = (int)(new DateTimeImmutable())->format('d');
-
-            // The years/months/weeks that we are "rewinding" to, for calculations
-            $yearForThisWeek = getYearWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-            $monthForThisWeek = getMonthWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-            $seasonForThisWeek = getSeason($monthForThisWeek);
-            $weekNum = getWeekNumber($currentYear, $currentMonth, $currentDay);
-
+            $monday = turnBackTime(new DateTimeImmutable());
             $tasksForTheWeek = getTasksForTheWeek(
-                $yearForThisWeek,
-                $monthForThisWeek,
-                $weekNum,
+                $monday->year,
+                $monday->month,
+                $monday->weekNum,
                 require 'taskMatrix.php'
             );
 
-            $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
+            $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum);
             $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
 
             if (!array_diff($tasksForTheWeek, $completedTasks)) {
@@ -279,25 +259,15 @@ getTelegram()->addCommand(
         protected $description = 'Mark a task as completed';
 
         public function handle($arguments) {
-            // The actual date/time when the script is called
-            $currentYear = (int)(new DateTimeImmutable())->format('Y');
-            $currentMonth = (int)(new DateTimeImmutable())->format('n');
-            $currentDay = (int)(new DateTimeImmutable())->format('d');
-
-            // The years/months/weeks that we are "rewinding" to, for calculations
-            $yearForThisWeek = getYearWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-            $monthForThisWeek = getMonthWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-            $seasonForThisWeek = getSeason($monthForThisWeek);
-            $weekNum = getWeekNumber($currentYear, $currentMonth, $currentDay);
-
+            $monday = turnBackTime(new DateTimeImmutable);
             $tasksForTheWeek = getTasksForTheWeek(
-                $yearForThisWeek,
-                $monthForThisWeek,
-                $weekNum,
+                $monday->year,
+                $monday->month,
+                $monday->weekNum,
                 require 'taskMatrix.php'
             );
 
-            $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
+            $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum);
             $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
 
             if (!array_diff($tasksForTheWeek, $completedTasks)) {
index 38ecb83..42c0f7c 100644 (file)
@@ -3,34 +3,23 @@
 require_once('common.php');
 
 $taskMatrix = require 'taskMatrix.php';
-
-// The actual date/time when the script is called
-$currentYear = (int)(new DateTimeImmutable())->format('Y');
-$currentMonth = (int)(new DateTimeImmutable())->format('n');
-$currentDay = (int)(new DateTimeImmutable())->format('d');
-
-// The years/months/weeks that we are "rewinding" to, for calculations
-$yearForThisWeek = getYearWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-$monthForThisWeek = getMonthWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-$seasonForThisWeek = getSeason($monthForThisWeek);
-$weekNum = getWeekNumber($currentYear, $currentMonth, $currentDay);
-$dayNum = getDayNumber($currentYear, $currentMonth, $currentDay);
+$monday = turnBackTime(new DateTimeImmutable());
 
 $taskLists = array_merge(
-    isStartOfSeason($monthForThisWeek, $dayNum) ? [unlines(map(getString)(getTasksForTheSeason($seasonForThisWeek, $taskMatrix)))] : [],
-    isStartOfMonth($dayNum) ? [unlines(map(getString)(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))] : [],
-    [unlines(map(getString)(getTasksForTheWeek($yearForThisWeek, $monthForThisWeek, $weekNum, $taskMatrix)))]
+    isStartOfSeason($monday->month, $monday->dayNum) ? [unlines(map(getString)(getTasksForTheSeason($monday->season, $taskMatrix)))] : [],
+    isStartOfMonth($monday->dayNum) ? [unlines(map(getString)(getTasksForTheMonth($monday->month, $taskMatrix)))] : [],
+    [unlines(map(getString)(getTasksForTheWeek($monday->year, $monday->month, $monday->weekNum, $taskMatrix)))]
 );
 
 $taskMessages = [
     [getString('happyMonday')],
     [
-        [getString('beginningOf'. ucfirst(getMonthName($monthForThisWeek))), getString('thisMonthThereAre', count(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))],
+        [getString('beginningOf'. ucfirst(getMonthName($monday->month))), getString('thisMonthThereAre', count(getTasksForTheMonth($monday->month, $taskMatrix)))],
          getString('anywayHeresTheWeek')
     ],
     [
-        getString('beginningOf' . ucfirst(getSeason($monthForThisWeek))),
-        [getString('anywayHeresTheMonth'), getString('thisMonthThereAre', count(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))],
+        getString('beginningOf' . ucfirst($monday->season)),
+        [getString('anywayHeresTheMonth'), getString('thisMonthThereAre', count(getTasksForTheMonth($monday->month, $taskMatrix)))],
         getString('finallyHeresTheWeek')
     ]
 ];
@@ -46,7 +35,7 @@ $messages = zipWith(
     },
     // Magic. startOfSeason implies startofMonth so we get 2, start of month without start of season gives 1 and
     // a regular week (not the start of a season or month) gives 0. And this is how the indicies are ordered in the array.
-    $taskMessages[(int)isStartOfSeason($monthForThisWeek, $dayNum) + (int)isStartOfMonth($dayNum)],
+    $taskMessages[(int)isStartOfSeason($monday->month, $monday->dayNum) + (int)isStartOfMonth($monday->dayNum)],
     $taskLists
 );
 
index b106c76..5e3a02c 100644 (file)
@@ -3,18 +3,7 @@
 require_once('common.php');
 
 $taskMatrix = require 'taskMatrix.php';
-
-// The actual date/time when the script is called
-$currentYear = (int)(new DateTimeImmutable())->format('Y');
-$currentMonth = (int)(new DateTimeImmutable())->format('n');
-$currentDay = (int)(new DateTimeImmutable())->format('d');
-
-// The years/months/weeks that we are "rewinding" to, for calculations
-$yearForThisWeek = getYearWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-$monthForThisWeek = getMonthWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-$seasonForThisWeek = getSeason($monthForThisWeek);
-$weekNum = getWeekNumber($currentYear, $currentMonth, $currentDay);
-$dayNum = getDayNumber($currentYear, $currentMonth, $currentDay);
+$monday = turnBackTime(new DateTimeImmutable());
 
 $extractTasks = function($tasks, $path) {
     return array_merge($tasks, file_exists($path) ? lines(trim(file_get_contents($path))) : []);
@@ -26,35 +15,35 @@ $unfinishedForYear = array_diff(
              return getTasksForTheSeason($season, $taskMatrix);
          }
      )(['summer', 'autumn', 'winter', 'spring'])),
-     array_reduce(getFilePathsForYear($yearForThisWeek), $extractTasks, [])
+     array_reduce(getFilePathsForYear($monday->year), $extractTasks, [])
 );
 
 $unfinishedForSeason = array_diff(
-    getTasksForTheSeason($seasonForThisWeek, $taskMatrix),
-    array_reduce(getFilePathsForSeason($yearForThisWeek, $seasonForThisWeek), $extractTasks, [])
+    getTasksForTheSeason($monday->season, $taskMatrix),
+    array_reduce(getFilePathsForSeason($monday->year, $monday->season), $extractTasks, [])
 );
 
 $unfinishedForMonth = array_diff(
-    getTasksForTheMonth($monthForThisWeek, $taskMatrix),
-    array_reduce(getFilePathsForMonth($yearForThisWeek, $monthForThisWeek), $extractTasks, [])
+    getTasksForTheMonth($monday->month, $taskMatrix),
+    array_reduce(getFilePathsForMonth($monday->year, $monday->month), $extractTasks, [])
 );
 
-$filePathForWeek = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
+$filePathForWeek = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum);
 $unfinishedForWeek = array_diff(
-    getTasksForTheWeek($yearForThisWeek, $monthForThisWeek, $weekNum, $taskMatrix),
+    getTasksForTheWeek($monday->year, $monday->month, $monday->weekNum, $taskMatrix),
     file_exists($filePathForWeek) ? lines(trim(file_get_contents($filePathForWeek))) : []
 );
 
 //EOY => (EOM & EOW) & !EOSx
 //EOS => (EOM & EOW) & !EOY
 $taskLists = array_merge(
-    isEndOfYear($yearForThisWeek, $monthForThisWeek, $dayNum) ? [unlines(map(getString)($unfinishedForYear))] : [],
-    isEndOfSeason($yearForThisWeek, $monthForThisWeek, $dayNum) ? [unlines(map(getString)($unfinishedForSeason))] : [],
-    isEndOfMonth($yearForThisWeek, $monthForThisWeek, $dayNum) ? [unlines(map(getString)($unfinishedForMonth))] : [],
+    isEndOfYear($monday->year, $monday->month, $monday->dayNum) ? [unlines(map(getString)($unfinishedForYear))] : [],
+    isEndOfSeason($monday->year, $monday->month, $monday->dayNum) ? [unlines(map(getString)($unfinishedForSeason))] : [],
+    isEndOfMonth($monday->year, $monday->month, $monday->dayNum) ? [unlines(map(getString)($unfinishedForMonth))] : [],
     [unlines(map(getString)($unfinishedForWeek))]
 );
 
-$seasonName = ucfirst($seasonForThisWeek);
+$seasonName = ucfirst($monday->season);
 $goodOrBad = function($string, $goodOrBad) {
     return getString($string . ($goodOrBad ? '' : 'Good'));
 };
@@ -88,9 +77,9 @@ $messages = zipWith(
     },
     // Similar magic to tasks.php
     $taskMessages[
-        isEndOfMonth($yearForThisWeek, $monthForThisWeek, $dayNum) +
-        isEndOfSeason($yearForThisWeek, $monthForThisWeek, $dayNum) +
-        (isEndOfYear($yearForThisWeek, $monthForThisWeek, $dayNum) ? 2 : 0) // EOY is independant of EOS, to get the right index need to add 2 instead of 1.
+        isEndOfMonth($monday->year, $monday->month, $monday->dayNum) +
+        isEndOfSeason($monday->year, $monday->month, $monday->dayNum) +
+        (isEndOfYear($monday->year, $monday->month, $monday->dayNum) ? 2 : 0) // EOY is independant of EOS, to get the right index need to add 2 instead of 1.
     ],
     $taskLists
 );