|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* ReportingCloud PHP SDK |
|
6
|
|
|
* |
|
7
|
|
|
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
|
8
|
|
|
* |
|
9
|
|
|
* @link https://www.reporting.cloud to learn more about ReportingCloud |
|
10
|
|
|
* @link https://git.io/Jejj2 for the canonical source repository |
|
11
|
|
|
* @license https://git.io/Jejjr |
|
12
|
|
|
* @copyright © 2022 Text Control GmbH |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace TxTextControl\ReportingCloud\Assert; |
|
16
|
|
|
|
|
17
|
|
|
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException; |
|
18
|
|
|
use TxTextControl\ReportingCloud\Stdlib\Path; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Trait AssertLanguageTrait |
|
22
|
|
|
* |
|
23
|
|
|
* @package TxTextControl\ReportingCloud |
|
24
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
|
25
|
|
|
*/ |
|
26
|
|
|
trait AssertLanguageTrait |
|
27
|
|
|
{ |
|
28
|
|
|
use ValueToStringTrait; |
|
29
|
|
|
use AssertOneOfTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Check value is a valid language |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $value |
|
35
|
|
|
* @param string $message |
|
36
|
|
|
* |
|
37
|
|
|
* @return void |
|
38
|
|
|
* @throws InvalidArgumentException |
|
39
|
|
|
*/ |
|
40
|
10 |
|
public static function assertLanguage(string $value, string $message = ''): void |
|
41
|
|
|
{ |
|
42
|
10 |
|
$haystack = self::getDictionaries(); |
|
43
|
10 |
|
$format = 0 === strlen($message) ? '%1$s contains an unsupported language' : $message; |
|
44
|
10 |
|
$message = sprintf($format, self::valueToString($value)); |
|
45
|
|
|
|
|
46
|
10 |
|
self::assertOneOf($value, $haystack, $message); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Return the filename, containing languages aka dictionaries array |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
12 |
|
public static function getDictionariesFilename(): string |
|
55
|
|
|
{ |
|
56
|
12 |
|
return sprintf('%1$s/dictionaries.php', Path::data()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Return languages aka dictionaries array |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
10 |
|
private static function getDictionaries(): array |
|
65
|
|
|
{ |
|
66
|
10 |
|
$filename = self::getDictionariesFilename(); |
|
67
|
|
|
|
|
68
|
10 |
|
return (array) include $filename; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|