Add explicit return types and TranslatableInterface support

Updated interfaces to include explicit return types for improved type safety and readability. Integrated support for Symfony's TranslatableInterface in relevant methods to enhance translation handling.
This commit is contained in:
Julien Fastré 2025-04-07 12:56:37 +02:00
parent 2482dcc62e
commit 8331a836f2
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
8 changed files with 72 additions and 34 deletions

View File

@ -12,27 +12,41 @@ declare(strict_types=1);
namespace Chill\MainBundle\Export; namespace Chill\MainBundle\Export;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatableInterface;
/** /**
* Interface for Aggregators. * Interface for Aggregators.
* *
* Aggregators gather result of a query. Most of the time, it will add * Aggregators gather result of a query. Most of the time, it will add
* a GROUP BY clause. * a GROUP BY clause.
*
* @template D of array
*/ */
interface AggregatorInterface extends ModifierInterface interface AggregatorInterface extends ModifierInterface
{ {
/** /**
* Add a form to collect data from the user. * Add a form to collect data from the user.
*/ */
public function buildForm(FormBuilderInterface $builder); public function buildForm(FormBuilderInterface $builder): void;
/** /**
* Get the default data, that can be use as "data" for the form. * Get the default data, that can be use as "data" for the form.
*
* @return D
*/ */
public function getFormDefaultData(): array; public function getFormDefaultData(): array;
/**
* @param D $formData
* @return array
*/
public function normalizeFormData(array $formData): array; public function normalizeFormData(array $formData): array;
/**
* @param array $formData
* @param int $fromVersion
* @return D
*/
public function denormalizeFormData(array $formData, int $fromVersion): array; public function denormalizeFormData(array $formData, int $fromVersion): array;
public function getNormalizationVersion(): int; public function getNormalizationVersion(): int;
@ -80,7 +94,7 @@ interface AggregatorInterface extends ModifierInterface
* @param string $key The column key, as added in the query * @param string $key The column key, as added in the query
* @param mixed[] $values The values from the result. if there are duplicates, those might be given twice. Example: array('FR', 'BE', 'CZ', 'FR', 'BE', 'FR') * @param mixed[] $values The values from the result. if there are duplicates, those might be given twice. Example: array('FR', 'BE', 'CZ', 'FR', 'BE', 'FR')
* *
* @return \Closure where the first argument is the value, and the function should return the label to show in the formatted file. Example : `function($countryCode) use ($countries) { return $countries[$countryCode]->getName(); }` * @return (callable(string|int|float|'_header'|null $value): string|int|\DateTimeInterface|TranslatableInterface) where the first argument is the value, and the function should return the label to show in the formatted file. Example : `function($countryCode) use ($countries) { return $countries[$countryCode]->getName(); }`
*/ */
public function getLabels($key, array $values, mixed $data); public function getLabels($key, array $values, mixed $data);
@ -91,7 +105,8 @@ interface AggregatorInterface extends ModifierInterface
* Example: if your query builder will contains `SELECT count(id) AS count_id ...`, * Example: if your query builder will contains `SELECT count(id) AS count_id ...`,
* this function will return `array('count_id')`. * this function will return `array('count_id')`.
* *
* @param mixed[] $data the data from the export's form (added by self::buildForm) * @param D $data the data from the export's form (added by self::buildForm)
* @return list<string>
*/ */
public function getQueryKeys($data); public function getQueryKeys(array $data): array;
} }

View File

@ -11,6 +11,8 @@ declare(strict_types=1);
namespace Chill\MainBundle\Export; namespace Chill\MainBundle\Export;
use Symfony\Contracts\Translation\TranslatableInterface;
/** /**
* The common methods between different object used to build export (i.e. : ExportInterface, * The common methods between different object used to build export (i.e. : ExportInterface,
* FilterInterface, AggregatorInterface). * FilterInterface, AggregatorInterface).
@ -19,8 +21,6 @@ interface ExportElementInterface
{ {
/** /**
* get a title, which will be used in UI (and translated). * get a title, which will be used in UI (and translated).
*
* @return string
*/ */
public function getTitle(); public function getTitle(): string|TranslatableInterface;
} }

View File

@ -31,5 +31,5 @@ interface ExportElementValidatedInterface
* validate the form's data and, if required, build a contraint * validate the form's data and, if required, build a contraint
* violation on the data. * violation on the data.
*/ */
public function validateForm(mixed $data, ExecutionContextInterface $context); public function validateForm(mixed $data, ExecutionContextInterface $context): void;
} }

View File

@ -21,7 +21,7 @@ namespace Chill\MainBundle\Export;
interface ExportElementsProviderInterface interface ExportElementsProviderInterface
{ {
/** /**
* @return ExportElementInterface[] * @return iterable<ExportElementInterface>
*/ */
public function getExportElements(); public function getExportElements(): iterable;
} }

View File

