1
0

LanguageIterator.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\L10N;
  8. use OCP\IConfig;
  9. use OCP\IUser;
  10. use OCP\L10N\ILanguageIterator;
  11. class LanguageIterator implements ILanguageIterator {
  12. private $i = 0;
  13. /** @var IConfig */
  14. private $config;
  15. /** @var IUser */
  16. private $user;
  17. public function __construct(IUser $user, IConfig $config) {
  18. $this->config = $config;
  19. $this->user = $user;
  20. }
  21. /**
  22. * Rewind the Iterator to the first element
  23. */
  24. public function rewind(): void {
  25. $this->i = 0;
  26. }
  27. /**
  28. * Return the current element
  29. *
  30. * @since 14.0.0
  31. */
  32. public function current(): string {
  33. switch ($this->i) {
  34. /** @noinspection PhpMissingBreakStatementInspection */
  35. case 0:
  36. $forcedLang = $this->config->getSystemValue('force_language', false);
  37. if (is_string($forcedLang)) {
  38. return $forcedLang;
  39. }
  40. $this->next();
  41. /** @noinspection PhpMissingBreakStatementInspection */
  42. // no break
  43. case 1:
  44. $forcedLang = $this->config->getSystemValue('force_language', false);
  45. if (is_string($forcedLang)
  46. && ($truncated = $this->getTruncatedLanguage($forcedLang)) !== $forcedLang
  47. ) {
  48. return $truncated;
  49. }
  50. $this->next();
  51. /** @noinspection PhpMissingBreakStatementInspection */
  52. // no break
  53. case 2:
  54. $userLang = $this->config->getUserValue($this->user->getUID(), 'core', 'lang', null);
  55. if (is_string($userLang)) {
  56. return $userLang;
  57. }
  58. $this->next();
  59. /** @noinspection PhpMissingBreakStatementInspection */
  60. // no break
  61. case 3:
  62. $userLang = $this->config->getUserValue($this->user->getUID(), 'core', 'lang', null);
  63. if (is_string($userLang)
  64. && ($truncated = $this->getTruncatedLanguage($userLang)) !== $userLang
  65. ) {
  66. return $truncated;
  67. }
  68. $this->next();
  69. // no break
  70. case 4:
  71. return $this->config->getSystemValueString('default_language', 'en');
  72. /** @noinspection PhpMissingBreakStatementInspection */
  73. case 5:
  74. $defaultLang = $this->config->getSystemValueString('default_language', 'en');
  75. if (($truncated = $this->getTruncatedLanguage($defaultLang)) !== $defaultLang) {
  76. return $truncated;
  77. }
  78. $this->next();
  79. // no break
  80. default:
  81. return 'en';
  82. }
  83. }
  84. /**
  85. * Move forward to next element
  86. *
  87. * @since 14.0.0
  88. */
  89. public function next(): void {
  90. ++$this->i;
  91. }
  92. /**
  93. * Return the key of the current element
  94. *
  95. * @since 14.0.0
  96. */
  97. public function key(): int {
  98. return $this->i;
  99. }
  100. /**
  101. * Checks if current position is valid
  102. *
  103. * @since 14.0.0
  104. */
  105. public function valid(): bool {
  106. return $this->i <= 6;
  107. }
  108. protected function getTruncatedLanguage(string $lang):string {
  109. $pos = strpos($lang, '_');
  110. if ($pos !== false) {
  111. $lang = substr($lang, 0, $pos);
  112. }
  113. return $lang;
  114. }
  115. }