Lines 90.32% 28 / 31
Functions and Methods 60.00% 3 / 5
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
DistrictParser 90.32% 28 / 31 60.00% 3 / 5 10.09 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 parse 86.66% 13 / 15 0.00% 0 / 1 3.02
 getArea 100.00% 4 / 4 100.00% 1 / 1 1
 getPopulation 100.00% 3 / 3 100.00% 1 / 1 1
 getSingleMatch 87.50% 7 / 8 0.00% 0 / 1 4.03
1<?php
2
3declare(strict_types=1);
4
5namespace Districts\Scraper\Domain\Gdansk;
6
7use Districts\Scraper\Domain\DistrictDTO;
8use Districts\Scraper\Domain\Exception\InvalidHtmlException;
9use Districts\Scraper\Domain\Exception\ParsingException;
10use Districts\Scraper\Domain\HtmlFinder;
11
12readonly class DistrictParser
13{
14    public function __construct(
15        private HtmlFinder $htmlFinder,
16    ) {
17    }
18
19    public function parse(string $html): DistrictDTO
20    {
21        // This is an HTML fragment instead of a valid document. Add a charset
22        // declaration in order to decode the text properly.
23        $fixedHtml = "<html><head><meta charset=\"utf-8\"></head><body>{$html}</body></html>";
24        $xpath = "//div[contains(@class, 'opis')]/div";
25        try {
26            $nodes = $this->htmlFinder->findNodes($fixedHtml, $xpath);
27        } catch (InvalidHtmlException) {
28            throw new ParsingException();
29        }
30        if (count($nodes) < 1) {
31            throw new ParsingException();
32        }
33        $texts = array_map(
34            static fn ($node) => $node->textContent,
35            $nodes
36        );
37        $name = $texts[0];
38        $area = self::getArea($texts);
39        $population = self::getPopulation($texts);
40        return new DistrictDTO($name, $area, $population);
41    }
42
43    /**
44     * @param string[] $texts
45     */
46    private static function getArea(array $texts): float
47    {
48        $regexp = "/Powierzchnia:[[:space:]]+([0-9]+(,[0-9]+){0,1})[[:space:]]+km/";
49        $match = self::getSingleMatch($texts, $regexp);
50        $area = str_replace(",", ".", $match); // decimal point
51        return floatval($area);
52    }
53
54    /**
55     * @param string[] $texts
56     */
57    private static function getPopulation(array $texts): int
58    {
59        $regexp = "/Liczba[[:space:]]ludno.+ci:[[:space:]]+([0-9]+)[[:space:]]/";
60        $match = self::getSingleMatch($texts, $regexp);
61        return intval($match);
62    }
63
64    /**
65     * @param string[] $texts
66     */
67    private static function getSingleMatch(array $texts, string $regexp): string
68    {
69        $allMatches = [];
70        foreach ($texts as $text) {
71            $currentMatches = [];
72            if (preg_match($regexp, $text, $currentMatches) === 1) {
73                $allMatches[] = $currentMatches[1];
74            }
75        }
76        if (count($allMatches) !== 1) {
77            throw new ParsingException();
78        }
79        return $allMatches[0];
80    }
81}