@ -14,6 +14,7 @@ namespace Chill\MainBundle\Export;
use Doctrine\ORM\NativeQuery; use Doctrine\ORM\NativeQuery;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatableInterface;
/** /**
* Interface for Export. * Interface for Export.
@ -27,6 +28,7 @@ use Symfony\Component\Form\FormBuilderInterface;
* @example Chill\PersonBundle\Export\CountPerson an example of implementation * @example Chill\PersonBundle\Export\CountPerson an example of implementation
* *
* @template Q of QueryBuilder|NativeQuery * @template Q of QueryBuilder|NativeQuery
* @tempalte D of array
*/ */
interface ExportInterface extends ExportElementInterface interface ExportInterface extends ExportElementInterface
{ {
@ -95,9 +97,9 @@ interface ExportInterface extends ExportElementInterface
* database. But the header must be, in every case, translated. * database. But the header must be, in every case, translated.
* *
* @param string $key The column key, as added in the query * @param string $key The column key, as added in the query
* @param mixed[] $values The values from the result. if there are duplicates, those might be given twice. Example: array('FR', 'BE', 'CZ', 'FR', 'BE', 'FR') * @param list<mixed> $values The values from the result. if there are duplicates, those might be given twice. Example: array('FR', 'BE', 'CZ', 'FR', 'BE', 'FR')
* *
* @return (callable(string|int|float|'_header'|null $value): string|int|\DateTimeInterface) where the first argument is the value, and the function should return the label to show in the formatted file. Example : `function($countryCode) use ($countries) { return $countries[$countryCode]->getName(); }` * @return (callable(string|int|float|'_header'|null $value): string|int|\DateTimeInterface|TranslatableInterface) where the first argument is the value, and the function should return the label to show in the formatted file. Example : `function($countryCode) use ($countries) { return $countries[$countryCode]->getName(); }`
*/ */
public function getLabels($key, array $values, mixed $data); public function getLabels($key, array $values, mixed $data);
@ -108,29 +110,27 @@ interface ExportInterface extends ExportElementInterface
* Example: if your query builder will contains `SELECT count(id) AS count_id ...`, * Example: if your query builder will contains `SELECT count(id) AS count_id ...`,
* this function will return `array('count_id')`. * this function will return `array('count_id')`.
* *
* @param mixed[] $data the data from the export's form (added by self::buildForm) * @param D $data the data from the export's form (added by self::buildForm)
*/ */
public function getQueryKeys($data); public function getQueryKeys(array $data): array;
/** /**
* Return the results of the query builder. * Return the results of the query builder.
* *
* @param Q $query * @param Q $query
* @param mixed[] $data the data from the export's fomr (added by self::buildForm) * @param D $data the data from the export's fomr (added by self::buildForm)
* *
* @return mixed[] an array of results * @return mixed[] an array of results
*/ */
public function getResult($query, $data, ExportGenerationContext $context); public function getResult(QueryBuilder|NativeQuery $query, array $data, ExportGenerationContext $context): array;
/** /**
* Return the Export's type. This will inform _on what_ export will apply. * Return the Export's type. This will inform _on what_ export will apply.
* Most of the type, it will be a string which references an entity. * Most of the type, it will be a string which references an entity.
* *
* Example of types : Chill\PersonBundle\Export\Declarations::PERSON_TYPE * Example of types : Chill\PersonBundle\Export\Declarations::PERSON_TYPE
*
* @return string
*/ */
public function getType(); public function getType(): string;
/** /**
* The initial query, which will be modified by ModifiersInterface * The initial query, which will be modified by ModifiersInterface
@ -147,10 +147,19 @@ interface ExportInterface extends ExportElementInterface
* *
* @return Q the query to execute * @return Q the query to execute
*/ */
public function initiateQuery(array $requiredModifiers, array $acl, array $data, ExportGenerationContext $context); public function initiateQuery(array $requiredModifiers, array $acl, array $data, ExportGenerationContext $context): QueryBuilder|NativeQuery;
/**
* @param D $formData
* @return array
*/
public function normalizeFormData(array $formData): array; public function normalizeFormData(array $formData): array;
/**
* @param array $formData the normalized data
* @param int $fromVersion
* @return D
*/
public function denormalizeFormData(array $formData, int $fromVersion): array; public function denormalizeFormData(array $formData, int $fromVersion): array;
public function getNormalizationVersion(): int; public function getNormalizationVersion(): int;

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Export; namespace Chill\MainBundle\Export;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatableInterface;
/** /**
* Interface for filters. * Interface for filters.
@ -20,6 +21,8 @@ use Symfony\Component\Form\FormBuilderInterface;
* it will add a `WHERE` clause on this query. * it will add a `WHERE` clause on this query.
* *
* Filters should not add column in `SELECT` clause. * Filters should not add column in `SELECT` clause.
*
* @template D of array
*/ */
interface FilterInterface extends ModifierInterface interface FilterInterface extends ModifierInterface
{ {
@ -28,18 +31,29 @@ interface FilterInterface extends ModifierInterface
/** /**
* Add a form to collect data from the user. * Add a form to collect data from the user.
*/ */
public function buildForm(FormBuilderInterface $builder); public function buildForm(FormBuilderInterface $builder): void;
/** /**
* Get the default data, that can be use as "data" for the form. * Get the default data, that can be use as "data" for the form.
* *
* In case of adding new parameters to a filter, you can implement a @see{DataTransformerFilterInterface} to * In case of adding new parameters to a filter, you can implement a @see{DataTransformerFilterInterface} to
* transforme the filters's data saved in an export to the desired state. * transforme the filters's data saved in an export to the desired state.
*
* @return D
*/ */
public function getFormDefaultData(): array; public function getFormDefaultData(): array;
/**
* @param D $formData
* @return array
*/
public function normalizeFormData(array $formData): array; public function normalizeFormData(array $formData): array;
/**
* @param array $formData
* @param int $fromVersion
* @return D
*/
public function denormalizeFormData(array $formData, int $fromVersion): array; public function denormalizeFormData(array $formData, int $fromVersion): array;
public function getNormalizationVersion(): int; public function getNormalizationVersion(): int;
@ -58,7 +72,7 @@ interface FilterInterface extends ModifierInterface
* supported, later some 'html' will be added. The filter should always * supported, later some 'html' will be added. The filter should always
* implement the 'string' format and fallback to it if other format are used. * implement the 'string' format and fallback to it if other format are used.
* *
* If no i18n is necessery, or if the filter translate the string by himself, * If no i18n is necessary, or if the filter translate the string by himself,
* this function should return a string. If the filter does not do any translation, * this function should return a string. If the filter does not do any translation,
* the return parameter should be an array, where * the return parameter should be an array, where
* *
@ -69,10 +83,9 @@ interface FilterInterface extends ModifierInterface
* *
* Example: `array('my string with %parameter%', ['%parameter%' => 'good news'], 'mydomain', 'mylocale')` * Example: `array('my string with %parameter%', ['%parameter%' => 'good news'], 'mydomain', 'mylocale')`
* *
* @param array $data * @param D $data
* @param string $format the format
* *
* @return array|string a string with the data or, if translatable, an array where first element is string, second elements is an array of arguments * @return array|string|TranslatableInterface a string with the data or, if translatable, an array where first element is string, second elements is an array of arguments
*/ */
public function describeAction($data, $format = 'string'); public function describeAction(array $data): array|string|TranslatableInterface;
} }

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Export; namespace Chill\MainBundle\Export;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatableInterface;
/** /**
* @method generate($result, $formatterData, string $exportAlias, array $exportData, array $filtersData, array $aggregatorsData): FormattedExportGeneration * @method generate($result, $formatterData, string $exportAlias, array $exportData, array $filtersData, array $aggregatorsData): FormattedExportGeneration
@ -33,16 +34,16 @@ interface FormatterInterface
*/ */
public function buildForm( public function buildForm(
FormBuilderInterface $builder, FormBuilderInterface $builder,
$exportAlias, string $exportAlias,
array $aggregatorAliases, array $aggregatorAliases,
); ): void;
/** /**
* get the default data for the form build by buildForm. * get the default data for the form build by buildForm.
*/ */
public function getFormDefaultData(array $aggregatorAliases): array; public function getFormDefaultData(array $aggregatorAliases): array;
public function getName(); public function getName(): string|TranslatableInterface;
/** /**
* Generate a response from the data collected on differents ExportElementInterface. * Generate a response from the data collected on differents ExportElementInterface.
@ -54,6 +55,8 @@ interface FormatterInterface
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data * @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
* *
* @return \Symfony\Component\HttpFoundation\Response The response to be shown * @return \Symfony\Component\HttpFoundation\Response The response to be shown
*
* @deprecated use generate instead
*/ */
public function getResponse( public function getResponse(
$result, $result,
@ -64,7 +67,7 @@ interface FormatterInterface
array $aggregatorsData, array $aggregatorsData,
); );
public function getType(); public function getType(): string;
public function normalizeFormData(array $formData): array; public function normalizeFormData(array $formData): array;

View File

@ -36,15 +36,13 @@ interface ModifierInterface extends ExportElementInterface
* *
* @param QueryBuilder $qb the QueryBuilder initiated by the Export (and eventually modified by other Modifiers) * @param QueryBuilder $qb the QueryBuilder initiated by the Export (and eventually modified by other Modifiers)
* @param mixed[] $data the data from the Form (builded by buildForm) * @param mixed[] $data the data from the Form (builded by buildForm)
*
* @return void
*/ */
public function alterQuery(QueryBuilder $qb, $data, ExportGenerationContext $exportGenerationContext); public function alterQuery(QueryBuilder $qb, $data, ExportGenerationContext $exportGenerationContext): void;
/** /**
* On which type of Export this ModifiersInterface may apply. * On which type of Export this ModifiersInterface may apply.
* *
* @return string the type on which the Modifiers apply * @return string the type on which the Modifiers apply
*/ */
public function applyOn(); public function applyOn(): string;
} }