Completed
Push — siteaccessaware-layer-only ( edaec2...37e924 )
by André
22:17
created

LanguageResolver::getPrioritizedLanguages()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace eZ\Publish\Core\Repository\Helper;
4
5
use eZ\Publish\API\Repository\Values\Content\Language;
6
7
/**
8
 * Resolves language settings for use in SiteAccess aware Repository.
9
 */
10
class LanguageResolver
11
{
12
    /**
13
     * Values typically provided by configuration.
14
     *
15
     * These will need to change when configuration (scope) changes mid flight using setters below.
16
     *
17
     * @var array
18
     */
19
    private $configLanguages;
20
21
    /** @var bool */
22
    private $defaultUseAlwaysAvailable;
23
24
    /** @var bool */
25
    private $defaultShowAllTranslations;
26
27
    /**
28
     * Values typically provided by user context, will need to be set depending on your own custom logic using setter.
29
     *
30
     * E.g. Backend UI might expose a language selector for the whole backend that should be reflected on both
31
     *      UI strings as well as default languages to prioritize for repository objects.
32
     *
33
     * If set, this will have priority over configured languages.
34
     *
35
     * @var string|null
36
     */
37
    private $contextLanguage;
38
39
    public function __construct(
40
        array $configLanguages,
41
        bool $defaultUseAlwaysAvailable = true,
42
        bool $defaultShowAllTranslations = false
43
    ) {
44
        $this->configLanguages = $configLanguages;
45
        $this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable;
46
        $this->defaultShowAllTranslations = $defaultShowAllTranslations;
47
    }
48
49
    /**
50
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
51
     *
52
     * @param array $configLanguages
53
     */
54
    public function setConfigLanguages(array $configLanguages): void
55
    {
56
        $this->configLanguages = $configLanguages;
57
    }
58
59
    /**
60
     * For use by custom events / logic setting language for all retrieved objects from repository.
61
     *
62
     * User language will, if set, will have prepended before configured languages. But in cases PHP API consumer
63
     * specifies languages to retrieve repository objects in it will instead be appended as a fallback.
64
     *
65
     * If set, this will have priority over configured languages.
66
     *
67
     * @param string|null $contextLanguage
68
     */
69
    public function setContextLanguage(?string $contextLanguage): void
70
    {
71
        $this->contextLanguage = $contextLanguage;
72
    }
73
74
    /**
75
     * Get prioritized languages taking into account forced-, context- and lastly configured-languages.
76
     *
77
     * @param array|null $forcedLanguages Optional, typically arguments provided to API, will be used first if set.
78
     *
79
     * @return array
80
     */
81
    public function getPrioritizedLanguages(?array $forcedLanguages): array
82
    {
83
        // Skip if languages param has been set by API user
84
        if ($forcedLanguages !== null) {
85
            return $forcedLanguages;
86
        }
87
88
        // Detect if we should load all languages by default
89
        if ($this->defaultShowAllTranslations) {
90
            return Language::ALL;
91
        }
92
93
        // create language based on context and configuration, where context language is made most important one
94
        $languages = [];
95
        if ($this->contextLanguage !== null) {
96
            $languages[] = $this->contextLanguage;
97
        }
98
99
        return array_values(array_unique(array_merge($languages, $this->configLanguages)));
100
    }
101
102
    /**
103
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
104
     *
105
     * @param bool $defaultUseAlwaysAvailable
106
     */
107
    public function setDefaultUseAlwaysAvailable(bool $defaultUseAlwaysAvailable): void
108
    {
109
        $this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable;
110
    }
111
112
    /**
113
     * Get currently set UseAlwaysAvailable.
114
     *
115
     * @param bool|null $forcedUseAlwaysAvailable Optional, if set will be used instead of configured value,
116
     *        typically arguments provided to API.
117
     *
118
     * @return bool
119
     */
120
    public function getUseAlwaysAvailable(?bool $forcedUseAlwaysAvailable = null): bool
121
    {
122
        if ($forcedUseAlwaysAvailable !== null) {
123
            return $forcedUseAlwaysAvailable;
124
        }
125
126
        return $this->defaultUseAlwaysAvailable;
127
    }
128
129
    /**
130
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
131
     *
132
     * @param bool $defaultShowAllTranslations
133
     */
134
    public function setShowAllTranslations(bool $defaultShowAllTranslations): void
135
    {
136
        $this->defaultShowAllTranslations = $defaultShowAllTranslations;
137
    }
138
139
    /**
140
     * Get currently set showAllTranslations.
141
     *
142
     * @param bool|null $forcedShowAllTranslations Optional, if set will be used instead of configured value,
143
     *        typically arguments provided to API.
144
     *
145
     * @return bool
146
     */
147
    public function getShowAllTranslations(?bool $forcedShowAllTranslations = null): bool
148
    {
149
        if ($forcedShowAllTranslations !== null) {
150
            return $forcedShowAllTranslations;
151
        }
152
153
        return $this->defaultShowAllTranslations;
154
    }
155
}
156