Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace GeoIp2\Record;
6
 
7
abstract class AbstractNamedRecord implements \JsonSerializable
8
{
9
    /**
10
     * @var string|null The name based on the locales list
11
     *                  passed to the constructor. This attribute is returned by all location
12
     *                  services and databases.
13
     */
14
    public readonly ?string $name;
15
 
16
    /**
17
     * @var array<string, string> An array map where the keys are locale codes
18
     *                            and the values are names. This attribute is returned by all location
19
     *                            services and databases.
20
     */
21
    public readonly array $names;
22
 
23
    /**
24
     * @ignore
25
     *
26
     * @param array<string, mixed> $record
27
     * @param list<string>         $locales
28
     */
29
    public function __construct(array $record, array $locales = ['en'])
30
    {
31
        $this->names = $record['names'] ?? [];
32
 
33
        foreach ($locales as $locale) {
34
            if (isset($this->names[$locale])) {
35
                $this->name = $this->names[$locale];
36
 
37
                return;
38
            }
39
        }
40
        $this->name = null;
41
    }
42
 
43
    /**
44
     * @return array<string, mixed>
45
     */
46
    public function jsonSerialize(): array
47
    {
48
        $js = [];
49
        if (!empty($this->names)) {
50
            $js['names'] = $this->names;
51
        }
52
 
53
        return $js;
54
    }
55
}