TrackManiaControl/application/core/Libs/FML/Elements/Dico.php

267 lines
4.8 KiB
PHP
Raw Normal View History

2014-01-19 19:30:21 +01:00
<?php
namespace FML\Elements;
/**
* Dictionary Element
*
2014-05-20 15:44:45 +02:00
* @author steeffeen <mail@steeffeen.com>
2014-04-13 18:21:40 +02:00
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
2014-05-18 19:45:50 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-01-19 19:30:21 +01:00
*/
class Dico {
/**
2014-06-21 03:18:21 +02:00
* Czech language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_CZECH = 'cz';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Danish language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_DANISH = 'da';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* German language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_GERMAN = 'de';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* English language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_ENGLISH = 'en';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Spanish language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_SPANISH = 'es';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* French language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_FRENCH = 'fr';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Hungarian language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_HUNGARIAN = 'hu';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Italian language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_ITALIAN = 'it';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Japanese language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_JAPANESE = 'jp';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Korean language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_KOREAN = 'kr';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Norwegian language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_NORWEGIAN = 'nb';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Dutch language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_DUTCH = 'nl';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Polish language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_POLISH = 'pl';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Portuguese language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_PORTUGUESE = 'pt';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Brazilian Portuguese language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_BRAZILIAN_PORTUGUESE = 'pt_BR';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Romanian language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_ROMANIAN = 'ro';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Russian language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_RUSSIAN = 'ru';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Slovak language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_SLOVAK = 'sk';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Turkish language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_TURKISH = 'tr';
2014-05-18 19:45:50 +02:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Chinese language
2014-01-19 19:30:21 +01:00
*
* @var string
*/
const LANG_CHINESE = 'zh';
2014-05-18 19:45:50 +02:00
2014-01-21 20:30:40 +01:00
/*
2014-06-21 03:18:21 +02:00
* Protected properties
2014-01-19 19:30:21 +01:00
*/
protected $tagName = 'dico';
protected $entries = array();
/**
2014-06-21 03:18:21 +02:00
* Create a new Dictionary object
2014-01-19 19:30:21 +01:00
*
2014-06-21 03:18:21 +02:00
* @return \FML\Elements\Dico|static
2014-01-19 19:30:21 +01:00
*/
public static function create() {
2014-06-21 03:18:21 +02:00
return new static();
2014-01-19 19:30:21 +01:00
}
/**
2014-06-21 03:18:21 +02:00
* Set the translatable entry for the specific language
2014-01-19 19:30:21 +01:00
*
2014-06-21 03:18:21 +02:00
* @param string $language Language id
* @param string $entryId Entry id
* @param string $entryValue Translated entry value
* @return \FML\Elements\Dico|static
2014-01-19 19:30:21 +01:00
*/
public function setEntry($language, $entryId, $entryValue) {
2014-05-18 19:45:50 +02:00
$language = (string)$language;
$entryId = (string)$entryId;
$entryValue = (string)$entryValue;
2014-01-19 19:30:21 +01:00
if (!isset($this->entries[$language]) && $entryValue) {
$this->entries[$language] = array();
}
if ($entryValue) {
$this->entries[$language][$entryId] = $entryValue;
2014-05-18 19:45:50 +02:00
} else {
2014-01-19 19:30:21 +01:00
if (isset($this->entries[$language][$entryId])) {
unset($this->entries[$language][$entryId]);
}
}
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Remove entries of the given id
2014-01-19 19:30:21 +01:00
*
2014-06-21 03:18:21 +02:00
* @param string $entryId Entry id that should be removed
* @param string $language (optional) Only remove entries of the given language
* @return \FML\Elements\Dico|static
2014-01-19 19:30:21 +01:00
*/
public function removeEntry($entryId, $language = null) {
2014-05-18 19:45:50 +02:00
$entryId = (string)$entryId;
2014-01-19 19:30:21 +01:00
if ($language) {
2014-05-18 19:45:50 +02:00
$language = (string)$language;
2014-01-19 19:30:21 +01:00
if (isset($this->entries[$language])) {
unset($this->entries[$language][$entryId]);
}
2014-05-18 19:45:50 +02:00
} else {
2014-01-19 19:30:21 +01:00
foreach ($this->entries as $language => $entries) {
if (isset($entries[$entryId])) {
unset($entries[$language][$entryId]);
}
}
}
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Remove entries of the given language
2014-01-19 19:30:21 +01:00
*
2014-06-21 03:18:21 +02:00
* @param string $language Language which entries should be removed
* @param string $entryId (optional) Only remove the given entry id
* @return \FML\Elements\Dico|static
2014-01-19 19:30:21 +01:00
*/
public function removeLanguage($language, $entryId = null) {
2014-05-18 19:45:50 +02:00
$language = (string)$language;
2014-01-19 19:30:21 +01:00
if (isset($this->entries[$language])) {
if ($entryId) {
2014-05-18 19:45:50 +02:00
$entryId = (string)$entryId;
2014-01-19 19:30:21 +01:00
unset($this->entries[$language][$entryId]);
2014-05-18 19:45:50 +02:00
} else {
2014-01-19 19:30:21 +01:00
unset($this->entries[$language]);
}
}
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Remove all entries from the Dictionary
2014-01-19 19:30:21 +01:00
*
2014-06-21 03:18:21 +02:00
* @return \FML\Elements\Dico|static
2014-01-19 19:30:21 +01:00
*/
public function removeEntries() {
$this->entries = array();
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Render the Dico XML element
2014-01-19 19:30:21 +01:00
*
2014-06-21 03:18:21 +02:00
* @param \DOMDocument $domDocument DOMDocument for which the Dico XML element should be rendered
2014-01-19 19:30:21 +01:00
* @return \DOMElement
*/
public function render(\DOMDocument $domDocument) {
$xmlElement = $domDocument->createElement($this->tagName);
foreach ($this->entries as $language => $entries) {
$languageElement = $domDocument->createElement('language');
$languageElement->setAttribute('id', $language);
foreach ($entries as $entryId => $entryValue) {
$entryElement = $domDocument->createElement($entryId, $entryValue);
$languageElement->appendChild($entryElement);
}
$xmlElement->appendChild($languageElement);
}
return $xmlElement;
}
}