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

169 lines
5.8 KiB
PHP

<?php
/**
* Definijce zwracanych błędów
*/
if (!defined("WEBAPI_INCORRECT_ID"))
{
define( "WEBAPI_INCORRECT_ID", "Błędny numer ID." );
define( "WEBAPI_ADD_ERROR", "Błąd podczas dodawania danych. Informacja: %s" );
define( "WEBAPI_UPDATE_ERROR", "Błąd podczas aktualizacji danych. Informacja: %s" );
define( "WEBAPI_DELETE_ERROR", "Błąd podczas usuwania danych. Informacja: %s" );
define( "WEBAPI_COUNT_ERROR", "Błąd podczas liczenia danych. Informacja: %s" );
define( "WEBAPI_VALIDATE_ERROR", "Błąd podczas sprawdzania danych, niewłasciwa wartość pola \"%s\"." );
define( "WEBAPI_VALIDATE_UNIQUE_ERROR", "Błąd podczas sprawdzania danych, podana wartość dla pola \"%s\" już istnieje." );
define( "WEBAPI_REQUIRE_ERROR", "Nie podano wymaganego pola: \"%s\"." );
define( "WEBAPI_CONFIG_ERROR", "Błąd w pliku konfiguracyjnym modułu.");
}
abstract class stWebApiBase
{
public function __construct()
{
sfLoader::loadHelpers(array('Helper', 'stUrl'));
if (stSecurity::getSSL())
{
sfContext::getInstance()->getRequest()->setIsSecure(true);
}
stPluginHelper::addRouting('stWebApiProductUrlLangFrontend', '/:lang/:url.html', 'stProduct', 'show', null, array(), array('lang' => '[a-z]{2,2}'));
stPluginHelper::addRouting('stWebApiProductUrlFrontend', '/:url.html', 'stProduct', 'show', null);
stPluginHelper::addRouting('stWebApiCategoryUrlFrontend', '/category/:url', 'stProduct', 'list');
stPluginHelper::addRouting('stWebApiCategoryUrlLangFrontend', '/category/:lang/:url', 'stProduct', 'list', null, array(), array('lang' => '[a-z]{2,2}'));
stPluginHelper::addRouting('stWebApiProducerUrlFrontend', '/manufacturer/:url', 'stProduct', 'producerList');
stPluginHelper::addRouting('stWebApiProducerUrlLangFrontend', '/manufacturer/:lang/:url', 'stProduct', 'producerList', null, array(), array('lang' => '[a-z]{2,2}'));
$this->__setCulture(stLanguage::getOptLanguage());
}
/**
* Zwraca przetłumaczony tekst
*
* @param string $message
* @param array $args
* @param null|string $catalogue
* @return mixed
*/
public static function translate(string $text, array $args = [], ?string $catalogue = null): string
{
return sfContext::getInstance()->getI18n()->__($text, $args, null !== $catalogue ? $catalogue : 'stWebApiBackend');
}
/**
* Wyrzuca wyjątek z komunikatem błędu
*
* @param string $code Kod błędu
* @param string $errorMsg Komunikat błędu
* @param array $i18nArgs Dodatkowe argumenty komunikatu
* @param null|string $i18nCatalogue Katalog tłumaczeń
* @return void
* @throws SoapFault
*/
public function throwSoapFault(string $code, string $errorMsg, array $i18nArgs = [], ?string $i18nCatalogue = null): void
{
throw new SoapFault($code, self::translate($errorMsg, $i18nArgs, $i18nCatalogue), null);
}
public function __(string $text, array $args = [], ?string $catalogue = null): string
{
return self::translate($text, $args, $catalogue);
}
public function __setCulture($culture)
{
if ($culture == 'en')
{
$culture = 'en_US';
}
elseif ($culture == 'pl')
{
$culture = 'pl_PL';
}
$this->_culture = $culture;
sfContext::getInstance()->getRequest()->setHostByCulture($culture);
}
public function __getCulture()
{
return $this->_culture ? $this->_culture : stLanguage::getOptLanguage();
}
/**
* @param Product $product
* @return false|string
*/
public function __getProductUrl(Product $product)
{
return st_url_for(array('module' => 'stProduct', 'action' => 'show', 'url' => $product->getUrl()), true, 'frontend', null, $product->getCulture());
}
/**
* @param Category $category
* @return false|string
*/
public function __getCategoryUrl(Category $category)
{
return st_url_for(array('module' => 'stProduct', 'action' => 'list', 'url' => $category->getUrl()), true, 'frontend', null, $category->getCulture());
}
/**
* @param Producer $producer
* @return false|string
*/
public function __getProducerUrl(Producer $producer)
{
return st_url_for(array('module' => 'stProduct', 'action' => 'producerList', 'url' => $producer->getUrl()), true, 'frontend', null, $producer->getCulture());
}
protected function validateArrayOfInteger($values, string $fieldPath)
{
if (!is_array($values))
{
self::throwSoapFault('3', WEBAPI_VALIDATE_ERROR, ['%s' => $fieldPath]);
}
foreach ($values as $index => $value)
{
if (!is_numeric($value))
{
self::throwSoapFault('3', WEBAPI_VALIDATE_ERROR, ['%s' => $fieldPath . '['.$index.']']);
}
}
}
protected function validateArrayOfString($values, string $fieldPath)
{
if (!is_array($values))
{
self::throwSoapFault('3', WEBAPI_VALIDATE_ERROR, ['%s' => $fieldPath]);
}
foreach ($values as $index => $value)
{
if (!is_string($value))
{
self::throwSoapFault('3', WEBAPI_VALIDATE_ERROR, ['%s' => $fieldPath . '['.$index.']']);
}
}
}
protected function validateArrayOfDouble($values, string $fieldPath)
{
if (!is_array($values))
{
self::throwSoapFault('3', WEBAPI_VALIDATE_ERROR, ['%s' => $fieldPath]);
}
foreach ($values as $index => $value)
{
if (!is_numeric($value))
{
self::throwSoapFault('3', WEBAPI_VALIDATE_ERROR, ['%s' => $fieldPath . '['.$index.']']);
}
}
}
}