Passed
Branch development-2.0 (f1893c)
by Jonathan
06:14
created

AssertCultureTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 33
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assertCulture() 0 7 2
A getCulturesFilename() 0 9 2
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
 */
22
trait AssertCultureTrait
23
{
24
    /**
25
     * Validate culture
26
     *
27
     * @param string $value
28
     * @param string $message
29
     *
30
     * @return null
31
     */
32 4
    public static function assertCulture(string $value, string $message = '')
33
    {
34 4
        $haystack = include static::getCulturesFilename();
35 4
        $format   = '%s contains an unsupported culture';
36 4
        $message  = sprintf($message ?: $format, static::valueToString($value));
37
38 4
        return static::oneOf($value, $haystack, $message);
39
    }
40
41
    /**
42
     * Return resource filename, containing cultures array
43
     *
44
     * @return string
45
     */
46 4
    public static function getCulturesFilename(): string
47
    {
48 4
        $filename = __DIR__ . '/../../data/cultures.php';
49
50 4
        if (is_readable($filename)) {
51 4
            $filename = realpath($filename);
52
        }
53
54 4
        return $filename;
55
    }
56
}
57