Move is_a_date_arg to DateTimeFormat::isYearMonth

- Improved functionality
- Added tests
This commit is contained in:
Philipp Holzer 2019-10-23 02:39:28 +02:00
parent 52c42491c4
commit ad67fd3aa8
No known key found for this signature in database
GPG Key ID: D8365C3D36B77D90
5 changed files with 107 additions and 22 deletions

View File

@ -72,22 +72,3 @@ function get_cats_and_terms($item)
return [$categories, $folders]; 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;
}

View File

@ -5,6 +5,7 @@
*/ */
use Friendica\App; use Friendica\App;
use Friendica\BaseObject;
use Friendica\Content\Feature; use Friendica\Content\Feature;
use Friendica\Content\ForumManager; use Friendica\Content\ForumManager;
use Friendica\Content\Nav; use Friendica\Content\Nav;
@ -51,9 +52,12 @@ function network_init(App $a)
$group_id = 0; $group_id = 0;
} }
/** @var DateTimeFormat $dtFormat */
$dtFormat = BaseObject::getClass(DateTimeFormat::class);
if ($a->argc > 1) { if ($a->argc > 1) {
for ($x = 1; $x < $a->argc; $x ++) { 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; $is_a_date_query = true;
break; break;
} }
@ -461,9 +465,12 @@ function networkThreadedView(App $a, $update, $parent)
$default_permissions = []; $default_permissions = [];
/** @var DateTimeFormat $dtFormat */
$dtFormat = BaseObject::getClass(DateTimeFormat::class);
if ($a->argc > 1) { if ($a->argc > 1) {
for ($x = 1; $x < $a->argc; $x ++) { for ($x = 1; $x < $a->argc; $x ++) {
if (is_a_date_arg($a->argv[$x])) { if ($dtFormat->isYearMonth($a->argv[$x])) {
if ($datequery) { if ($datequery) {
$datequery2 = Strings::escapeHtml($a->argv[$x]); $datequery2 = Strings::escapeHtml($a->argv[$x]);
} else { } else {

View File

@ -131,9 +131,12 @@ class Profile extends BaseModule
$category = $datequery = $datequery2 = ''; $category = $datequery = $datequery2 = '';
/** @var DateTimeFormat $dtFormat */
$dtFormat = self::getClass(DateTimeFormat::class);
if ($a->argc > 2) { if ($a->argc > 2) {
for ($x = 2; $x < $a->argc; $x ++) { for ($x = 2; $x < $a->argc; $x ++) {
if (is_a_date_arg($a->argv[$x])) { if ($dtFormat->isYearMonth($a->argv[$x])) {
if ($datequery) { if ($datequery) {
$datequery2 = Strings::escapeHtml($a->argv[$x]); $datequery2 = Strings::escapeHtml($a->argv[$x]);
} else { } else {

View File

@ -148,4 +148,37 @@ class DateTimeFormat
return $d->format($format); 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;
}
} }

View File

@ -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));
}
}