Skip to content

Commit

Permalink
added carbon macros
Browse files Browse the repository at this point in the history
  • Loading branch information
PyaeSoneAungRgn committed Feb 18, 2023
1 parent 02cecc4 commit bd42c65
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Carbon/IsIndependenceDay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PyaeSoneAung\LaravelMyanmarTools\Carbon;

class IsIndependenceDay
{
public function __invoke()
{
return function (): bool {
/** @var \Illuminate\Support\Carbon $this */
if ($this->year < 1948) {
return false;
}

return $this->month === 1 && $this->day === 4;
};
}
}
14 changes: 14 additions & 0 deletions src/Carbon/IsLabourDay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace PyaeSoneAung\LaravelMyanmarTools\Carbon;

class IsLabourDay
{
public function __invoke()
{
return function (): bool {
/** @var \Illuminate\Support\Carbon $this */
return $this->month === 5 && $this->day === 1;
};
}
}
18 changes: 18 additions & 0 deletions src/Carbon/IsMartyrsDay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PyaeSoneAung\LaravelMyanmarTools\Carbon;

class IsMartyrsDay
{
public function __invoke()
{
return function (): bool {
/** @var \Illuminate\Support\Carbon $this */
if ($this->year < 1947) {
return false;
}

return $this->month === 7 && $this->day === 19;
};
}
}
18 changes: 18 additions & 0 deletions src/Carbon/IsPeasantsDay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PyaeSoneAung\LaravelMyanmarTools\Carbon;

class IsPeasantsDay
{
public function __invoke()
{
return function (): bool {
/** @var \Illuminate\Support\Carbon $this */
if ($this->year < 1962) {
return false;
}

return $this->month === 3 && $this->day === 2;
};
}
}
18 changes: 18 additions & 0 deletions src/Carbon/IsUnionDay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PyaeSoneAung\LaravelMyanmarTools\Carbon;

class IsUnionDay
{
public function __invoke()
{
return function (): bool {
/** @var \Illuminate\Support\Carbon $this */
if ($this->year < 1947) {
return false;
}

return $this->month === 2 && $this->day === 12;
};
}
}
23 changes: 23 additions & 0 deletions src/LaravelMyanmarToolsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -53,6 +54,9 @@ public function boot()
});

Collection::make($this->queryBuilderMacros())
->reject(function ($class, $macro) {
return QueryBuilder::hasMacro($macro);
})
->each(function ($class, $macro) {
QueryBuilder::macro($macro, app($class)());
});
Expand All @@ -61,6 +65,14 @@ public function boot()
->each(function ($class, $extend) {
Validator::extend($extend, app($class)(), $this->getErrorMessage($extend));
});

Collection::make($this->carbonMacros())
->reject(function ($class, $macro) {
return Carbon::hasMacro($macro);
})
->each(function ($class, $macro) {
Carbon::macro($macro, app($class)());
});
}

private function strMacros(): array
Expand Down Expand Up @@ -171,6 +183,17 @@ private function validatorExtends(): array
];
}

private function carbonMacros(): array
{
return [
'isIndependenceDay' => \PyaeSoneAung\LaravelMyanmarTools\Carbon\IsIndependenceDay::class,
'isUnionDay' => \PyaeSoneAung\LaravelMyanmarTools\Carbon\IsUnionDay::class,
'isPeasantsDay' => \PyaeSoneAung\LaravelMyanmarTools\Carbon\IsPeasantsDay::class,
'isLabourDay' => \PyaeSoneAung\LaravelMyanmarTools\Carbon\IsLabourDay::class,
'isMartyrsDay' => \PyaeSoneAung\LaravelMyanmarTools\Carbon\IsMartyrsDay::class,
];
}

private function getPackageName(): string
{
return 'laravelMyanmarTools';
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/CarbonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Support\Carbon;

it('can check independence day', function () {
expect(Carbon::parse('2023-01-04')->isIndependenceDay())->toBeTrue();
expect(Carbon::parse('1947-01-04')->isIndependenceDay())->toBeFalse();
});

it('can check union day', function () {
expect(Carbon::parse('2023-02-12')->isUnionDay())->toBeTrue();
expect(Carbon::parse('1946-02-12')->isUnionDay())->toBeFalse();
});

it('can check peasants\' day', function () {
expect(Carbon::parse('2023-03-02')->isPeasantsDay())->toBeTrue();
expect(Carbon::parse('1961-03-02')->isPeasantsDay())->toBeFalse();
});

it('can check labour day', function () {
expect(Carbon::parse('2023-05-01')->isLabourDay())->toBeTrue();
expect(Carbon::parse('2023-05-02')->isLabourDay())->toBeFalse();
});

it('can check martyr\'s day', function () {
expect(Carbon::parse('2023-07-19')->isMartyrsDay())->toBeTrue();
expect(Carbon::parse('1946-07-19')->isMartyrsDay())->toBeFalse();
});

0 comments on commit bd42c65

Please sign in to comment.