xref: /plugin/combo/ComboStrap/Dictionary.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
1<?php
2
3
4namespace ComboStrap;
5
6
7class Dictionary
8{
9
10    /**
11     * @throws ExceptionCombo
12     */
13    public static function getFrom(string $name): array
14    {
15        $path = Site::getComboDictionaryDirectory()->resolve("$name.json");
16        if (!FileSystems::exists($path)) {
17            throw new ExceptionCombo("The dictionary file ($path) does not exist");
18        }
19        $jsonContent = FileSystems::getContent($path);
20        try {
21            $dict = Json::createFromString($jsonContent)->toArray();
22        } catch (ExceptionCombo $e) {
23            throw new ExceptionCombo("The dictionary ($path) is not a valid json. Error: {$e->getMessage()}");
24        }
25        if ($dict === null) {
26            throw new ExceptionCombo("The returned dictionary of the file ($path) is empty");
27        }
28        return $dict;
29    }
30}
31