configurable csv/xlsx export templates (#5531)
This commit is contained in:
24
src/Validator/Constraints/ExportRenderer.php
Normal file
24
src/Validator/Constraints/ExportRenderer.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
final class ExportRenderer extends Constraint
|
||||
{
|
||||
public const UNKNOWN_TYPE = 'kimai-export-type-00';
|
||||
|
||||
protected const ERROR_NAMES = [
|
||||
self::UNKNOWN_TYPE => 'Unknown exporter type.',
|
||||
];
|
||||
|
||||
public string $message = 'Unknown exporter type.';
|
||||
}
|
||||
41
src/Validator/Constraints/ExportRendererValidator.php
Normal file
41
src/Validator/Constraints/ExportRendererValidator.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
final class ExportRendererValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* @param string|mixed $value
|
||||
*/
|
||||
public function validate(mixed $value, Constraint $constraint): void
|
||||
{
|
||||
if (!($constraint instanceof ExportRenderer)) {
|
||||
throw new UnexpectedTypeException($constraint, ExportRenderer::class);
|
||||
}
|
||||
|
||||
if ($value === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$ids = ['csv', 'xlsx'];
|
||||
|
||||
if (!\is_string($value) || !\in_array($value, $ids, true)) {
|
||||
$this->context->buildViolation(ExportRenderer::getErrorName(ExportRenderer::UNKNOWN_TYPE))
|
||||
->setParameter('{{ value }}', $this->formatValue($value))
|
||||
->setTranslationDomain('validators')
|
||||
->setCode(ExportRenderer::UNKNOWN_TYPE)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user