Skip to content

Commit 5af2743

Browse files
incluido metodos private no topo, deixando os publics abaixo
1 parent 5695960 commit 5af2743

2 files changed

Lines changed: 101 additions & 101 deletions

File tree

src/ValidateDate.php

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,71 @@ private static function validateYear(string $ano, string $mes, string $dia): boo
2323
return false;
2424
}
2525

26+
private static function isCalendarDateTime(string $input): bool
27+
{
28+
$pattern = '/^\d{4}((-?\d{2})(-?\d{2})?)?(T\d{2}(:?\d{2}(:?\d{2}(\.\d+)?)?)?(Z|[+-]\d{2}(:?\d{2})?)?)?$/';
29+
if (!preg_match($pattern, $input)) {
30+
return false;
31+
}
32+
try {
33+
$date = new \DateTime($input);
34+
if (str_contains($input, '-')) {
35+
$parts = explode('T', $input)[0];
36+
if (count(explode('-', $parts)) === 3 && $date->format('Y-m-d') !== $parts) {
37+
return false;
38+
}
39+
}
40+
return true;
41+
} catch (\Exception) {
42+
return false;
43+
}
44+
}
45+
46+
private static function isWeekDate(string $input): bool
47+
{
48+
$pattern = '/^(\d{4})-?W(0[1-9]|[1-4][0-9]|5[0-3])(-?([1-7]))?$/';
49+
if (!preg_match($pattern, $input, $matches)) {
50+
return false;
51+
}
52+
$year = (int)$matches[1];
53+
$week = (int)$matches[2];
54+
if ($week === 53) {
55+
$date = new \DateTime();
56+
$date->setISODate($year, 53);
57+
return $date->format('W') === '53';
58+
}
59+
return true;
60+
}
61+
62+
private static function isOrdinalDate(string $input): bool
63+
{
64+
if (!preg_match('/^(\d{4})-?(\d{3})$/', $input, $matches)) {
65+
return false;
66+
}
67+
$year = (int)$matches[1];
68+
$dayOfYear = (int)$matches[2];
69+
$isLeap = ($year % 4 === 0 && ($year % 100 !== 0 || $year % 400 === 0));
70+
return $dayOfYear >= 1 && $dayOfYear <= ($isLeap ? 366 : 365);
71+
}
72+
73+
private static function isDuration(string $input): bool
74+
{
75+
$pattern = '/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/';
76+
return (bool) preg_match($pattern, $input);
77+
}
78+
79+
private static function isInterval(string $input): bool
80+
{
81+
if (!str_contains($input, '/')) {
82+
return false;
83+
}
84+
$parts = explode('/', $input);
85+
if (count($parts) !== 2) {
86+
return false;
87+
}
88+
return (self::validateDateIso8601($parts[0]) && self::validateDateIso8601($parts[1]));
89+
}
90+
2691
public static function validateDateBrazil(string $data): bool
2792
{
2893
if (strlen($data) < 8) {
@@ -96,69 +161,4 @@ public static function validateDateIso8601(string $input): bool
96161
return self::isCalendarDateTime($input) || self::isDuration($input) || self::isWeekDate($input)
97162
|| self::isOrdinalDate($input) || self::isInterval($input);
98163
}
99-
100-
private static function isCalendarDateTime(string $input): bool
101-
{
102-
$pattern = '/^\d{4}((-?\d{2})(-?\d{2})?)?(T\d{2}(:?\d{2}(:?\d{2}(\.\d+)?)?)?(Z|[+-]\d{2}(:?\d{2})?)?)?$/';
103-
if (!preg_match($pattern, $input)) {
104-
return false;
105-
}
106-
try {
107-
$date = new \DateTime($input);
108-
if (str_contains($input, '-')) {
109-
$parts = explode('T', $input)[0];
110-
if (count(explode('-', $parts)) === 3 && $date->format('Y-m-d') !== $parts) {
111-
return false;
112-
}
113-
}
114-
return true;
115-
} catch (\Exception) {
116-
return false;
117-
}
118-
}
119-
120-
private static function isWeekDate(string $input): bool
121-
{
122-
$pattern = '/^(\d{4})-?W(0[1-9]|[1-4][0-9]|5[0-3])(-?([1-7]))?$/';
123-
if (!preg_match($pattern, $input, $matches)) {
124-
return false;
125-
}
126-
$year = (int)$matches[1];
127-
$week = (int)$matches[2];
128-
if ($week === 53) {
129-
$date = new \DateTime();
130-
$date->setISODate($year, 53);
131-
return $date->format('W') === '53';
132-
}
133-
return true;
134-
}
135-
136-
private static function isOrdinalDate(string $input): bool
137-
{
138-
if (!preg_match('/^(\d{4})-?(\d{3})$/', $input, $matches)) {
139-
return false;
140-
}
141-
$year = (int)$matches[1];
142-
$dayOfYear = (int)$matches[2];
143-
$isLeap = ($year % 4 === 0 && ($year % 100 !== 0 || $year % 400 === 0));
144-
return $dayOfYear >= 1 && $dayOfYear <= ($isLeap ? 366 : 365);
145-
}
146-
147-
private static function isDuration(string $input): bool
148-
{
149-
$pattern = '/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/';
150-
return (bool) preg_match($pattern, $input);
151-
}
152-
153-
private static function isInterval(string $input): bool
154-
{
155-
if (!str_contains($input, '/')) {
156-
return false;
157-
}
158-
$parts = explode('/', $input);
159-
if (count($parts) !== 2) {
160-
return false;
161-
}
162-
return (self::validateDateIso8601($parts[0]) && self::validateDateIso8601($parts[1]));
163-
}
164164
}

