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;
-}
*/
use Friendica\App;
+use Friendica\BaseObject;
use Friendica\Content\Feature;
use Friendica\Content\ForumManager;
use Friendica\Content\Nav;
$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;
}
$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 {
$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 {
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;
+ }
}
--- /dev/null
+<?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));
+ }
+}