From ad67fd3aa894048e9023c18e83f13fc72954f6fd Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Wed, 23 Oct 2019 02:39:28 +0200 Subject: [PATCH] Move is_a_date_arg to DateTimeFormat::isYearMonth - Improved functionality - Added tests --- include/text.php | 19 --------- mod/network.php | 11 ++++- src/Module/Profile.php | 5 ++- src/Util/DateTimeFormat.php | 33 +++++++++++++++ tests/src/Util/DateTimeFormatTest.php | 61 +++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 22 deletions(-) create mode 100644 tests/src/Util/DateTimeFormatTest.php diff --git a/include/text.php b/include/text.php index 775931c6ff..89c07000f7 100644 --- a/include/text.php +++ b/include/text.php @@ -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; -} diff --git a/mod/network.php b/mod/network.php index 0438be7059..64f5cf505f 100644 --- a/mod/network.php +++ b/mod/network.php @@ -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 { diff --git a/src/Module/Profile.php b/src/Module/Profile.php index ed37540753..f38c77f2cd 100644 --- a/src/Module/Profile.php +++ b/src/Module/Profile.php @@ -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 { diff --git a/src/Util/DateTimeFormat.php b/src/Util/DateTimeFormat.php index 0b47a16f15..e29420e9ea 100644 --- a/src/Util/DateTimeFormat.php +++ b/src/Util/DateTimeFormat.php @@ -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 index 0000000000..bdc902eab2 --- /dev/null +++ b/tests/src/Util/DateTimeFormatTest.php @@ -0,0 +1,61 @@ + [ + '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)); + } +} -- 2.39.5