Completed
Push — siteaccessaware-layer-only ( 65e0ca...b62a6f )
by André
12:44
created

LanguageResolver::setShowAllTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace eZ\Publish\Core\Repository\Helper;
4
5
/**
6
 * Resolves language settings for use in SiteAccess aware Repository.
7
 *
8
 * @todo BC BREAK !!: SiteAccessAware Repository will when switched on on by default in current state break all api
9
 * @todo calls where prioritized languages is used for $language filter parameter! Given it will then by default always
10
 * @todo filter results in cases where it was from before set to null specifically to return all translations.
11
 * @todo Solution is either to expose a ShowAllTranslations argument like in UrlAliasService (not in interface yet)
12
 * @todo or expose new $prioritizedLanguages argument that does not filter and align UrlAliasService $languagesFilter +
13
 * @todo $prioritizedLanguages logic on this, in order to also omit undocumented $ShowAllTranslations argument.
14
 *       No matter what the outcome is, what should be exposed in config to users is a ShowAllTranslations or similar
15
 *       config that in backend UI allows all translations to be shown, while in frontend languages are by default
16
 *       filtered.
17
 *
18
 */
19
class LanguageResolver
20
{
21
    /**
22
     * Values typically provided by configuration.
23
     *
24
     * These will need to change when configuration (scope) changes using setters below.
25
     */
26
    private $configLanguages;
27
    private $useAlwaysAvailable;
28
    private $showAllTranslations;
29
30
    /**
31
     * Values typically provided by user context, will need to be set depending on your own custom logic using setter.
32
     *
33
     * E.g. Backend UI might expose a language selector for the whole backend that should be reflected on both
34
     *      UI strings as well as default languages to prioritize for repository objects.
35
     */
36
    private $contextLanguage;
37
38
    public function __construct(array $configLanguages, $useAlwaysAvailable = null, $showAllTranslations = null)
39
    {
40
        $this->configLanguages = $configLanguages;
41
        $this->useAlwaysAvailable = $useAlwaysAvailable;
42
        $this->showAllTranslations = $showAllTranslations;
43
    }
44
45
    /**
46
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
47
     *
48
     * @param array $configLanguages
49
     */
50
    public function setConfigLanguages(array $configLanguages)
51
    {
52
        $this->configLanguages = $configLanguages;
53
    }
54
55
    /**
56
     * For use by custom events / logic setting language for all retrieved objects from repository.
57
     *
58
     * User language will, if set, will have prepended before configured languages. But in cases PHP API consumer
59
     * specifies languages to retrieve repository objects in it will instead be appended as a fallback.
60
     *
61
     * @param string|null $contextLanguage
62
     */
63
    public function setContextLanguage($contextLanguage)
64
    {
65
        $this->contextLanguage = $contextLanguage;
66
    }
67
68
    /**
69
     * Get prioritized languages taking into account forced-, context- and lastly configured-languages.
70
     *
71
     * @param array|null $forcedLanguages Optional, typically arguments provided to API, will be used first if set.
72
     *
73
     * @return array
74
     */
75
    public function getPrioritizedLanguages(array $forcedLanguages = null)
76
    {
77
        $languages = empty($forceLanguages) ? [] : $forcedLanguages;
0 ignored issues
show
Bug introduced by
The variable $forceLanguages does not exist. Did you mean $forcedLanguages?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
78
        if ($this->contextLanguage !== null) {
79
            $languages[] = $this->contextLanguage;
80
        }
81
82
        return array_unique($languages + $this->configLanguages);
83
    }
84
85
    /**
86
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
87
     *
88
     * @param bool $useAlwaysAvailable
89
     */
90
    public function setUseAlwaysAvailable($useAlwaysAvailable)
91
    {
92
        $this->useAlwaysAvailable = $useAlwaysAvailable;
93
    }
94
95
    /**
96
     * Get currently set UseAlwaysAvailable.
97
     *
98
     * @param bool|null $forcedUseAlwaysAvailable Optional, if set will be used instead of configured value,
99
     *        typically arguments provided to API.
100
     * @param bool $defaultUseAlwaysAvailable
101
     *
102
     * @return bool
103
     */
104
    public function getUseAlwaysAvailable($forcedUseAlwaysAvailable = null, $defaultUseAlwaysAvailable = true)
105
    {
106
        if ($forcedUseAlwaysAvailable !== null) {
107
            return $forcedUseAlwaysAvailable;
108
        }
109
110
        if ($this->useAlwaysAvailable !== null) {
111
            return $this->useAlwaysAvailable;
112
        }
113
114
        return $defaultUseAlwaysAvailable;
115
    }
116
117
    /**
118
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
119
     *
120
     * @param bool $showAllTranslations
121
     */
122
    public function setShowAllTranslations($showAllTranslations)
123
    {
124
        $this->showAllTranslations = $showAllTranslations;
125
    }
126
127
    /**
128
     * Get currently set UseAlwaysAvailable.
129
     *
130
     * @param bool|null $forcedShowAllTranslations Optional, if set will be used instead of configured value,
131
     *        typically arguments provided to API.
132
     * @param bool $defaultShowAllTranslations
133
     *
134
     * @return bool
135
     */
136
    public function getShowAllTranslations($forcedShowAllTranslations = null, $defaultShowAllTranslations = false)
137
    {
138
        if ($forcedShowAllTranslations !== null) {
139
            return $forcedShowAllTranslations;
140
        }
141
142
        if ($this->showAllTranslations !== null) {
143
            return $this->showAllTranslations;
144
        }
145
146
        return $defaultShowAllTranslations;
147
    }
148
}
149