Files
grzanieplus.pl/plugins/stLanguagePlugin/lib/stLanguageModelI18nHelper.class.php
2025-03-12 17:06:23 +01:00

82 lines
2.6 KiB
PHP

<?php
class stLanguageModelI18nHelper
{
/**
* Ustawia wartość dla pola I18n
*
* @param BaseObject $obj Instancja modelu
* @param string $methodName Nazwa metody pola
* @param string|null|array|object $value Wartość pola
* @return void
*/
public static function setI18nField(BaseObject $obj, string $methodName, $value): void
{
list(, $methodName) = explode("::", $methodName);
if (call_user_func([$obj, 'getCulture']) === stLanguage::getOptLanguage())
{
$optMethodName = substr_replace($methodName, "setOpt", 0, 3);
if (method_exists($obj, $optMethodName))
{
call_user_func([$obj, $optMethodName], $value);
}
}
self::callParentMethod($obj, $methodName, [$value]);
}
/**
* Zwracs wartość dla pola I18n
*
* @param BaseObject $obj Instancja modelu
* @param string $methodName Nazwa metody pola
* @return null|string|array|object
*/
public static function getI18nField(BaseObject $obj, string $methodName)
{
list(, $methodName) = explode("::", $methodName);
if (call_user_func([$obj, 'getCulture']) === stLanguage::getOptLanguage() || null === self::callParentMethod($obj, $methodName))
{
$optMethodName = substr_replace($methodName, "getOpt", 0, 3);
if (!method_exists($obj, $optMethodName))
{
$culture = call_user_func([$obj, 'getCulture']);
call_user_func([$obj, 'setCulture'], stLanguage::getOptLanguage());
$result = self::callParentMethod($obj, $methodName);
call_user_func([$obj, 'setCulture'], $culture);
}
else
{
$result = call_user_func([$obj, $optMethodName]);
}
}
else
{
$result = self::callParentMethod($obj, $methodName);
}
return $result;
}
/**
* Metoda pomocnicza umożliwiająca wywołanie metody z obiektu nadrzędnego
*
* @param BaseObject $obj Instancja modelu
* @param string $method Nazwa metody
* @param array $args Dodatkowe argumenty
* @return mixed
*/
protected static function callParentMethod(BaseObject $obj, string $method, array $args = [])
{
$objectContextFix = function(BaseObject $obj, string $method, array $args) {
$parentClass = get_parent_class($obj);
return call_user_func_array([$parentClass, $method], $args);
};
return $objectContextFix->call($obj, $obj, $method, $args);
}
}