Skip to content

Commit 8c2f42e

Browse files
committed
Removed Carbon dependency
Signed-off-by: victor <victor@4uno.org>
1 parent 5adc9e1 commit 8c2f42e

File tree

3 files changed

+13
-31
lines changed

3 files changed

+13
-31
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"minimum-stability": "dev",
2020
"prefer-stable": true,
2121
"require": {
22-
"php": "7.3.* | 7.4.*"
22+
"php": "7.2.* | 7.3.* | 7.4.*"
2323
},
2424
"suggest": {
2525
"illuminatech/validation-composite": "^1.2"

src/Rules/CurpBirthdate.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33

44
namespace Pollin14\LaravelCurpValidation\Rules;
55

6-
use Carbon\Carbon;
7-
use Carbon\Exceptions\InvalidFormatException;
6+
use DateTime;
87
use Illuminate\Contracts\Validation\Rule;
98
use Illuminate\Support\Str;
109

11-
1210
class CurpBirthdate implements Rule
1311
{
1412
public function passes($attribute, $value)
@@ -22,14 +20,12 @@ public function passes($attribute, $value)
2220

2321
$date = Str::substr($value, 4, 6);
2422

25-
try {
26-
27-
$carbonDate = Carbon::createFromFormat('ymd', $date);
28-
} catch (InvalidFormatException $exception) {
23+
$result = $dateTime = DateTime::createFromFormat('ymd', $date);
24+
if ($result === false) {
2925
return false;
3026
}
3127

32-
return $carbonDate->format('ymd') === $date;
28+
return $dateTime->format('ymd') === $date;
3329
}
3430

3531
public function message()

src/Rules/CurpPenultimateChar.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
namespace Pollin14\LaravelCurpValidation\Rules;
55

6-
use Carbon\Carbon;
7-
use Carbon\Exceptions\InvalidFormatException;
6+
use DateTime;
87
use Illuminate\Contracts\Validation\Rule;
98
use Illuminate\Support\Str;
109

@@ -23,9 +22,10 @@ public function passes($attribute, $value)
2322

2423
$dateString = Str::substr($value, 4, 6);
2524

26-
try {
27-
$date = Carbon::createFromFormat('ymd', $dateString);
28-
} catch (InvalidFormatException $exception) {
25+
26+
$date = DateTime::createFromFormat('ymd', $dateString);
27+
28+
if ($date === false) {
2929
return false;
3030
}
3131

@@ -40,28 +40,14 @@ public function message()
4040
return trans('curp-validation::validation.curp_penultimate_char');
4141
}
4242

43-
private function getRegexp(Carbon $date)
43+
private function getRegexp(DateTime $date)
4444
{
45-
// Fix to Carbon < 2
45+
$limit = DateTime::createFromFormat('Y-m-d h:i:s', '2000-01-01 00:00:00');
4646

47-
48-
if ($this->isBefore($date)) {
47+
if ($date < $limit) {
4948
return '/^.{16}\d/i';
5049
}
5150

5251
return '/^.{16}[a-z]{1}/i';
5352
}
54-
55-
/**
56-
* Carbon 1.* does not has isBefore function
57-
*/
58-
private function isBefore(Carbon $date): bool
59-
{
60-
$limit = Carbon::parse('2000-01-01 00:00:00');
61-
if (method_exists($date, 'isBefore')) {
62-
return $date->isBefore($limit);
63-
}
64-
65-
return $date->diffInDays($limit, false) > 0;
66-
}
6753
}

0 commit comments

Comments
 (0)