|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace PrinsFrank\Standards\Dev\DataSource\Mapping; |
| 4 | + |
| 5 | +use Facebook\WebDriver\WebDriverBy; |
| 6 | +use PrinsFrank\Standards\Dev\DataSource\Sorting\KeySorting; |
| 7 | +use PrinsFrank\Standards\Dev\DataTarget\EnumCase; |
| 8 | +use PrinsFrank\Standards\Dev\DataTarget\EnumCaseAttribute; |
| 9 | +use PrinsFrank\Standards\Dev\DataTarget\SpecFile; |
| 10 | +use PrinsFrank\Standards\URIScheme\Attributes\Status; |
| 11 | +use PrinsFrank\Standards\URIScheme\URIScheme; |
| 12 | +use PrinsFrank\Standards\URIScheme\URISchemeStatus; |
| 13 | +use RuntimeException; |
| 14 | +use stdClass; |
| 15 | +use Symfony\Component\Panther\Client; |
| 16 | +use Symfony\Component\Panther\DomCrawler\Crawler; |
| 17 | +use TypeError; |
| 18 | +use ValueError; |
| 19 | + |
| 20 | +/** |
| 21 | + * @template TDataSet of object{name: string, template: string, description: string, status: string, wellKnown: string, reference: string, notes: string}&stdClass |
| 22 | + * @implements Mapping<TDataSet> |
| 23 | + */ |
| 24 | +class URISchemeMapping implements Mapping { |
| 25 | + public static function url(): string { |
| 26 | + return 'https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml'; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @throws RuntimeException |
| 31 | + * @return list<TDataSet> |
| 32 | + */ |
| 33 | + public static function toDataSet(Client $client, Crawler $crawler): array { |
| 34 | + $dataSet = []; |
| 35 | + foreach ($crawler->filterXPath('//table[@id="table-uri-schemes-1"]/tbody/tr')->getIterator() as $item) { |
| 36 | + $columns = $item->findElements(WebDriverBy::xpath('./td')); |
| 37 | + if ($columns === []) { |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + if (array_key_exists(0, $columns) === false |
| 42 | + || array_key_exists(1, $columns) === false |
| 43 | + || array_key_exists(2, $columns) === false |
| 44 | + || array_key_exists(3, $columns) === false |
| 45 | + || array_key_exists(4, $columns) === false |
| 46 | + || array_key_exists(5, $columns) === false |
| 47 | + || array_key_exists(6, $columns) === false) { |
| 48 | + throw new RuntimeException('Expected exactly 7 columns'); |
| 49 | + } |
| 50 | + |
| 51 | + $record = (object) []; |
| 52 | + $record->name = $columns[0]->getText(); |
| 53 | + $record->template = $columns[1]->getText(); |
| 54 | + $record->description = $columns[2]->getText(); |
| 55 | + $record->status = $columns[3]->getText(); |
| 56 | + $record->wellKnown = $columns[4]->getText(); |
| 57 | + $record->reference = $columns[5]->getText(); |
| 58 | + $record->notes = $columns[6]->getText(); |
| 59 | + |
| 60 | + /** @var TDataSet $record */ |
| 61 | + $dataSet[] = $record; |
| 62 | + } |
| 63 | + |
| 64 | + return $dataSet; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param list<TDataSet> $dataSet |
| 69 | + * @throws TypeError|ValueError |
| 70 | + * @return array<SpecFile> |
| 71 | + */ |
| 72 | + public static function toEnumMapping(array $dataSet): array { |
| 73 | + $URIScheme = new SpecFile(URIScheme::class, KeySorting::class); |
| 74 | + foreach ($dataSet as $dataRow) { |
| 75 | + $URIScheme->addCase(new EnumCase($dataRow->name, $dataRow->name, [new EnumCaseAttribute(Status::class, [URISchemeStatus::from($dataRow->status)])])); |
| 76 | + } |
| 77 | + |
| 78 | + return [$URIScheme]; |
| 79 | + } |
| 80 | +} |
0 commit comments