Fixed leap year birthdays without a year set.
This commit is contained in:
parent
3909cdf762
commit
3e2183b7b2
2 changed files with 37 additions and 6 deletions
27
src/DateCheck.php
Normal file
27
src/DateCheck.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
namespace Misuzu;
|
||||
|
||||
final class DateCheck {
|
||||
public static function isLeapYear(int $year): bool {
|
||||
return (($year % 4) === 0 && ($year % 100) !== 0)
|
||||
|| ($year % 400) === 0;
|
||||
}
|
||||
|
||||
// i realise this implementation is probably horribly naive,
|
||||
// but for what i need it will Do the Job
|
||||
public static function isValidDate(int $year, int $month, int $day): bool {
|
||||
if($month < 1 || $month > 12)
|
||||
return false;
|
||||
if($day < 1 || $day > 31)
|
||||
return false;
|
||||
|
||||
if($month === 2)
|
||||
$days = $year === 0 || self::isLeapYear($year) ? 29 : 28;
|
||||
elseif($month === 4 || $month === 6 || $month === 9 || $month === 11)
|
||||
$days = 30;
|
||||
else
|
||||
$days = 31;
|
||||
|
||||
return $day <= $days;
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ use DateTime;
|
|||
use DateTimeZone;
|
||||
use Index\XString;
|
||||
use Index\Colour\Colour;
|
||||
use Misuzu\DateCheck;
|
||||
use Misuzu\DB;
|
||||
use Misuzu\HasRankInterface;
|
||||
use Misuzu\Memoizer;
|
||||
|
@ -318,6 +319,10 @@ class User implements HasRankInterface {
|
|||
return new DateTime($this->user_birthdate ?? '0000-01-01', new DateTimeZone('UTC'));
|
||||
}
|
||||
public function setBirthdate(int $year, int $month, int $day): self {
|
||||
// lowest leap year mariadb supports lol
|
||||
// should probably split the date field and year field in the db but i'm afraid of internal dependencies rn
|
||||
if($year < 1004)
|
||||
$year = 1004;
|
||||
$this->user_birthdate = $month < 1 || $day < 1 ? null : sprintf('%04d-%02d-%02d', $year, $month, $day);
|
||||
return $this;
|
||||
}
|
||||
|
@ -652,14 +657,13 @@ class User implements HasRankInterface {
|
|||
}
|
||||
|
||||
public static function validateBirthdate(int $year, int $month, int $day, int $yearRange = 100): string {
|
||||
if($year > 0) {
|
||||
if($year < date('Y') - $yearRange || $year > date('Y'))
|
||||
if($day !== 0 && $month !== 0) {
|
||||
if($year > 0 && ($year < date('Y') - $yearRange || $year > date('Y')))
|
||||
return 'year';
|
||||
$checkYear = $year;
|
||||
} else $checkYear = date('Y');
|
||||
|
||||
if(!($day === 0 && $month === 0) && !checkdate($month, $day, $checkYear))
|
||||
return 'date';
|
||||
if(!DateCheck::isValidDate($year, $month, $day))
|
||||
return 'date';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue