Completed
Push — ezp-25721-use-config-resolver-... ( 919520...d0d9f3 )
by
unknown
26:24
created

LanguageResolver::resolveConfigLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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