mirror of
https://github.com/holidayapi/holidayapi-php.git
synced 2025-06-21 12:16:31 +00:00
feat: add workdays endpoint
Added support for our new workdays endpoint that allows you to get the number of working days for a country between two dates.
This commit is contained in:
parent
c12f512f8a
commit
95bba75265
5 changed files with 172 additions and 15 deletions
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
|
@ -23,7 +23,7 @@ jobs:
|
|||
- name: Run Tests
|
||||
run: vendor/bin/phpunit --coverage-clover ./coverage.xml
|
||||
- name: Upload Coverage
|
||||
if: ${{ matrix.php-version == '7.4' }}
|
||||
if: ${{ matrix.php-version == '8.0' }}
|
||||
uses: codecov/codecov-action@v1
|
||||
with:
|
||||
file: ./coverage.xml
|
||||
|
|
26
README.md
26
README.md
|
@ -8,19 +8,6 @@
|
|||
Official PHP library for [Holiday API](https://holidayapi.com) providing quick
|
||||
and easy access to holiday information from applications written in PHP.
|
||||
|
||||
## Migrating from 1.x
|
||||
|
||||
Please note, version 2.x of this library is a full rewrite of the 1.x series.
|
||||
The interfacing to the library has been simplified and existing applications
|
||||
upgrading to 2.x will need to be updated.
|
||||
|
||||
| Version 1.x Syntax (Old) | Version 2.x Syntax (New) |
|
||||
|--------------------------------------------|-----------------------------------------------------------|
|
||||
| `$holiday_api = new \HolidayAPI\v1($key);` | `$holiday_api = new \HolidayAPI\Client(['key' => $key]);` |
|
||||
|
||||
Version 1.x of the library can still be found
|
||||
[here](https://github.com/joshtronic/php-holidayapi).
|
||||
|
||||
## Documentation
|
||||
|
||||
Full documentation of the Holiday API endpoints is available
|
||||
|
@ -265,3 +252,16 @@ $holiday_api->workday([
|
|||
'days' => 7,
|
||||
]);
|
||||
```
|
||||
|
||||
### Workdays
|
||||
|
||||
#### Fetch number of workdays between two dates
|
||||
|
||||
```php
|
||||
<?php
|
||||
$holiday_api->workdays([
|
||||
'country' => 'US',
|
||||
'start' => '2019-07-01',
|
||||
'end' => '2019-07-10',
|
||||
]);
|
||||
```
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "holidayapi/holidayapi-php",
|
||||
"description": "Official PHP library for Holiday API",
|
||||
"version": "2.3.0",
|
||||
"version": "2.4.0",
|
||||
"type": "library",
|
||||
"keywords": [
|
||||
"calendar",
|
||||
|
|
|
@ -97,5 +97,18 @@ class Client
|
|||
|
||||
return $this->request('workday', $request);
|
||||
}
|
||||
|
||||
public function workdays($request)
|
||||
{
|
||||
if (!isset($request['country'])) {
|
||||
throw new \Exception('Missing country');
|
||||
} elseif (!isset($request['start'])) {
|
||||
throw new \Exception('Missing start date');
|
||||
} elseif (!isset($request['end'])) {
|
||||
throw new \Exception('Missing end date');
|
||||
}
|
||||
|
||||
return $this->request('workdays', $request);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -790,5 +790,149 @@ class ClientTest extends TestCase
|
|||
$this->assertSame('Internal server error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testReturnWorkdays()
|
||||
{
|
||||
$url = self::BASE_URL . 'workdays?key=' . self::KEY . '&country=US&start=2019-07-01&end=2019-07-10';
|
||||
|
||||
$request = new Request(array(
|
||||
'execute' => array(
|
||||
$url => function ()
|
||||
{
|
||||
return json_encode(array(
|
||||
'status' => 200,
|
||||
'workdays' => 7,
|
||||
));
|
||||
},
|
||||
),
|
||||
));
|
||||
|
||||
$client = new Client(array('key' => self::KEY, 'handler' => $request));
|
||||
|
||||
$this->assertEquals(array(
|
||||
'status' => 200,
|
||||
'workdays' => 7,
|
||||
), $client->workdays(array(
|
||||
'country' => 'US',
|
||||
'start' => '2019-07-01',
|
||||
'end' => '2019-07-10',
|
||||
)));
|
||||
}
|
||||
|
||||
public function testWorkdaysCountryMissing()
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
|
||||
$assertRegExp = 'assertMatchesRegularExpression';
|
||||
} else {
|
||||
$assertRegExp = 'assertRegExp';
|
||||
}
|
||||
|
||||
$client = new Client(array('key' => self::KEY));
|
||||
|
||||
try {
|
||||
$client->workdays(array());
|
||||
} catch (\Exception $e) {
|
||||
$this->$assertRegExp('/missing country/i', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testWorkdaysStartMissing()
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
|
||||
$assertRegExp = 'assertMatchesRegularExpression';
|
||||
} else {
|
||||
$assertRegExp = 'assertRegExp';
|
||||
}
|
||||
|
||||
$client = new Client(array('key' => self::KEY));
|
||||
|
||||
try {
|
||||
$client->workdays(array('country' => 'US'));
|
||||
} catch (\Exception $e) {
|
||||
$this->$assertRegExp('/missing start date/i', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testWorkdaysEndMissing()
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
|
||||
$assertRegExp = 'assertMatchesRegularExpression';
|
||||
} else {
|
||||
$assertRegExp = 'assertRegExp';
|
||||
}
|
||||
|
||||
$client = new Client(array('key' => self::KEY));
|
||||
|
||||
try {
|
||||
$client->workdays(array(
|
||||
'country' => 'US',
|
||||
'start' => '2019-07-01',
|
||||
));
|
||||
} catch (\Exception $e) {
|
||||
$this->$assertRegExp('/missing end date/i', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testWorkdaysRaise4xxErrors()
|
||||
{
|
||||
$url = self::BASE_URL . 'workdays?key=' . self::KEY . '&country=US&start=2019-07-01&end=2019-07-10';
|
||||
|
||||
$request = new Request(array(
|
||||
'execute' => array(
|
||||
$url => function ()
|
||||
{
|
||||
return json_encode(array(
|
||||
'status' => 429,
|
||||
'error' => 'Rate limit exceeded',
|
||||
));
|
||||
},
|
||||
),
|
||||
));
|
||||
|
||||
$client = new Client(array('key' => self::KEY, 'handler' => $request));
|
||||
|
||||
try {
|
||||
$client->workdays(array(
|
||||
'country' => 'US',
|
||||
'start' => '2019-07-01',
|
||||
'end' => '2019-07-10',
|
||||
));
|
||||
} catch (\Exception $e) {
|
||||
$this->assertSame(429, $e->getCode());
|
||||
$this->assertSame('Rate limit exceeded', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testWorkdaysRaise5xxErrors()
|
||||
{
|
||||
$url = self::BASE_URL . 'workdays?key=' . self::KEY . '&country=US&start=2019-07-01&end=2019-07-10';
|
||||
|
||||
$request = new Request(array(
|
||||
'execute' => array(
|
||||
$url => function ()
|
||||
{
|
||||
return false;
|
||||
},
|
||||
),
|
||||
'error' => array(
|
||||
$url => function ()
|
||||
{
|
||||
return 'Internal server error';
|
||||
},
|
||||
),
|
||||
));
|
||||
|
||||
$client = new Client(array('key' => self::KEY, 'handler' => $request));
|
||||
|
||||
try {
|
||||
$client->workdays(array(
|
||||
'country' => 'US',
|
||||
'start' => '2019-07-01',
|
||||
'end' => '2019-07-10',
|
||||
));
|
||||
} catch (\Exception $e) {
|
||||
$this->assertSame('Internal server error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue