TrackManiaControl/libs/FML/Elements/Dico.php

299 lines
6.0 KiB
PHP
Raw Permalink 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>
2017-03-25 18:40:15 +01:00
* @copyright FancyManiaLinks Copyright © 2017 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
*/
2017-03-25 18:40:15 +01:00
class Dico
{
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Czech language
*
* @var string
*/
const LANG_CZECH = 'cz';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Danish language
*
* @var string
*/
const LANG_DANISH = 'da';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* German language
*
* @var string
*/
const LANG_GERMAN = 'de';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* English language
*
* @var string
*/
const LANG_ENGLISH = 'en';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Spanish language
*
* @var string
*/
const LANG_SPANISH = 'es';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* French language
*
* @var string
*/
const LANG_FRENCH = 'fr';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Hungarian language
*
* @var string
*/
const LANG_HUNGARIAN = 'hu';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Italian language
*
* @var string
*/
const LANG_ITALIAN = 'it';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Japanese language
*
* @var string
*/
const LANG_JAPANESE = 'jp';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Korean language
*
* @var string
*/
const LANG_KOREAN = 'kr';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Norwegian language
*
* @var string
*/
const LANG_NORWEGIAN = 'nb';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Dutch language
*
* @var string
*/
const LANG_DUTCH = 'nl';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Polish language
*
* @var string
*/
const LANG_POLISH = 'pl';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Portuguese language
*
* @var string
*/
const LANG_PORTUGUESE = 'pt';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Brazilian Portuguese language
*
* @var string
*/
const LANG_BRAZILIAN_PORTUGUESE = 'pt_BR';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Romanian language
*
* @var string
*/
const LANG_ROMANIAN = 'ro';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Russian language
*
* @var string
*/
const LANG_RUSSIAN = 'ru';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Slovak language
*
* @var string
*/
const LANG_SLOVAK = 'sk';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Turkish language
*
* @var string
*/
const LANG_TURKISH = 'tr';
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* Chinese language
*
* @var string
*/
const LANG_CHINESE = 'zh';
2014-01-19 19:30:21 +01:00
2017-03-25 18:40:15 +01:00
/*
* Protected properties
*/
protected $entries = array();
2014-01-19 19:30:21 +01:00
2017-03-25 18:40:15 +01:00
/**
* Create a new Dico
*
* @api
* @return static
*/
public static function create()
{
return new static();
}
2014-01-19 19:30:21 +01:00
2017-03-25 18:40:15 +01:00
/**
* Get the translatable entry
*
* @api
* @param string $language Language id
* @param string $entryId Entry id
* @return string
*/
public function getEntry($language, $entryId)
{
if (isset($this->entries[$language]) && isset($this->entries[$language][$entryId])) {
return $this->entries[$language][$entryId];
}
return null;
}
2014-01-19 19:30:21 +01:00
2017-03-25 18:40:15 +01:00
/**
* Set the translatable entry for the specific language
*
* @api
* @param string $language Language id
* @param string $entryId Entry id
* @param string $entryValue Translated entry value
* @return static
*/
public function setEntry($language, $entryId, $entryValue)
{
$language = (string)$language;
$entryId = (string)$entryId;
$entryValue = (string)$entryValue;
if (!isset($this->entries[$language]) && $entryValue) {
$this->entries[$language] = array();
}
if ($entryValue) {
$this->entries[$language][$entryId] = $entryValue;
} else {
if (isset($this->entries[$language][$entryId])) {
unset($this->entries[$language][$entryId]);
}
}
return $this;
}
2014-01-19 19:30:21 +01:00
2017-03-25 18:40:15 +01:00
/**
* Remove entries of the given id
*
* @api
2017-04-02 16:32:57 +02:00
* @param string $entryId Entry id that should be removed
* @param string $language (optional) Only remove entry from the given language
2017-03-25 18:40:15 +01:00
* @return static
*/
2017-04-02 16:32:57 +02:00
public function removeEntry($entryId, $language = null)
2017-03-25 18:40:15 +01:00
{
$entryId = (string)$entryId;
2017-04-02 16:32:57 +02:00
foreach ($this->entries as $languageKey => $entries) {
if ($language && $language !== $languageKey) {
continue;
}
if (isset($this->entries[$languageKey][$entryId])) {
unset($this->entries[$languageKey][$entryId]);
2017-03-25 18:40:15 +01:00
}
}
return $this;
}
/**
* Remove entries of the given language
*
* @api
* @param string $language Language which entries should be removed
* @return static
*/
public function removeLanguage($language)
{
$language = (string)$language;
if (isset($this->entries[$language])) {
unset($this->entries[$language]);
}
return $this;
}
2017-04-02 16:32:57 +02:00
/**
* Remove entries
*
* @api
* @return static
* @deprecated Use removeAllEntries()
* @see Dico::removeAllEntries()
*/
public function removeEntries()
{
return $this->removeAllEntries();
}
2017-03-25 18:40:15 +01:00
/**
* Remove all entries
*
* @api
* @return static
*/
public function removeAllEntries()
{
$this->entries = array();
return $this;
}
/**
* Render the Dico
*
* @param \DOMDocument $domDocument DOMDocument for which the Dico should be rendered
* @return \DOMElement
*/
public function render(\DOMDocument $domDocument)
{
$domElement = $domDocument->createElement("dico");
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);
}
$domElement->appendChild($languageElement);
}
return $domElement;
}
2014-01-19 19:30:21 +01:00
}