Completed
Pull Request — master (#283)
by De Cramer
03:36
created

Countries::getCodeFromCountry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace eXpansion\Framework\Core\Helpers;
4
5
class Countries
6
{
7
    /** @var array  */
8
    protected $countriesMapping = [];
9
10
    /** @var string */
11
    protected $otherCountryCode;
12
13
    /** @var string */
14
    protected $otherCountryLabel;
15
16
    /**
17
     * Countries constructor.
18
     *
19
     * @param array $countriesMapping
20
     * @param string $otherCountryCode
21
     * @param string $otherCountryLabel
22
     */
23 141
    public function __construct(array $countriesMapping, string $otherCountryCode, string $otherCountryLabel)
24
    {
25 141
        $this->countriesMapping = $countriesMapping;
26 141
        $this->otherCountryCode = $otherCountryCode;
27 141
        $this->otherCountryLabel = $otherCountryLabel;
28 141
    }
29
30
31
    /**
32
     * Get 3-letter country code from full country name
33
     *
34
     * @param $country
35
     * @return mixed|string
36
     */
37
    public function getCodeFromCountry($country)
38
    {
39
        $output = 'OTH';
40
        if (array_key_exists($country, $this->countriesMapping)) {
41
            $output = $this->countriesMapping[$country];
42
        }
43
44
        return $output;
45
    }
46
47
    /**
48
     * Get full country name from 3-letter country code
49
     *
50
     * @param string $code
51
     * @return string
52
     */
53
    public function getCountryFromCode($code)
54
    {
55
        $code = strtoupper($code);
56
        $output = "Other";
57
        if (in_array($code, $this->countriesMapping)) {
58
            foreach ($this->countriesMapping as $country => $short) {
59
                if ($code == $short) {
60
                    $output = $country;
61
                    break;
62
                }
63
            }
64
        }
65
66
        return $output;
67
    }
68
69
    /**
70
     * Parses country from maniaplanet player objects' path
71
     *
72
     * @param string $path Maniaplanet path from player object
73
     * @return string long country name
74
     */
75
    public function parseCountryFromPath($path)
76
    {
77
        $parts = explode("|", $path);
78
        if (count($parts) >= 2) {
79
            return $parts[2];
80
        }
81
82
        return "Other";
83
    }
84
}
85