src/WebBundle/Service/ExtendService.php line 48

Open in your IDE?
  1. <?php
  2. namespace WebBundle\Service;
  3. /*
  4.  * class: BaseService
  5.  * -----------------------------------------------------
  6.  * базовый класс для сервисов, исключительно для наследования.
  7.  */
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use WebBundle\Helper\App;
  10. abstract class ExtendService
  11. {
  12.     protected array $errors = [];
  13.     /**
  14.      * Получаем объект сервиса
  15.      * @param $id
  16.      * @return object
  17.      */
  18.     public function get($id)
  19.     {
  20.         return App::getContainer()->get($id);
  21.     }
  22.     /**
  23.      * translator
  24.      * @param $val
  25.      * @param array $parameters
  26.      * @param null $domain
  27.      * @param null $locale
  28.      * @return string
  29.      */
  30.     public function translate($val$locale null, array $parameters = [], $domain null): string
  31.     {
  32.         return App::getTranslator()->trans($val$parameters$domain$locale);
  33.     }
  34.     /**
  35.      * Renders a view.
  36.      *
  37.      * @param string $view The view name
  38.      * @param array $parameters An array of parameters to pass to the view
  39.      * @return string
  40.      */
  41.     public function render(string $view, array $parameters = []): string
  42.     {
  43.         $content App::getContainer()->get('twig')->render($view$parameters);
  44.         // фикс лишних табов и переносов строк от твига
  45.         $content preg_replace(['#\\t{2,}#umi'], [' '], $content);
  46.         $content preg_replace(['# {2,}#umi'], [' '], $content);
  47.         return preg_replace(['#\\n{2,}#umi'], [''], $content);
  48.     }
  49.     /**
  50.      * Generates a URL from the given parameters.
  51.      * @param string $route The name of the route
  52.      * @param mixed $parameters An array of parameters
  53.      * @param bool|int|string $referenceType
  54.      * @return string The generated URL
  55.      * @see UrlGeneratorInterface
  56.      */
  57.     public function generateUrl(string $route$parameters = [], $referenceType UrlGeneratorInterface::ABSOLUTE_PATH): string
  58.     {
  59.         return urldecode(App::getRouter()->generate($route$parameters$referenceType));
  60.     }
  61.     protected function setError(string $error): void
  62.     {
  63.         $this->errors[] = $error;
  64.     }
  65.     /**
  66.      * Получение ошибок в сервисе
  67.      * @return string
  68.      */
  69.     public function getErrors(): string
  70.     {
  71.         $error count($this->errors) == $this->errors[0] : implode('; '$this->errors);
  72.         return is_array($error) ? json_encode($error) : $error;
  73.     }
  74.     /**
  75.      * Проверка наличия ошибок в сервисе
  76.      * @return bool
  77.      */
  78.     public function hasErrors(): bool
  79.     {
  80.         return count($this->errors);
  81.     }
  82. }