tests/UnitValidateDateTest.php

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,6 @@
1212

1313
class UnitValidateDateTest extends TestCase
1414
{
15-
public function testValidateDateBrazil(): void
16-
{
17-
self::assertEquals(true, ValidateDate::validateDateBrazil('29/04/2021'));
18-
self::assertEquals(false, ValidateDate::validateDateBrazil('31/04/2021'));
19-
}
20-
21-
public function testValidateDateAmerican(): void
22-
{
23-
self::assertEquals(true, ValidateDate::validateDateAmerican('2021-04-29'));
24-
self::assertEquals(false, ValidateDate::validateDateAmerican('2021-04-31'));
25-
}
26-
27-
public function testValidateTimeStamp(): void
28-
{
29-
self::assertEquals(true, ValidateDate::validateTimeStamp('2021-04-29 11:17:12'));
30-
self::assertEquals(false, ValidateDate::validateTimeStamp('2021-04-31 11:1'));
31-
}
32-
33-
public function testValidateDateNotFuture(): void
34-
{
35-
$dateNow = new DateTimeImmutable();
36-
$newDateFuture = $dateNow->add(new DateInterval('P32D'))->format('Y-m-d');
37-
self::assertEquals(true, ValidateDate::validateDateNotFuture(Format::dateAmerican('28/12/2022')));
38-
self::assertEquals(false, ValidateDate::validateDateNotFuture($newDateFuture));
39-
}
40-
41-
public function testValidateDateUTCWithoutTimezone(): void
42-
{
43-
self::assertTrue(ValidateDate::validateDateUTCWithoutTimezone('2025-11-20T10:30:00'));
44-
self::assertTrue(ValidateDate::validateDateUTCWithoutTimezone('1999-01-01T00:00:00'));
45-
self::assertFalse(ValidateDate::validateDateUTCWithoutTimezone('2025-11-20T10:30'));
46-
self::assertFalse(ValidateDate::validateDateUTCWithoutTimezone('2025-11-20 10:30:00'));
47-
self::assertFalse(ValidateDate::validateDateUTCWithoutTimezone('2025-11-20T10:30:00Z'));
48-
self::assertFalse(ValidateDate::validateDateUTCWithoutTimezone('2025-11-20T10:30:00+00:00'));
49-
}
50-
5115
private static function biuldDataTestDateIso8601Z(): array
5216
{
5317
return [
@@ -88,6 +52,42 @@ private static function biuldDataDateIsoTest(): array
8852
];
8953
}
9054

55+
public function testValidateDateBrazil(): void
56+
{
57+
self::assertEquals(true, ValidateDate::validateDateBrazil('29/04/2021'));
58+
self::assertEquals(false, ValidateDate::validateDateBrazil('31/04/2021'));
59+
}
60+
61+
public function testValidateDateAmerican(): void
62+
{
63+
self::assertEquals(true, ValidateDate::validateDateAmerican('2021-04-29'));
64+
self::assertEquals(false, ValidateDate::validateDateAmerican('2021-04-31'));
65+
}
66+
67+
public function testValidateTimeStamp(): void
68+
{
69+
self::assertEquals(true, ValidateDate::validateTimeStamp('2021-04-29 11:17:12'));
70+
self::assertEquals(false, ValidateDate::validateTimeStamp('2021-04-31 11:1'));
71+
}
72+
73+
public function testValidateDateNotFuture(): void
74+
{
75+
$dateNow = new DateTimeImmutable();
76+
$newDateFuture = $dateNow->add(new DateInterval('P32D'))->format('Y-m-d');
77+
self::assertEquals(true, ValidateDate::validateDateNotFuture(Format::dateAmerican('28/12/2022')));
78+
self::assertEquals(false, ValidateDate::validateDateNotFuture($newDateFuture));
79+
}
80+
81+
public function testValidateDateUTCWithoutTimezone(): void
82+
{
83+
self::assertTrue(ValidateDate::validateDateUTCWithoutTimezone('2025-11-20T10:30:00'));
84+
self::assertTrue(ValidateDate::validateDateUTCWithoutTimezone('1999-01-01T00:00:00'));
85+
self::assertFalse(ValidateDate::validateDateUTCWithoutTimezone('2025-11-20T10:30'));
86+
self::assertFalse(ValidateDate::validateDateUTCWithoutTimezone('2025-11-20 10:30:00'));
87+
self::assertFalse(ValidateDate::validateDateUTCWithoutTimezone('2025-11-20T10:30:00Z'));
88+
self::assertFalse(ValidateDate::validateDateUTCWithoutTimezone('2025-11-20T10:30:00+00:00'));
89+
}
90+
9191
public function testValidateDateIso8601AllFormats(): void
9292
{
9393
$valid = self::biuldDataDateIsoTest();

0 commit comments

Comments
 (0)