1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* ReportingCloud PHP Wrapper |
6
|
|
|
* |
7
|
|
|
* PHP wrapper 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://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository |
11
|
|
|
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md |
12
|
|
|
* @copyright © 2019 Text Control GmbH |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace TxTextControl\ReportingCloud\Assert; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Trait AssertLanguageTrait |
19
|
|
|
* |
20
|
|
|
* @package TxTextControl\ReportingCloud |
21
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
22
|
|
|
*/ |
23
|
|
|
trait AssertLanguageTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Validate language |
27
|
|
|
* |
28
|
|
|
* @param string $value |
29
|
|
|
* @param string $message |
30
|
|
|
* |
31
|
|
|
* @return null |
32
|
|
|
* @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
33
|
|
|
*/ |
34
|
15 |
|
public static function assertLanguage(string $value, string $message = '') |
35
|
|
|
{ |
36
|
15 |
|
$haystack = self::getDictionaries(); |
37
|
15 |
|
$format = $message ?: '%s contains an unsupported language'; |
38
|
15 |
|
$message = sprintf($format, self::valueToString($value)); |
39
|
|
|
|
40
|
15 |
|
return self::oneOf($value, $haystack, $message); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return the filename, containing languages aka dictionaries array |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
15 |
|
public static function getDictionariesFilename(): string |
49
|
|
|
{ |
50
|
15 |
|
$filename = __DIR__ . '/../../data/dictionaries.php'; |
51
|
15 |
|
$filename = realpath($filename); |
52
|
|
|
|
53
|
15 |
|
return $filename; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return languages aka dictionaries array |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
15 |
|
private static function getDictionaries(): array |
62
|
|
|
{ |
63
|
15 |
|
$filename = self::getDictionariesFilename(); |
64
|
|
|
|
65
|
15 |
|
return include $filename; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|