]> _ Git - cubist_matomo.git/commitdiff
wip #5877
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 24 May 2023 17:09:43 +0000 (19:09 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 24 May 2023 17:09:43 +0000 (19:09 +0200)
composer.json
composer.lock
src/MatomoUtils.php

index 4eee7a68632a0c201357b795ab4d8f9d3bfeb3f0..9f94bee2cc29f9f6db8091514246905aae60ba8c 100644 (file)
@@ -25,6 +25,7 @@
         "php": ">=7.2",
         "guzzlehttp/guzzle": "^7.7",
         "ext-json": "*",
-        "illuminate/cache": ">=5"
+        "illuminate/cache": ">=5",
+        "nesbot/carbon": "^2.66"
     }
 }
index 65ca8b222c671dc9c89ad159d6c16851fed9c362..731d5087b7b53a3411a1b4bb71b0539f3929b948 100644 (file)
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "510c6ead0c5b49decdb21fa4fe18914c",
+    "content-hash": "b4abfad71e4d4f905f5ce400600696ac",
     "packages": [
         {
             "name": "doctrine/inflector",
index 03a0ec3329f455944cc331f5e36ffdc33e3331d8..792154c57544d78832e433161e78325ad66a83f4 100644 (file)
@@ -2,9 +2,10 @@
 
 namespace Cubist\Matomo;
 
+use Carbon\Carbon;
+
 class MatomoUtils {
-    public static function parseDate($date)
-    {
+    public static function parseDate($date) {
         // Match possible date strings:
         // - YYYY
         // - YYYY-MM
@@ -24,4 +25,39 @@ class MatomoUtils {
 
         return $date_matches;
     }
+
+    public static function getBestPeriod($dates) {
+        // Extract array keys as variables for easier access:
+        // $start_date, $start_year, $start_month, $start_day, $end_date
+        extract($dates);
+
+        // At this point, if the end_date still isn't set, it must either be a YYYY or YYYY-MM that was passed in.
+        // Therefore, we can also assume that $start_year will be set. All that's left to do is set appropriate
+        // start and end dates so that we can determine the best period based on the overall length of the range.
+        if (!isset($end_date)) {
+            if (isset($start_month) && !empty($start_month)) { // Month mode
+                $start_date = "{$start_year}-{$start_month}-01";
+                $end_date = Carbon::parse($start_date)->endOfMonth()->isoFormat('YYYY-MM-DD');
+            } else { // Year mode
+                $start_date = "{$start_year}-01-01";
+                $end_date = Carbon::parse($start_date)->endOfYear()->isoFormat('YYYY-MM-DD');
+            }
+        }
+
+        // Count <= 60 days: period = day
+        // Count 61–180 days: period = week
+        // Count 180+ days: period = month
+        // Count 2+ years: period = year
+        $day_count = Carbon::parse($start_date)->diffInDays($end_date);
+
+        if ($day_count <= 60) {
+            return 'day';
+        } elseif ($day_count <= 180) {
+            return 'week';
+        } elseif ($day_count <= 730) {
+            return 'month';
+        }
+
+        return 'year';
+    }
 }