Completed
Push — siteaccessaware-layer-only ( 7e91dd...edaec2 )
by André
56:17 queued 37:14
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 using setters below.
16
     */
17
    private $configLanguages;
18
    private $useAlwaysAvailable;
19
    private $showAllTranslations;
20
21
    /**
22
     * Values typically provided by user context, will need to be set depending on your own custom logic using setter.
23
     *
24
     * E.g. Backend UI might expose a language selector for the whole backend that should be reflected on both
25
     *      UI strings as well as default languages to prioritize for repository objects.
26
     */
27
    private $contextLanguage;
28
29
    public function __construct(array $configLanguages, bool $useAlwaysAvailable = null, bool $showAllTranslations = null)
30
    {
31
        $this->configLanguages = $configLanguages;
32
        $this->useAlwaysAvailable = $useAlwaysAvailable;
33
        $this->showAllTranslations = $showAllTranslations;
34
    }
35
36
    /**
37
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
38
     *
39
     * @param array $configLanguages
40
     */
41
    public function setConfigLanguages(array $configLanguages)
42
    {
43
        $this->configLanguages = $configLanguages;
44
    }
45
46
    /**
47
     * For use by custom events / logic setting language for all retrieved objects from repository.
48
     *
49
     * User language will, if set, will have prepended before configured languages. But in cases PHP API consumer
50
     * specifies languages to retrieve repository objects in it will instead be appended as a fallback.
51
     *
52
     * @param string|null $contextLanguage
53
     */
54
    public function setContextLanguage(?string $contextLanguage)
55
    {
56
        $this->contextLanguage = $contextLanguage;
57
    }
58
59
    /**
60
     * Get prioritized languages taking into account forced-, context- and lastly configured-languages.
61
     *
62
     * @param array|null $forcedLanguages Optional, typically arguments provided to API, will be used first if set.
63
     *
64
     * @return array|null
65
     */
66
    public function getPrioritizedLanguages(?array $forcedLanguages)
67
    {
68
        // Skip if languages param has been set by API user
69
        if ($forcedLanguages !== null) {
70
            return $forcedLanguages;
71
        }
72
73
        // Detect if we should load all languages by default
74
        if ($this->showAllTranslations) {
75
            return Language::ALL;
76
        }
77
78
        // create language based on context and configuration
79
        $languages = [];
80
        if ($this->contextLanguage !== null) {
81
            $languages[] = $this->contextLanguage;
82
        }
83
84
        return array_values(array_unique(array_merge($languages, $this->configLanguages)));
85
    }
86
87
    /**
88
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
89
     *
90
     * @param bool $useAlwaysAvailable
91
     */
92
    public function setUseAlwaysAvailable(bool $useAlwaysAvailable)
93
    {
94
        $this->useAlwaysAvailable = $useAlwaysAvailable;
95
    }
96
97
    /**
98
     * Get currently set UseAlwaysAvailable.
99
     *
100
     * @param bool|null $forcedUseAlwaysAvailable Optional, if set will be used instead of configured value,
101
     *        typically arguments provided to API.
102
     * @param bool $defaultUseAlwaysAvailable
103
     *
104
     * @return bool
105
     */
106
    public function getUseAlwaysAvailable(?bool $forcedUseAlwaysAvailable = null, bool $defaultUseAlwaysAvailable = true)
107
    {
108
        if ($forcedUseAlwaysAvailable !== null) {
109
            return $forcedUseAlwaysAvailable;
110
        }
111
112
        if ($this->useAlwaysAvailable !== null) {
113
            return $this->useAlwaysAvailable;
114
        }
115
116
        return $defaultUseAlwaysAvailable;
117
    }
118
119
    /**
120
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
121
     *
122
     * @param bool $showAllTranslations
123
     */
124
    public function setShowAllTranslations(bool $showAllTranslations)
125
    {
126
        $this->showAllTranslations = $showAllTranslations;
127
    }
128
129
    /**
130
     * Get currently set showAllTranslations.
131
     *
132
     * @param bool|null $forcedShowAllTranslations Optional, if set will be used instead of configured value,
133
     *        typically arguments provided to API.
134
     * @param bool $defaultShowAllTranslations
135
     *
136
     * @return bool
137
     */
138
    public function getShowAllTranslations(?bool $forcedShowAllTranslations = null, bool $defaultShowAllTranslations = false)
139
    {
140
        if ($forcedShowAllTranslations !== null) {
141
            return $forcedShowAllTranslations;
142
        }
143
144
        if ($this->showAllTranslations !== null) {
145
            return $this->showAllTranslations;
146
        }
147
148
        return $defaultShowAllTranslations;
149
    }
150
}
151