From c019da9bcf03c80b49336c876d6f4e4cfe579c27 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 6 May 2021 21:23:19 +0200 Subject: [PATCH] Remove templating.yaml, add interfaces. --- .../Templating/TranslatableStringHelper.php | 38 +++++++++---------- .../TranslatableStringHelperInterface.php | 19 ++++++++++ .../ChillMainBundle/config/services.yaml | 5 ++- .../ChillPersonBundle/config/services.yaml | 5 +++ .../config/services/templating.yaml | 12 ------ .../src/Config/ConfigPersonAltNamesHelper.php | 22 +++++------ .../ConfigPersonAltNamesHelperInterface.php | 20 ++++++++++ .../ChillPersonExtension.php | 1 - .../Templating/Entity/ClosingMotiveRender.php | 29 +++++++------- .../src/Templating/Entity/PersonRender.php | 28 +++++++------- 10 files changed, 105 insertions(+), 74 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Templating/TranslatableStringHelperInterface.php delete mode 100644 src/Bundle/ChillPersonBundle/config/services/templating.yaml create mode 100644 src/Bundle/ChillPersonBundle/src/Config/ConfigPersonAltNamesHelperInterface.php diff --git a/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php index 673b87dbe..e3bd0072d 100644 --- a/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php +++ b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php @@ -23,36 +23,36 @@ use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Translation\Translator; /** - * + * * This helper helps to find the string in current locale from translatable_strings - * + * * @author Julien Fastré * */ -class TranslatableStringHelper +class TranslatableStringHelper implements TranslatableStringHelperInterface { /** - * + * * @var RequestStack */ private $requestStack; - + private $fallbackLocales; - + public function __construct(RequestStack $requestStack, Translator $translator) { $this->requestStack = $requestStack; $this->fallbackLocales = $translator->getFallbackLocales(); } - + /** * return the string in current locale if it exists. - * + * * If it does not exists; return the name in the first language available. - * + * * Return a blank string if any strings are available. * Return NULL if $translatableString is NULL - * + * * @param array $translatableStrings * @return string */ @@ -61,32 +61,32 @@ class TranslatableStringHelper if (NULL === $translatableStrings) { return NULL; } - + $language = $this->requestStack->getCurrentRequest()->getLocale(); - + if (isset($translatableStrings[$language])) { - + return $translatableStrings[$language]; } else { foreach ($this->fallbackLocales as $locale) { if (array_key_exists($locale, $translatableStrings)) { - + return $translatableStrings[$locale]; } } - + } - + // no fallback translation... trying the first available $langs = array_keys($translatableStrings); - + if (count($langs) === 0) { return ''; } - + return $translatableStrings[$langs[0]]; } -} \ No newline at end of file +} diff --git a/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelperInterface.php b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelperInterface.php new file mode 100644 index 000000000..3121a3f9e --- /dev/null +++ b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelperInterface.php @@ -0,0 +1,19 @@ +config = $config; } - + /** * Return true if at least one alt name is configured - * + * * @return bool */ public function hasAltNames(): bool { return count($this->config) > 0; } - + /** * get the choices as key => values - * + * * @return array */ public function getChoices(): array { $choices = []; foreach ($this->config as $entry) { - + $labels = $entry['labels']; $lang = false; $label = false; @@ -50,11 +50,11 @@ class ConfigPersonAltNamesHelper if (key($labels) === 'lang') { $lang = current($labels); } - + if (key($labels) === 'label') { $label = current($labels); } - + if ($lang !== FALSE && $label !== FALSE) { $choices[$entry['key']][$lang] = $label; $lang = false; @@ -63,7 +63,7 @@ class ConfigPersonAltNamesHelper $cur = next($labels); } } - + return $choices; } diff --git a/src/Bundle/ChillPersonBundle/src/Config/ConfigPersonAltNamesHelperInterface.php b/src/Bundle/ChillPersonBundle/src/Config/ConfigPersonAltNamesHelperInterface.php new file mode 100644 index 000000000..0c49a4c8a --- /dev/null +++ b/src/Bundle/ChillPersonBundle/src/Config/ConfigPersonAltNamesHelperInterface.php @@ -0,0 +1,20 @@ + values + * + * @return array + */ + public function getChoices(): array; +} diff --git a/src/Bundle/ChillPersonBundle/src/DependencyInjection/ChillPersonExtension.php b/src/Bundle/ChillPersonBundle/src/DependencyInjection/ChillPersonExtension.php index ad97f21fa..b0f804098 100644 --- a/src/Bundle/ChillPersonBundle/src/DependencyInjection/ChillPersonExtension.php +++ b/src/Bundle/ChillPersonBundle/src/DependencyInjection/ChillPersonExtension.php @@ -68,7 +68,6 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac $loader->load('services/search.yaml'); $loader->load('services/command.yaml'); $loader->load('services/form.yaml'); - $loader->load('services/templating.yaml'); $loader->load('services/alt_names.yaml'); $loader->load('services/serializer.yaml'); diff --git a/src/Bundle/ChillPersonBundle/src/Templating/Entity/ClosingMotiveRender.php b/src/Bundle/ChillPersonBundle/src/Templating/Entity/ClosingMotiveRender.php index 8b54fca02..c4106ff45 100644 --- a/src/Bundle/ChillPersonBundle/src/Templating/Entity/ClosingMotiveRender.php +++ b/src/Bundle/ChillPersonBundle/src/Templating/Entity/ClosingMotiveRender.php @@ -4,7 +4,7 @@ namespace Chill\PersonBundle\Templating\Entity; use Chill\MainBundle\Templating\Entity\AbstractChillEntityRender; use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive; -use Chill\MainBundle\Templating\TranslatableStringHelper; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; /** * Render closing motive @@ -13,21 +13,20 @@ use Chill\MainBundle\Templating\TranslatableStringHelper; class ClosingMotiveRender extends AbstractChillEntityRender { private CONST SEPARATOR = ' > '; - + /** - * - * @var TranslatableStringHelper + * @var TranslatableStringHelperInterface */ private $translatableStringHelper; - - public function __construct(TranslatableStringHelper $translatableStringHelper) + + public function __construct(TranslatableStringHelperInterface $translatableStringHelper) { $this->translatableStringHelper = $translatableStringHelper; } - + public function renderBox($entity, array $options): string { - return + return $this->getDefaultOpeningBox('closing-motive'). $this->renderString($entity, $options). $this->getDefaultClosingBox() @@ -35,28 +34,28 @@ class ClosingMotiveRender extends AbstractChillEntityRender } /** - * + * * @param ClosingMotive $entity * @param array $options * @return string */ public function renderString($entity, array $options): string { - return $this->renderStringRecursive($entity, - '', //$this->translatableStringHelper->localize($entity->getName()), + return $this->renderStringRecursive($entity, + '', //$this->translatableStringHelper->localize($entity->getName()), $options); } - + protected function renderStringRecursive(ClosingMotive $motive, $existing, array $options) { $newExisting = $this->translatableStringHelper->localize($motive->getName()); - + if ($motive->hasParent()) { - + if (!empty($existing)) { $newExisting = $newExisting.self::SEPARATOR.$existing; } - + return $this->renderStringRecursive($motive->getParent(), $newExisting, $options); } else { diff --git a/src/Bundle/ChillPersonBundle/src/Templating/Entity/PersonRender.php b/src/Bundle/ChillPersonBundle/src/Templating/Entity/PersonRender.php index d1f554492..5969a8c26 100644 --- a/src/Bundle/ChillPersonBundle/src/Templating/Entity/PersonRender.php +++ b/src/Bundle/ChillPersonBundle/src/Templating/Entity/PersonRender.php @@ -2,7 +2,7 @@ /* * Chill is a software for social workers * - * Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS, + * Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS, * * * This program is free software: you can redistribute it and/or modify @@ -22,7 +22,7 @@ namespace Chill\PersonBundle\Templating\Entity; use Chill\MainBundle\Templating\Entity\AbstractChillEntityRender; use Chill\PersonBundle\Entity\Person; -use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper; +use Chill\PersonBundle\Config\ConfigPersonAltNamesHelperInterface; /** * Render a Person @@ -32,24 +32,24 @@ class PersonRender extends AbstractChillEntityRender { /** * - * @var ConfigPersonAltNamesHelper + * @var ConfigPersonAltNamesHelperInterface */ protected $configAltNamesHelper; - - public function __construct(ConfigPersonAltNamesHelper $configAltNamesHelper) + + public function __construct(ConfigPersonAltNamesHelperInterface $configAltNamesHelper) { $this->configAltNamesHelper = $configAltNamesHelper; } - + /** - * + * * @param Person $person * @param array $options * @return string */ public function renderBox($person, array $options): string { - return + return $this->getDefaultOpeningBox('person'). ''.$person->getFirstName().''. ' '.$person->getLastName().''. @@ -59,7 +59,7 @@ class PersonRender extends AbstractChillEntityRender } /** - * + * * @param Person $person * @param array $options * @return string @@ -69,11 +69,11 @@ class PersonRender extends AbstractChillEntityRender return $person->getFirstName().' '.$person->getLastName() .$this->addAltNames($person, false); } - + protected function addAltNames(Person $person, bool $addSpan) { $str = ''; - + if ($this->configAltNamesHelper->hasAltNames()) { $altNames = $this->configAltNamesHelper->getChoices(); $isFirst = true; @@ -91,18 +91,18 @@ class PersonRender extends AbstractChillEntityRender $str .= ''; } $str .= $altName->getLabel(); - + if ($addSpan) { $str .= ""; } } } - + if (!$isFirst) { $str .= ")"; } } - + return $str; }