mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 01:31:45 +01:00
Minor refactoring
This commit is contained in:
@@ -191,7 +191,7 @@ class Date
|
|||||||
* method.
|
* method.
|
||||||
* @return null|string
|
* @return null|string
|
||||||
*/
|
*/
|
||||||
public static function generateRandomTime($format = 'H:i:s')
|
public static function generateRandomTime($format = 'H:i:s'): ?string
|
||||||
{
|
{
|
||||||
$dateTime = new DateTime();
|
$dateTime = new DateTime();
|
||||||
|
|
||||||
@@ -303,7 +303,7 @@ class Date
|
|||||||
* @param int $day The day value
|
* @param int $day The day value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function getDayOfWeekName($year, $month, $day)
|
public static function getDayOfWeekName($year, $month, $day): string
|
||||||
{
|
{
|
||||||
$hour = 0;
|
$hour = 0;
|
||||||
$minute = 0;
|
$minute = 0;
|
||||||
@@ -381,22 +381,26 @@ class Date
|
|||||||
self::DATE_DIFFERENCE_UNIT_MINUTES,
|
self::DATE_DIFFERENCE_UNIT_MINUTES,
|
||||||
];
|
];
|
||||||
|
|
||||||
if (null === $differenceUnit || self::DATE_DIFFERENCE_UNIT_YEARS === $differenceUnit) {
|
$differenceYear = self::DATE_DIFFERENCE_UNIT_YEARS === $differenceUnit;
|
||||||
|
$differenceMonth = self::DATE_DIFFERENCE_UNIT_MONTHS === $differenceUnit;
|
||||||
|
$differenceMinutes = self::DATE_DIFFERENCE_UNIT_MINUTES === $differenceUnit;
|
||||||
|
|
||||||
|
if (null === $differenceUnit || $differenceYear) {
|
||||||
$diff = $end->diff($start);
|
$diff = $end->diff($start);
|
||||||
|
|
||||||
// Difference between dates in years should be returned only?
|
// Difference between dates in years should be returned only?
|
||||||
if (self::DATE_DIFFERENCE_UNIT_YEARS === $differenceUnit) {
|
if ($differenceYear) {
|
||||||
return $diff->y;
|
return $diff->y;
|
||||||
}
|
}
|
||||||
|
|
||||||
$difference[self::DATE_DIFFERENCE_UNIT_YEARS] = $diff->y;
|
$difference[self::DATE_DIFFERENCE_UNIT_YEARS] = $diff->y;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === $differenceUnit || self::DATE_DIFFERENCE_UNIT_MONTHS === $differenceUnit) {
|
if (null === $differenceUnit || $differenceMonth) {
|
||||||
$diff = $end->diff($start);
|
$diff = $end->diff($start);
|
||||||
|
|
||||||
// Difference between dates in months should be returned only?
|
// Difference between dates in months should be returned only?
|
||||||
if (self::DATE_DIFFERENCE_UNIT_MONTHS === $differenceUnit) {
|
if ($differenceMonth) {
|
||||||
return $diff->m;
|
return $diff->m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -437,11 +441,11 @@ class Date
|
|||||||
$hoursInSeconds = $hours * $hourSeconds;
|
$hoursInSeconds = $hours * $hourSeconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === $differenceUnit || self::DATE_DIFFERENCE_UNIT_MINUTES === $differenceUnit) {
|
if (null === $differenceUnit || $differenceMinutes) {
|
||||||
$minutes = (int)floor(($dateDiff - $daysInSeconds - $hoursInSeconds) / 60);
|
$minutes = (int)floor(($dateDiff - $daysInSeconds - $hoursInSeconds) / 60);
|
||||||
|
|
||||||
// Difference between dates in minutes should be returned only?
|
// Difference between dates in minutes should be returned only?
|
||||||
if (self::DATE_DIFFERENCE_UNIT_MINUTES === $differenceUnit) {
|
if ($differenceMinutes) {
|
||||||
return $minutes;
|
return $minutes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,7 +467,7 @@ class Date
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function getDatesCollection(DateTime $startDate, $datesCount, $intervalTemplate = 'P%dD')
|
public static function getDatesCollection(DateTime $startDate, $datesCount, $intervalTemplate = 'P%dD'): array
|
||||||
{
|
{
|
||||||
$dates = [];
|
$dates = [];
|
||||||
|
|
||||||
@@ -513,8 +517,12 @@ class Date
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
* @return DateTime
|
* @return DateTime
|
||||||
*/
|
*/
|
||||||
public static function getRandomDate(DateTime $startDate = null, $start = 1, $end = 100, $intervalTemplate = 'P%sD')
|
public static function getRandomDate(
|
||||||
{
|
DateTime $startDate = null,
|
||||||
|
$start = 1,
|
||||||
|
$end = 100,
|
||||||
|
$intervalTemplate = 'P%sD'
|
||||||
|
): DateTime {
|
||||||
if (null === $startDate) {
|
if (null === $startDate) {
|
||||||
$startDate = new DateTime();
|
$startDate = new DateTime();
|
||||||
}
|
}
|
||||||
@@ -531,7 +539,7 @@ class Date
|
|||||||
}
|
}
|
||||||
|
|
||||||
$randomDate = clone $startDate;
|
$randomDate = clone $startDate;
|
||||||
$randomInterval = new DateInterval(sprintf($intervalTemplate, mt_rand($start, $end)));
|
$randomInterval = new DateInterval(sprintf($intervalTemplate, random_int($start, $end)));
|
||||||
|
|
||||||
return $randomDate->add($randomInterval);
|
return $randomDate->add($randomInterval);
|
||||||
}
|
}
|
||||||
@@ -623,12 +631,10 @@ class Date
|
|||||||
* Verify instance of the DateTime created by constructor and by createFromFormat() method.
|
* Verify instance of the DateTime created by constructor and by createFromFormat() method.
|
||||||
* After formatting, these dates should be the same.
|
* After formatting, these dates should be the same.
|
||||||
*/
|
*/
|
||||||
else {
|
elseif ($dateFromFormat->format($dateFormat) === $value) {
|
||||||
if ($dateFromFormat->format($dateFormat) === $value) {
|
return $date;
|
||||||
return $date;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (\Exception $exception) {
|
} catch (Exception $exception) {
|
||||||
if (!$allowCompoundFormats) {
|
if (!$allowCompoundFormats) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -643,7 +649,7 @@ class Date
|
|||||||
if ($dateString !== (string)$value) {
|
if ($dateString !== (string)$value) {
|
||||||
return new DateTime($dateString);
|
return new DateTime($dateString);
|
||||||
}
|
}
|
||||||
} catch (\Exception $exception) {
|
} catch (Exception $exception) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -658,7 +664,7 @@ class Date
|
|||||||
* month", "yyyy"). Otherwise - not and every incorrect value is refused.
|
* month", "yyyy"). Otherwise - not and every incorrect value is refused.
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function isValidDate($value, $allowCompoundFormats = false)
|
public static function isValidDate($value, $allowCompoundFormats = false): bool
|
||||||
{
|
{
|
||||||
return self::getDateTime($value, $allowCompoundFormats) instanceof DateTime;
|
return self::getDateTime($value, $allowCompoundFormats) instanceof DateTime;
|
||||||
}
|
}
|
||||||
@@ -669,7 +675,7 @@ class Date
|
|||||||
* @param string $format The validated format of date
|
* @param string $format The validated format of date
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function isValidDateFormat($format)
|
public static function isValidDateFormat($format): bool
|
||||||
{
|
{
|
||||||
if (empty($format) || !is_string($format)) {
|
if (empty($format) || !is_string($format)) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user