suilven / uk-covid-govt-api-client
API Client to UK Govt Covid API
Requires
- php: ~7.1
- league/climate: ^3.5
- nesbot/carbon: ^2.40
- phpunit/phpunit: ^7 || ^8 || ^9
- vanilla/garden-cli: ^2.2
Requires (Dev)
- ergebnis/composer-normalize: ^2.5
- php-parallel-lint/php-console-highlighter: ^0.5.0
- php-parallel-lint/php-parallel-lint: ^1.2
- php-vcr/phpunit-testlistener-vcr: ^3.2
- phpstan/extension-installer: ^1.0
- phpstan/phpstan-strict-rules: ^0.12.5
- slevomat/coding-standard: ^6.4
- suilven/php-travis-enhancer: ^1.0
- vimeo/psalm: ^3.16
This package is auto-updated.
Last update: 2024-11-24 08:37:27 UTC
README
Background
The UK Government has provided an API for accessing COVID data. The documentation is poor, see https://coronavirus.data.gov.uk/developers-guide , and if the structure parameter is missing the API call redirects to the documentation above (!).
I am not sure about how often and indeed how soon the data is updated, but hopefully it proves of use to the PHP community.
Installation
Existing PHP Environment
If you have an existing PHP environment, install as follows:
composer require suilven/uk-covid-govt-api-client
Docker
A Docker environment is provided for, primarily to run unit tests
sudo docker-compose up -d phpcli
sudo docker-compose exec /bin/bash
composer install
Usage
Get An Area Type
An area type is mandatory, but defaults to a level of nation. This can be set as follows:
use Suilven\CovidAPIClient\Factory\AreaTypeFactory;
use Suilven\CovidAPIClient\Filter\AreaType;
$factory = new AreaTypeFactory();
$areaType = $factory->getAreaType(AreaType::NATION);
public const OVERVIEW = 'overview';
public const NATION = 'nation';
public const REGION = 'region';
public const NHS_REGION = 'nhsRegion';
public const UPPER_TIER = 'utla';
public const LOWER_TIER = 'ltla';
Valid values for AreaType are as follows:
AreaType::OVERVIEW
AreaType::NATION
AreaType::REGION
AreaType::NHS_REGION
AreaType::UPPER_TIER
AreaType::LOWER_TIER
Valid values for upper and lower tier area names can be found in the docs directory.
Add a Filter
$factory = new AreaTypeFactory();
$areaType = $factory->getAreaType(AreaType::UPPER_TIER);
$client = new APIClient();
$filter = new Filter();
$filter->setAreaType($areaType);
$filter->setAreaName('Blackburn with Darwen');
$result = $client->getData([$filter]);
One can set a date, an area code or area name as above.
Get Results
Each recodr (of an array) is a Suilven\CovidAPIClient\Model<SingleEntry
object.
$record = $result->getRecords();
Get Data For An Individual Result
$record->getNewCasesByPublishDate();
$record->getNewDeaths28DaysByDeathDate();
$record->getCumCasesByPublishDate();
$record->getCumDeaths28DaysByDeathDate();
$record->getCumCasesByPublishDate();
$this->assertEquals('S12000027', $record->getAreaCode();
$this->assertEquals('Shetland Islands', $record->getAreaName();
$this->assertEquals('2020-09-21', $record->getDate())
COMMENTS
This is a functional but rough implementation, although well tested.
I still do not fully understand how the data behind the API is updated, this is a bit of a black box.