Lines 100.00% 37 / 37
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
TravelMap 100.00% 37 / 37 100.00% 2 / 2 4 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 fromJson 100.00% 36 / 36 100.00% 1 / 1 3
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Client\Api\TravelMap;
6
7use Lorekeeper\Core\Game\Level\LevelRange;
8use Lorekeeper\Core\World\Map\MapDescription;
9use Lorekeeper\Core\World\Map\MapId;
10use Lorekeeper\Core\World\Map\MapName;
11use Lorekeeper\Core\World\Map\MapTitle;
12use Lorekeeper\Core\World\Region\RegionId;
13use Lorekeeper\Core\World\Region\RegionMap;
14use Lorekeeper\Core\World\Region\RegionName;
15use Psl\Option;
16use Psl\Type;
17use Psl\Type\Exception\CoercionException;
18
19final readonly class TravelMap
20{
21    /** @param list<TravelMapRegion> $regions */
22    public function __construct(
23        public array $regions,
24    ) {}
25
26    /** @throws TravelMapUnavailableException */
27    public static function fromJson(string $json): self
28    {
29        $type = Type\json_decoded(Type\vec(Type\shape([
30            'regionID' => Type\positive_int(),
31            'regionName' => Type\non_empty_string(),
32            'maps' => Type\optional(Type\vec(Type\shape([
33                'mapID' => Type\positive_int(),
34                'mapName' => Type\non_empty_string(),
35                'mapTitle' => Type\non_empty_string(),
36                'description' => Type\string(),
37                'bUpgrade' => Type\bool(),
38                'minLevel' => Type\int_range(min: 0, max: 100),
39                'maxLevel' => Type\int_range(min: 0, max: 100),
40            ], allowUnknownFields: true))),
41        ], allowUnknownFields: true)));
42
43        try {
44            $regions = $type->coerce($json);
45        } catch (CoercionException $exception) {
46            throw new TravelMapUnavailableException('Unexpected travel map response.', previous: $exception);
47        }
48
49        return new self(array_map(
50            static fn(array $region): TravelMapRegion => new TravelMapRegion(
51                new RegionId($region['regionID']),
52                new RegionName($region['regionName']),
53                array_map(static function (array $map): TravelMapEntry {
54                    $description = trim($map['description']);
55
56                    return new TravelMapEntry(
57                        new MapId($map['mapID']),
58                        new RegionMap(
59                            new MapName($map['mapName']),
60                            new MapTitle($map['mapTitle']),
61                            $description === '' ? Option\none() : Option\some(new MapDescription($description)),
62                            new LevelRange($map['minLevel'], $map['maxLevel']),
63                        ),
64                        $map['bUpgrade'],
65                    );
66                }, $region['maps'] ?? []),
67            ),
68            $regions,
69        ));
70    }
71}