]> git.mxchange.org Git - friendica.git/commitdiff
Move is_a_date_arg to DateTimeFormat::isYearMonth
authorPhilipp Holzer <admin+github@philipp.info>
Wed, 23 Oct 2019 00:39:28 +0000 (02:39 +0200)
committerPhilipp Holzer <admin+github@philipp.info>
Wed, 23 Oct 2019 00:39:28 +0000 (02:39 +0200)
- Improved functionality
- Added tests

include/text.php
mod/network.php
src/Module/Profile.php
src/Util/DateTimeFormat.php
tests/src/Util/DateTimeFormatTest.php [new file with mode: 0644]

index 775931c6ff82c1578989cf4b05602ca513c3819a..89c07000f7557501ec4d43498a3795369041c1e0 100644 (file)
@@ -72,22 +72,3 @@ function get_cats_and_terms($item)
 
        return [$categories, $folders];
 }
-
-/// @TODO Rewrite this
-function is_a_date_arg($s) {
-       $i = intval($s);
-
-       if ($i > 1900) {
-               $y = date('Y');
-
-               if ($i <= $y + 1 && strpos($s, '-') == 4) {
-                       $m = intval(substr($s, 5));
-
-                       if ($m > 0 && $m <= 12) {
-                               return true;
-                       }
-               }
-       }
-
-       return false;
-}
index 0438be7059d7680ba8dbd926ef922473ed4bd9b5..64f5cf505f64f3530f790713c54c79cd25e5f312 100644 (file)
@@ -5,6 +5,7 @@
  */
 
 use Friendica\App;
+use Friendica\BaseObject;
 use Friendica\Content\Feature;
 use Friendica\Content\ForumManager;
 use Friendica\Content\Nav;
@@ -51,9 +52,12 @@ function network_init(App $a)
                $group_id = 0;
        }
 
+       /** @var DateTimeFormat $dtFormat */
+       $dtFormat = BaseObject::getClass(DateTimeFormat::class);
+
        if ($a->argc > 1) {
                for ($x = 1; $x < $a->argc; $x ++) {
-                       if (is_a_date_arg($a->argv[$x])) {
+                       if ($dtFormat->isYearMonth($a->argv[$x])) {
                                $is_a_date_query = true;
                                break;
                        }
@@ -461,9 +465,12 @@ function networkThreadedView(App $a, $update, $parent)
 
        $default_permissions = [];
 
+       /** @var DateTimeFormat $dtFormat */
+       $dtFormat = BaseObject::getClass(DateTimeFormat::class);
+
        if ($a->argc > 1) {
                for ($x = 1; $x < $a->argc; $x ++) {
-                       if (is_a_date_arg($a->argv[$x])) {
+                       if ($dtFormat->isYearMonth($a->argv[$x])) {
                                if ($datequery) {
                                        $datequery2 = Strings::escapeHtml($a->argv[$x]);
                                } else {
index ed37540753582fea049838a2139b5f285f0c88ad..f38c77f2cdcb5d8ba79d8484af15f8325da4e7e9 100644 (file)
@@ -131,9 +131,12 @@ class Profile extends BaseModule
 
                $category = $datequery = $datequery2 = '';
 
+               /** @var DateTimeFormat $dtFormat */
+               $dtFormat = self::getClass(DateTimeFormat::class);
+
                if ($a->argc > 2) {
                        for ($x = 2; $x < $a->argc; $x ++) {
-                               if (is_a_date_arg($a->argv[$x])) {
+                               if ($dtFormat->isYearMonth($a->argv[$x])) {
                                        if ($datequery) {
                                                $datequery2 = Strings::escapeHtml($a->argv[$x]);
                                        } else {
index 0b47a16f1549b5fa86c4417bc1bd6949c6684c5c..e29420e9ea41c272dcbbb63868bc0ffac6972b0a 100644 (file)
@@ -148,4 +148,37 @@ class DateTimeFormat
 
                return $d->format($format);
        }
+
+       /**
+        * Checks, if the given string is a date with the pattern YYYY-MM
+        *
+        * @param string $dateString The given date
+        *
+        * @return boolean True, if the date is a valid pattern
+        */
+       public function isYearMonth(string $dateString)
+       {
+               // Check format (2019-01, 2019-1, 2019-10)
+               if (!preg_match('/^([12]\d{3}-(1[0-2]|0[1-9]|\d))$/', $dateString)) {
+                       return false;
+               }
+
+               $date = DateTime::createFromFormat('Y-m', $dateString);
+
+               if (!$date) {
+                       return false;
+               }
+
+               try {
+                       $now = new DateTime();
+               } catch (\Throwable $t) {
+                       return false;
+               }
+
+               if ($date > $now) {
+                       return false;
+               }
+
+               return true;
+       }
 }
diff --git a/tests/src/Util/DateTimeFormatTest.php b/tests/src/Util/DateTimeFormatTest.php
new file mode 100644 (file)
index 0000000..bdc902e
--- /dev/null
@@ -0,0 +1,61 @@
+<?php
+
+namespace Friendica\Test\src\Util;
+
+use Friendica\Test\MockedTest;
+use Friendica\Util\DateTimeFormat;
+
+class DateTimeFormatTest extends MockedTest
+{
+       public function dataYearMonth()
+       {
+               return [
+                       'validNormal' => [
+                               'input' => '1990-10',
+                               'assert' => true,
+                       ],
+                       'validOneCharMonth' => [
+                               'input' => '1990-1',
+                               'assert' => true,
+                       ],
+                       'validTwoCharMonth' => [
+                               'input' => '1990-01',
+                               'assert' => true,
+                       ],
+                       'invalidFormat' => [
+                               'input' => '199-11',
+                               'assert' => false,
+                       ],
+                       'invalidFormat2' => [
+                               'input' => '1990-15',
+                               'assert' => false,
+                       ],
+                       'invalidFormat3' => [
+                               'input' => '99-101',
+                               'assert' => false,
+                       ],
+                       'invalidFormat4' => [
+                               'input' => '11-1990',
+                               'assert' => false,
+                       ],
+                       'invalidFuture' => [
+                               'input' => '3030-12',
+                               'assert' => false,
+                       ],
+                       'invalidYear' => [
+                               'input' => '-100-10',
+                               'assert' => false,
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider dataYearMonth
+        */
+       public function testIsYearMonth(string $input, bool $assert)
+       {
+               $dtFormat = new DateTimeFormat();
+
+               $this->assertEquals($assert, $dtFormat->isYearMonth($input));
+       }
+}