suilven/uk-covid-govt-api-client

dev-master 2020-09-23 22:27 UTC

This package is auto-updated.

Last update: 2024-04-24 07:11:43 UTC


README

Build Status Scrutinizer Code Quality codecov.io

Latest Stable Version Latest Unstable Version Total Downloads License Monthly Downloads Daily Downloads composer.lock

GitHub Code Size GitHub Repo Size GitHub Last Commit GitHub Activity GitHub Issues

codecov.io

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.