Language   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 26
dl 0
loc 88
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 10 3
A __toString() 0 3 1
A getLocale() 0 10 3
A __construct() 0 6 2
A getWeight() 0 7 2
A hasProperty() 0 11 3
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cecil\Renderer;
15
16
use Cecil\Exception\RuntimeException;
17
18
/**
19
 * Language renderer class.
20
 *
21
 * This class is responsible for managing language properties such as name, locale, and weight.
22
 * It retrieves these properties from the configuration object and provides methods to access them.
23
 * It also ensures that the properties exist and are not empty, throwing an exception if they are.
24
 */
25
class Language
26
{
27
    /**
28
     * Configuration object.
29
     * @var \Cecil\Config
30
     */
31
    protected $config;
32
    /**
33
     * Current language code.
34
     * @var string
35
     */
36
    protected $language;
37
38
    public function __construct(\Cecil\Config $config, ?string $language = null)
39
    {
40
        $this->config = $config;
41
        $this->language = $language;
42
        if ($language === null) {
43
            $this->language = $this->config->getLanguageDefault();
44
        }
45
    }
46
47
    /**
48
     * Returns the current language.
49
     *
50
     * @return string
51
     */
52
    public function __toString()
53
    {
54
        return $this->language;
55
    }
56
57
    /**
58
     * Returns the name of the current or of the given language code.
59
     */
60
    public function getName(?string $language = null): ?string
61
    {
62
        if ($language !== null) {
63
            $this->language = $language;
64
        }
65
        if ($this->hasProperty('name')) {
66
            return $this->config->getLanguageProperty('name', $this->language);
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * Returns the locale of the current or of the given language code.
74
     */
75
    public function getLocale(?string $language = null): ?string
76
    {
77
        if ($language !== null) {
78
            $this->language = $language;
79
        }
80
        if ($this->hasProperty('locale')) {
81
            return $this->config->getLanguageProperty('locale', $this->language);
82
        }
83
84
        return null;
85
    }
86
87
    /**
88
     * Returns the weight of the current or of the given language code.
89
     */
90
    public function getWeight(?string $language = null): int
91
    {
92
        if ($language !== null) {
93
            $this->language = $language;
94
        }
95
96
        return $this->config->getLanguageIndex($this->language);
97
    }
98
99
    /**
100
     * Checks if the given property exists for the current language.
101
     */
102
    private function hasProperty(string $property): bool
103
    {
104
        $value = $this->config->getLanguageProperty($property, $this->language);
105
106
        if (empty($value)) {
107
            $language = $this->language ?: $this->config->getLanguageDefault();
108
109
            throw new RuntimeException(\sprintf('The property "%s" is empty for language "%s".', $property, $language));
110
        }
111
112
        return true;
113
    }
114
}
115