Completed
Push — 7.5 ( 8fcf9d...73b8c1 )
by
unknown
73:34 queued 49:09
created

AbstractLanguageResolver::setShowAllTranslations()   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 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
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
 * Common abstract implementation of Language resolver.
16
 *
17
 * @internal
18
 */
19
abstract class AbstractLanguageResolver implements APILanguageResolver
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
    /**
40
     * @param bool $defaultUseAlwaysAvailable
41
     * @param bool $defaultShowAllTranslations
42
     */
43
    public function __construct(
44
        bool $defaultUseAlwaysAvailable = true,
45
        bool $defaultShowAllTranslations = false
46
    ) {
47
        $this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable;
48
        $this->defaultShowAllTranslations = $defaultShowAllTranslations;
49
    }
50
51
    /**
52
     * Get list of languages configured via dedicated layer.
53
     *
54
     * @return string[]
55
     */
56
    abstract protected function getConfiguredLanguages(): array;
57
58
    /**
59
     * For use by custom events / logic setting language for all retrieved objects from repository.
60
     *
61
     * User language will, if set, will have prepended before configured languages. But in cases PHP API consumer
62
     * specifies languages to retrieve repository objects in it will instead be appended as a fallback.
63
     *
64
     * If set, this will have priority over configured languages.
65
     *
66
     * @param string|null $contextLanguage
67
     */
68
    final public function setContextLanguage(?string $contextLanguage): void
69
    {
70
        $this->contextLanguage = $contextLanguage;
71
    }
72
73
    /**
74
     * Get context language currently set by custom logic.
75
     *
76
     * @return null|string
77
     */
78
    final public function getContextLanguage(): ?string
79
    {
80
        return $this->contextLanguage;
81
    }
82
83
    /**
84
     * Get prioritized languages taking into account forced and context languages.
85
     *
86
     * @param array|null $forcedLanguages Optional, typically arguments provided to API, will be used first if set.
87
     *
88
     * @return array
89
     */
90
    final public function getPrioritizedLanguages(?array $forcedLanguages = null): array
91
    {
92
        // Skip if languages param has been set by API user
93
        if ($forcedLanguages !== null) {
94
            return $forcedLanguages;
95
        }
96
97
        // Detect if we should load all languages by default
98
        if ($this->defaultShowAllTranslations) {
99
            return Language::ALL;
100
        }
101
102
        // create language based on context and configuration, where context language is made most important one
103
        $languages = [];
104
        if ($this->contextLanguage !== null) {
105
            $languages[] = $this->contextLanguage;
106
        }
107
108
        return array_values(array_unique(array_merge($languages, $this->getConfiguredLanguages())));
109
    }
110
111
    /**
112
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
113
     *
114
     * @param bool $defaultUseAlwaysAvailable
115
     */
116
    final public function setDefaultUseAlwaysAvailable(bool $defaultUseAlwaysAvailable): void
117
    {
118
        $this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable;
119
    }
120
121
    /**
122
     * Get currently set UseAlwaysAvailable.
123
     *
124
     * @param bool|null $forcedUseAlwaysAvailable Optional, if set will be used instead of configured value,
125
     *        typically arguments provided to API.
126
     *
127
     * @return bool
128
     */
129
    final public function getUseAlwaysAvailable(?bool $forcedUseAlwaysAvailable = null): bool
130
    {
131
        if ($forcedUseAlwaysAvailable !== null) {
132
            return $forcedUseAlwaysAvailable;
133
        }
134
135
        return $this->defaultUseAlwaysAvailable;
136
    }
137
138
    /**
139
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
140
     *
141
     * @param bool $defaultShowAllTranslations
142
     */
143
    final public function setShowAllTranslations(bool $defaultShowAllTranslations): void
144
    {
145
        $this->defaultShowAllTranslations = $defaultShowAllTranslations;
146
    }
147
148
    /**
149
     * Get currently set showAllTranslations.
150
     *
151
     * @param bool|null $forcedShowAllTranslations Optional, if set will be used instead of configured value,
152
     *        typically arguments provided to API.
153
     *
154
     * @return bool
155
     */
156
    final public function getShowAllTranslations(?bool $forcedShowAllTranslations = null): bool
157
    {
158
        if ($forcedShowAllTranslations !== null) {
159
            return $forcedShowAllTranslations;
160
        }
161
162
        return $this->defaultShowAllTranslations;
163
    }
164
}
165