Skip to content

Commit

Permalink
Merge pull request #81 from samatcd/feat/get-available-regions
Browse files Browse the repository at this point in the history
Add getHolidaysAvailableRegions() method
  • Loading branch information
kylekatarnls authored Jul 30, 2021
2 parents 855f09e + 0eb1530 commit 122636a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Business days methods are now available on any Carbon instance
used anywhere later.

You can also just enable methods on Carbon then set region/holidays later:

```php
<?php

Expand Down Expand Up @@ -123,6 +124,7 @@ And we add a lot of syntaxical sugar:
#### setHolidaysRegion / getHolidaysRegion

To select the set of holidays of a region, use:

```php
Carbon::parse('2000-12-25 00:00:00')->isHoliday(); // false
Carbon::setHolidaysRegion('us');
Expand Down Expand Up @@ -348,6 +350,7 @@ Carbon::isBusinessDay() // true if today is a business day

Add days to the date (Carbon instance) to jump to the next business day (skipping
holidays and week-ends).

```php
Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-12')->nextBusinessDay()->format('Y-m-d'); // 2018-01-16
Expand All @@ -363,6 +366,7 @@ echo Carbon::nextBusinessDay()->format('Y-m-d'); // returns the next business da

Sub days to the date (Carbon instance) to jump to the previous business day (skipping
holidays and week-ends).

```php
Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-12')->previousBusinessDay()->format('Y-m-d'); // 2018-01-11
Expand All @@ -379,6 +383,7 @@ echo Carbon::previousBusinessDay()->format('Y-m-d'); // returns the previous bus
Returns the current date (Carbon instance) if it's a business day, else
add days to jump to the next business day (skipping
holidays and week-ends).

```php
Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-12')->currentOrNextBusinessDay()->format('Y-m-d'); // 2018-01-12
Expand All @@ -395,6 +400,7 @@ echo Carbon::currentOrNextBusinessDay() // equivalent to Carbon::today()->curren
Returns the current date (Carbon instance) if it's a business day, else
sub days to jump to the previous business day (skipping
holidays and week-ends).

```php
Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-12')->currentOrPreviousBusinessDay()->format('Y-m-d'); // 2018-01-12
Expand All @@ -409,6 +415,7 @@ echo Carbon::currentOrPreviousBusinessDay() // equivalent to Carbon::today()->cu
#### addBusinessDays

Add days to the date (Carbon instance) skipping holidays and week-ends.

```php
Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-10')->addBusinessDays(4)->format('Y-m-d'); // 2018-01-17
Expand All @@ -423,6 +430,7 @@ Alias addBusinessDays.
#### subBusinessDays or subtractBusinessDays

Sub days to the date (Carbon instance) skipping holidays and week-ends.

```php
Carbon::setHolidaysRegion('us-national');
echo Carbon::parse('2018-01-17')->subBusinessDays(4)->format('Y-m-d'); // 2018-01-12
Expand Down Expand Up @@ -629,7 +637,7 @@ Add an extra workday to the list of a given region.
#### setBusinessDayChecker

Customize the way to determine if a date is a business day or not.

```php
// Global way
Carbon::setBusinessDayChecker(function (CarbonInterface $date) {
Expand All @@ -648,14 +656,15 @@ $date->setBusinessDayChecker($someFunction);
```

If not set or set to `null`, the default calculation is:

```php
$date->isExtraWorkday() or ($date->isWeekday() and !$date->isHoliday())
```

#### setHolidayGetter

Customize the way to determine if a date is a holiday and which one it is.

```php
// Global way
Carbon::setHolidayGetter(function (string $region, CarbonInterface $self, callable $fallback) {
Expand Down Expand Up @@ -721,6 +730,14 @@ Get stored array of data for current holiday (`null` if the current day is not a
Carbon::parse('2020-12-25')->getHolidayData()
```

#### getHolidaysAvailableRegions

Get an array of available regions that can be selected.

```php
Carbon::getHolidaysAvailableRegions()
```

### Laravel

To enable business-day globally in Laravel, set default holidays settings in the config file **config/carbon.php**
Expand Down
33 changes: 33 additions & 0 deletions src/Cmixin/BusinessDay/HolidaysList.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class HolidaysList extends MixinBase
*/
public $holidaysRegion = null;

/**
* @var array|null
*/
public $availableRegions = null;

/**
* Return a standardized region name.
*
Expand All @@ -44,6 +49,34 @@ public function standardizeHolidaysRegion()
};
}

/**
* Get an array of available holidays regions.
*
* @return \Closure
*/
public function getHolidaysAvailableRegions()
{
$mixin = $this;

/**
* Get the current holidays region.
*
* @return array
*/
return static function () use ($mixin) {
if (is_null($mixin->availableRegions)) {
$mixin->availableRegions = array_map(
static function ($file) {
return pathinfo($file, PATHINFO_FILENAME);
},
glob(__DIR__.'/../Holidays/*.php')
);
}

return $mixin->availableRegions;
};
}

/**
* Set the holidays region (see src/Cmixin/Holidays for examples).
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Cmixin/BusinessDayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,4 +735,15 @@ public function testSetExtraWorkdayGetter()
self::assertTrue($carbon::parse('2022-02-02')->isExtraWorkday());
self::assertSame('Too even day', $carbon::parse('2022-02-02')->getExtraWorkdayId());
}

public function testGetHolidaysAvailableRegions()
{
$carbon = static::CARBON_CLASS;
BusinessDay::enable($carbon, 'fr-national');

$actual = $carbon::getHolidaysAvailableRegions();

self::assertIsArray($actual);
self::assertContains('fr-national', $actual);
}
}

0 comments on commit 122636a

Please sign in to comment.