Completed
Push — master ( c48d97...353cc7 )
by Mario
13:39 queued 03:42
created

serializer_test.php ➔ readJson()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
require_once __DIR__ . '/../../vendor/autoload.php';
4
5
use Marek\OpenWeatherMap\Factory\DenormalizerFactory;
6
use Marek\OpenWeatherMap\Denormalizer\UltravioletIndexDenormalizer;
7
use Marek\OpenWeatherMap\API\Value\Response\UltravioletIndex\UltravioletIndex;
8
use Marek\OpenWeatherMap\API\Value\Response\UltravioletIndex\AggregatedUltravioletIndex;
9
10
$data = readJson('uv_index', 'history');
11
12
$denormalizerFactory = new DenormalizerFactory();
13
$denormalizer = new UltravioletIndexDenormalizer($denormalizerFactory->create());
14
15
/** @var \Marek\OpenWeatherMap\API\Value\Response\UltravioletIndex\UltravioletIndex $res */
16
$res = $denormalizer->denormalize($data, new AggregatedUltravioletIndex());
17
18
dump($res);
19
20
function readJson(string $service, string $file): array
21
{
22
    $file = __DIR__ . '/responses/' . $service . '/' . $file . '.json';
23
24
    if (!file_exists($file)) {
25
        throw new InvalidArgumentException('Invalid argument provided for json file.');
26
    }
27
28
    try {
29
        return json_decode(
30
            file_get_contents($file), true, 512, JSON_THROW_ON_ERROR
31
        );
32
    } catch (JsonException $exception) {
33
        throw new \Exception('Invalid JSON provided.');
34
    }
35
}
36