summaryrefslogtreecommitdiffstats
path: root/src/data.php
blob: 1028a8f3a0d97bb567a660a2814013e320baa8bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
function get_node_value($from, string $name): string
{
  return $from->getElementsByTagName($name)->item(0)->nodeValue;
}
function get_doc(): DOMDocument
{
  $doc = new DOMDocument();
  $path = "%PWD%src/db.xml";
  $doc->load($path);
  return $doc;
}
function get_data(): array
{
  $doc = get_doc();
  $out = [];
  $games = $doc->getElementsByTagName("game");
  $i = 0;
  while ($game = $games->item($i++)) {
    $out[] = [
      "id" => $game->getAttribute("id"),
      "order" => $game->getAttribute("order"),
      "name" => get_node_value($game, "name"),
      "thumbnail" => get_node_value($game, "thumbnail"),
      "author" => get_node_value($game, "author"),
      "magazine" => get_node_value($game, "magazine"),
    ];
  }
  usort($out, function ($a, $b) {
    return $a["order"] - $b["order"];
  });
  return [$out, $doc->getElementsByTagName("count")->item(0)->nodeValue];
}

$doc = get_doc();
$xpath = new DOMXpath($doc);
$game = $xpath->query('//game[@id="2"]/name')->item(0);
// var_dump($game);