LanguageIterator.php 3.4 KB

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