LanguageIterator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\L10N;
  26. use OCP\IConfig;
  27. use OCP\IUser;
  28. use OCP\L10N\ILanguageIterator;
  29. class LanguageIterator implements ILanguageIterator {
  30. private $i = 0;
  31. /** @var IConfig */
  32. private $config;
  33. /** @var IUser */
  34. private $user;
  35. public function __construct(IUser $user, IConfig $config) {
  36. $this->config = $config;
  37. $this->user = $user;
  38. }
  39. /**
  40. * Rewind the Iterator to the first element
  41. */
  42. public function rewind(): void {
  43. $this->i = 0;
  44. }
  45. /**
  46. * Return the current element
  47. *
  48. * @since 14.0.0
  49. */
  50. public function current(): string {
  51. switch ($this->i) {
  52. /** @noinspection PhpMissingBreakStatementInspection */
  53. case 0:
  54. $forcedLang = $this->config->getSystemValue('force_language', false);
  55. if (is_string($forcedLang)) {
  56. return $forcedLang;
  57. }
  58. $this->next();
  59. /** @noinspection PhpMissingBreakStatementInspection */
  60. // no break
  61. case 1:
  62. $forcedLang = $this->config->getSystemValue('force_language', false);
  63. if (is_string($forcedLang)
  64. && ($truncated = $this->getTruncatedLanguage($forcedLang)) !== $forcedLang
  65. ) {
  66. return $truncated;
  67. }
  68. $this->next();
  69. /** @noinspection PhpMissingBreakStatementInspection */
  70. // no break
  71. case 2:
  72. $userLang = $this->config->getUserValue($this->user->getUID(), 'core', 'lang', null);
  73. if (is_string($userLang)) {
  74. return $userLang;
  75. }
  76. $this->next();
  77. /** @noinspection PhpMissingBreakStatementInspection */
  78. // no break
  79. case 3:
  80. $userLang = $this->config->getUserValue($this->user->getUID(), 'core', 'lang', null);
  81. if (is_string($userLang)
  82. && ($truncated = $this->getTruncatedLanguage($userLang)) !== $userLang
  83. ) {
  84. return $truncated;
  85. }
  86. $this->next();
  87. // no break
  88. case 4:
  89. return $this->config->getSystemValue('default_language', 'en');
  90. /** @noinspection PhpMissingBreakStatementInspection */
  91. case 5:
  92. $defaultLang = $this->config->getSystemValue('default_language', 'en');
  93. if (($truncated = $this->getTruncatedLanguage($defaultLang)) !== $defaultLang) {
  94. return $truncated;
  95. }
  96. $this->next();
  97. // no break
  98. default:
  99. return 'en';
  100. }
  101. }
  102. /**
  103. * Move forward to next element
  104. *
  105. * @since 14.0.0
  106. */
  107. public function next(): void {
  108. ++$this->i;
  109. }
  110. /**
  111. * Return the key of the current element
  112. *
  113. * @since 14.0.0
  114. */
  115. public function key(): int {
  116. return $this->i;
  117. }
  118. /**
  119. * Checks if current position is valid
  120. *
  121. * @since 14.0.0
  122. */
  123. public function valid(): bool {
  124. return $this->i <= 6;
  125. }
  126. protected function getTruncatedLanguage(string $lang):string {
  127. $pos = strpos($lang, '_');
  128. if ($pos !== false) {
  129. $lang = substr($lang, 0, $pos);
  130. }
  131. return $lang;
  132. }
  133. }