Maslosoft Mangan API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Traits;
use Maslosoft\Mangan\Events\Event;
use Maslosoft\Mangan\Events\ModelEvent;
use Maslosoft\Mangan\Interfaces\InternationalInterface;
use Maslosoft\Mangan\Meta\ManganMeta;
/**
* I18N-able trait contains basic implementation of I18N feature. This covers
* methods from `InternationalInterface`.
*
* Use this trait to provide internationalized columns for models.
*
* @see InternationalInterface
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
trait I18NAbleTrait
{
private $_lang = 'en';
private $_rawI18N = [];
private $_languages = ['en'];
private $_defaultLanguage = 'en';
/**
* Get current working language code
* @return string Language code
* @Ignored
*/
public function getLang()
{
return $this->_lang? : $this->getDefaultLanguage();
}
/**
* Get available languages
* @return string[]
* @Ignored
*/
public function getLanguages()
{
return $this->_languages;
}
/**
* Get i18n values with all languages.
* This returns all language values of all class fields. Class field names are
* keys for arrays of language values, with language codes as a keys.
*
* Example returned variable:
* ```php
* [
* 'name' => [
* 'en' => 'January',
* 'pl' => 'Styczeń'
* ],
* 'description' => [
* 'en' => 'First mothn of a year'
* 'pl' => 'Pierwszy miesiąc roku'
* ]
* ]
* ```
* @return mixed[] Associative array of language values
* @Ignored
*/
public function getRawI18N()
{
$meta = ManganMeta::create($this);
$fields = $meta->properties('i18n');
// Set current model value for current language for each property
foreach ($fields as $name => $i18n)
{
// This is to keep rawi18n value in sync with current model value.
$this->_rawI18N[$name][$this->getLang()] = $this->$name;
}
return $this->_rawI18N;
}
/**
* Set language code. This changes current model language.
* After setting language model attributes will store values in different locale.
*
* Language code must be previously set by `setLanguages`.
* When trying to set undefined language code, method will do nothing.
* When setting already choosen language code, method will also ignore this call.
* Example method calls:
* ```php
* // Set available languages
* $model->setLanguages(['en', 'pl']);
*
* // Will ignore as en is by default
* $model->setLang('en');
*
* // Will set pl as language
* $model->setLang('pl');
*
* // Will ignore as ru is not available
* $model->setLang('ru')
* ```
*
* For initial call, when there are no data set yet, `$triggetEvents`
* can be set to false to improve performance.
*
* @param string $code
* @param boolean $triggerEvents
* @Ignored
*/
public function setLang($code, $triggerEvents = true)
{
// Ensure that calling with empty code will not set language
// to empty value
if(empty($code))
{
return false;
}
if ($this->_lang === $code)
{
return false;
}
if (!in_array($code, $this->getLanguages()))
{
$this->_languages[] = $code;
}
if(!$triggerEvents)
{
$this->_lang = $code;
return true;
}
$event = new ModelEvent($this);
$event->data = $code;
if (!Event::valid($this, InternationalInterface::EventBeforeLangChange, $event))
{
return false;
}
$this->_changeAttributesLang($this->_lang, $code);
$this->_lang = $code;
Event::trigger($this, InternationalInterface::EventAfterLangChange, $event);
return true;
}
/**
* Set available languages. This method accepts one parameter,
* array contaning language codes prefably in short ISO form.
*
* Example valid array and method calls:
*
* ```php
* $languages = ['en', 'pl', 'ru'];
* $model->setLanguages($languages);
* $model2->setLanguages(['en']);
* ```
*
* For initial call, when there are no data set yet, `$triggetEvents`
* can be set to false to improve performance.
*
* @param string[] $languages
* @param boolean $triggerEvents
* @Ignored
*/
public function setLanguages($languages, $triggerEvents = true)
{
if($triggerEvents)
{
$event = new ModelEvent($this);
$event->data = $languages;
if (!Event::valid($this, InternationalInterface::EventBeforeLanguagesSet, $event))
{
return;
}
}
$this->_languages = $languages;
if($triggerEvents)
{
Event::trigger($this, InternationalInterface::EventAfterLanguagesSet, $event);
}
}
/**
* Set i18n values in all languages.
* This method must keep `$values` for further use, by method `getRawI18N`.
* @param mixed[] $values
* @Ignored
*/
public function setRawI18N($values)
{
$this->_rawI18N = $values;
}
/**
* Get default language used for I18N operations.
*
* If not previously set, will fall back to `en`.
*
* @return string
* @Ignored
*/
public function getDefaultLanguage()
{
return $this->_defaultLanguage? : 'en';
}
/**
* Set default language used for I18N operations. This language
* will be used if the `setLang` method was not called.
*
* The value should be language code, for example `en`
*
* @param string $language
* @Ignored
*/
public function setDefaultLanguage($language)
{
$this->_defaultLanguage = $language;
}
/**
* Change i18n attributes values to appropriate language
* @param string $fromCode
* @param string $toCode
*/
private function _changeAttributesLang($fromCode, $toCode)
{
$meta = ManganMeta::create($this);
$fields = $meta->properties('i18n');
$defaultLang = $this->getDefaultLanguage();
foreach ($fields as $name => $i18n)
{
$current = $this->$name;
if (isset($this->_rawI18N[$name]) && array_key_exists($toCode, $this->_rawI18N[$name]))
{
$new = $this->_rawI18N[$name][$toCode];
}
else
{
$i18n = $meta->field($name)->i18n;
if($i18n->allowDefault && isset($this->_rawI18N[$name]) && array_key_exists($defaultLang, $this->_rawI18N[$name]))
{
$new = $this->_rawI18N[$name][$defaultLang];
}
elseif($i18n->allowAny && !empty($this->_rawI18N[$name]))
{
$wasFound = false;
foreach($this->getLanguages() as $code)
{
if(!empty($this->_rawI18N[$name][$code]))
{
$new = $this->_rawI18N[$name][$code];
$wasFound = true;
break;
}
}
if(!$wasFound)
{
$new = $meta->$name->default;
}
}
else
{
$new = $meta->$name->default;
}
}
$this->_rawI18N[$name][$fromCode] = $current;
$this->$name = $new;
}
}
}
API documentation generated by ApiGen