Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/Lib/Assert/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Text
*
* @return bool
*/
public static function isEmail(string $email): bool
public static function isEmail(string $str): bool
{
return filter_var($email, FILTER_VALIDATE_EMAIL) === false ? false : true;
return filter_var($str, FILTER_VALIDATE_EMAIL) === false ? false : true;
}


Expand All @@ -37,4 +37,23 @@ public static function hasMin(int $lim, string $str): bool
{
return strlen($str) >= $lim;
}


/**
* Test if string is date
*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @param string $str.

* @return bool
*/
public static function isTimestamp(string $str): bool
{
$ts = \App\Lib\Util\Timestamp::timestampToArray($str);
if (checkdate($ts['m'], $ts['d'], $ts['y']) &&
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can validate timestamp format by https://www.php.net/manual/en/function.strtotime.php it returns false if format is not valid.

Copy link
Copy Markdown
Member Author

@oliverstasa oliverstasa Jan 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes can be, but i dont think i could check for yyyy-mm-ddThh:ii and pass it accordingly, also we'd loose the ability of easy checks for separate values, like:

$ts = \App\Lib\Util\Timestamp::timestampToArray($str);
$hour = $ts['h'];

Copy link
Copy Markdown
Contributor

@pchalupa pchalupa Jan 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is true, but that is meant that 2021-01-01 will not be treated as valid timestamp despite the fact it can be converted into unix timestamp. Same with passing only time.

$ts['h'] >= 0 && $ts['h'] <= 23 &&
$ts['i'] >= 0 && $ts['i'] <= 59 &&
$ts['s'] >= 0 && $ts['s'] <= 59) {
return true;
} else {
return false;
}
}
}
39 changes: 39 additions & 0 deletions src/Lib/Util/Timestamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Lib\Util;

use Exception;

class Timestamp
{
/**
* Retruns array from date string
*
* @param string $str
* @return array<int>
*/
public static function timestampToArray(string $str): array
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check https://www.php.net/manual/en/book.datetime.php? If it has all the features, we can use that class instead.

{
if (strpos($str, ' ')) {
$ts = explode(' ', $str);
} else if (strpos($str, 'T')) {
$ts = explode('T', $str);
$ts[1] .= ':00';
} else {
throw new Exception('Timestamp is Not in right format YYYY-MM-DD HH:II:SS', 400);
}

$date = explode('-', $ts[0]);
$time = explode(':', $ts[1]);

if (sizeof($date) == 3 && sizeof($time) == 3 &&
is_numeric($date[0]) && is_numeric($date[1]) && is_numeric($date[2]) &&
is_numeric($time[0]) && is_numeric($time[1]) && is_numeric($time[2])) {
return (array('y' => intval($date[0]), 'm' => intval($date[1]), 'd' => intval($date[2]), 'h' => intval($time[0]), 'i' => intval($time[1]), 's' => intval($time[2])));
} else {
throw new Exception('Timestamp is Not in right format YYYY-MM-DD HH:II:SS', 400);
}
}
}