Language.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Style;
  18. /**
  19. * Language
  20. * A couple of predefined values are defined here, see the websites below for more values
  21. *
  22. * @see http://www.datypic.com/sc/ooxml/t-w_CT_Language.html
  23. * @see https://technet.microsoft.com/en-us/library/cc287874(v=office.12).aspx
  24. */
  25. final class Language extends AbstractStyle
  26. {
  27. const EN_US = 'en-US';
  28. const EN_US_ID = 1033;
  29. const EN_GB = 'en-GB';
  30. const EN_GB_ID = 2057;
  31. const FR_FR = 'fr-FR';
  32. const FR_FR_ID = 1036;
  33. const FR_BE = 'fr-BE';
  34. const FR_BE_ID = 2060;
  35. const ES_ES = 'es-ES';
  36. const ES_ES_ID = 3082;
  37. const DE_DE = 'de-DE';
  38. const DE_DE_ID = 1031;
  39. const HE_IL = 'he-IL';
  40. const HE_IL_ID = 1037;
  41. const IT_IT = 'it-IT';
  42. const IT_IT_ID = 1040;
  43. const JA_JP = 'ja-JP';
  44. const JA_JP_ID = 1041;
  45. const KO_KR = 'ko-KR';
  46. const KO_KR_ID = 1042;
  47. const ZH_CN = 'zh-CN';
  48. const ZH_CN_ID = 2052;
  49. const HI_IN = 'hi-IN';
  50. const HI_IN_ID = 1081;
  51. const PT_BR = 'pt-BR';
  52. const PT_BR_ID = 1046;
  53. const NL_NL = 'nl-NL';
  54. const NL_NL_ID = 1043;
  55. const UK_UA = 'uk-UA';
  56. const UK_UA_ID = 1058;
  57. const RU_RU = 'ru-RU';
  58. const RU_RU_ID = 1049;
  59. /**
  60. * Language ID, used for RTF document generation
  61. *
  62. * @var int
  63. * @see https://technet.microsoft.com/en-us/library/cc179219.aspx
  64. */
  65. private $langId;
  66. /**
  67. * Latin Language
  68. *
  69. * @var string
  70. */
  71. private $latin;
  72. /**
  73. * East Asian Language
  74. *
  75. * @var string
  76. */
  77. private $eastAsia;
  78. /**
  79. * Complex Script Language
  80. *
  81. * @var string
  82. */
  83. private $bidirectional;
  84. /**
  85. * Constructor
  86. *
  87. * @param string|null $latin
  88. * @param string|null $eastAsia
  89. * @param string|null $bidirectional
  90. */
  91. public function __construct($latin = null, $eastAsia = null, $bidirectional = null)
  92. {
  93. if (!empty($latin)) {
  94. $this->setLatin($latin);
  95. }
  96. if (!empty($eastAsia)) {
  97. $this->setEastAsia($eastAsia);
  98. }
  99. if (!empty($bidirectional)) {
  100. $this->setBidirectional($bidirectional);
  101. }
  102. }
  103. /**
  104. * Set the Latin Language
  105. *
  106. * @param string $latin
  107. * The value for the latin language
  108. * @return self
  109. */
  110. public function setLatin($latin)
  111. {
  112. $this->latin = $this->validateLocale($latin);
  113. return $this;
  114. }
  115. /**
  116. * Get the Latin Language
  117. *
  118. * @return string|null
  119. */
  120. public function getLatin()
  121. {
  122. return $this->latin;
  123. }
  124. /**
  125. * Set the Language ID
  126. *
  127. * @param int $langId
  128. * The value for the language ID
  129. * @return self
  130. * @see https://technet.microsoft.com/en-us/library/cc287874(v=office.12).aspx
  131. */
  132. public function setLangId($langId)
  133. {
  134. $this->langId = $langId;
  135. return $this;
  136. }
  137. /**
  138. * Get the Language ID
  139. *
  140. * @return int
  141. */
  142. public function getLangId()
  143. {
  144. return $this->langId;
  145. }
  146. /**
  147. * Set the East Asian Language
  148. *
  149. * @param string $eastAsia
  150. * The value for the east asian language
  151. * @return self
  152. */
  153. public function setEastAsia($eastAsia)
  154. {
  155. $this->eastAsia = $this->validateLocale($eastAsia);
  156. return $this;
  157. }
  158. /**
  159. * Get the East Asian Language
  160. *
  161. * @return string|null
  162. */
  163. public function getEastAsia()
  164. {
  165. return $this->eastAsia;
  166. }
  167. /**
  168. * Set the Complex Script Language
  169. *
  170. * @param string $bidirectional
  171. * The value for the complex script language
  172. * @return self
  173. */
  174. public function setBidirectional($bidirectional)
  175. {
  176. $this->bidirectional = $this->validateLocale($bidirectional);
  177. return $this;
  178. }
  179. /**
  180. * Get the Complex Script Language
  181. *
  182. * @return string|null
  183. */
  184. public function getBidirectional()
  185. {
  186. return $this->bidirectional;
  187. }
  188. /**
  189. * Validates that the language passed is in the format xx-xx
  190. *
  191. * @param string $locale
  192. * @return string
  193. */
  194. private function validateLocale($locale)
  195. {
  196. if ($locale !== null) {
  197. $locale = str_replace('_', '-', $locale);
  198. }
  199. if (strlen($locale) === 2) {
  200. return strtolower($locale) . '-' . strtoupper($locale);
  201. }
  202. if ($locale !== null && $locale !== 'zxx' && strstr($locale, '-') === false) {
  203. throw new \InvalidArgumentException($locale . ' is not a valid language code');
  204. }
  205. return $locale;
  206. }
  207. }