Next major version 2 with PHP 8.1, Symfony 6, Tabler UI, 2FA ... (#2902)
This commit is contained in:
@@ -14,32 +14,16 @@ use App\Validator\Constraints\Duration as DurationConstraint;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
|
||||
class DurationStringToSecondsTransformer implements DataTransformerInterface
|
||||
final class DurationStringToSecondsTransformer implements DataTransformerInterface
|
||||
{
|
||||
/**
|
||||
* @var Duration
|
||||
*/
|
||||
protected $formatter;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $pattern;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->formatter = new Duration();
|
||||
$constraint = new DurationConstraint();
|
||||
$this->pattern = $constraint->pattern;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $intToFormat
|
||||
* @return string|null
|
||||
*/
|
||||
public function transform($intToFormat)
|
||||
public function transform(mixed $intToFormat): ?string
|
||||
{
|
||||
try {
|
||||
return $this->formatter->format($intToFormat);
|
||||
return (new Duration())->format($intToFormat);
|
||||
} catch (\Exception | \TypeError $e) {
|
||||
throw new TransformationFailedException($e->getMessage());
|
||||
}
|
||||
@@ -49,7 +33,7 @@ class DurationStringToSecondsTransformer implements DataTransformerInterface
|
||||
* @param string|null $formatToInt
|
||||
* @return int|null
|
||||
*/
|
||||
public function reverseTransform($formatToInt)
|
||||
public function reverseTransform(mixed $formatToInt): ?int
|
||||
{
|
||||
if (null === $formatToInt) {
|
||||
return null;
|
||||
@@ -60,12 +44,12 @@ class DurationStringToSecondsTransformer implements DataTransformerInterface
|
||||
}
|
||||
|
||||
// we need this one here, because the data transformer is executed BEFORE the constraint is called
|
||||
if (!preg_match($this->pattern, $formatToInt)) {
|
||||
if (!preg_match((new DurationConstraint())->pattern, $formatToInt)) {
|
||||
throw new TransformationFailedException('Invalid duration format given');
|
||||
}
|
||||
|
||||
try {
|
||||
$seconds = $this->formatter->parseDurationString($formatToInt);
|
||||
$seconds = (new Duration())->parseDurationString($formatToInt);
|
||||
|
||||
// DateTime throws if a duration with too many seconds is passed and an amount of so
|
||||
// many seconds is likely not required in a time-tracking application ;-)
|
||||
|
||||
@@ -13,7 +13,7 @@ use App\Utils\SearchTerm;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
|
||||
class SearchTermTransformer implements DataTransformerInterface
|
||||
final class SearchTermTransformer implements DataTransformerInterface
|
||||
{
|
||||
/**
|
||||
* Transforms a SearchTerm object to a string.
|
||||
@@ -21,7 +21,7 @@ class SearchTermTransformer implements DataTransformerInterface
|
||||
* @param SearchTerm|null $searchTerm
|
||||
* @return string
|
||||
*/
|
||||
public function transform($searchTerm)
|
||||
public function transform(mixed $searchTerm): mixed
|
||||
{
|
||||
if (empty($searchTerm) || !($searchTerm instanceof SearchTerm)) {
|
||||
return '';
|
||||
@@ -37,7 +37,7 @@ class SearchTermTransformer implements DataTransformerInterface
|
||||
* @return SearchTerm|null
|
||||
* @throws TransformationFailedException if object (issue) is not found
|
||||
*/
|
||||
public function reverseTransform($searchTerm)
|
||||
public function reverseTransform(mixed $searchTerm): mixed
|
||||
{
|
||||
if (empty($searchTerm)) {
|
||||
return null;
|
||||
|
||||
58
src/Form/DataTransformer/StringToArrayTransformer.php
Normal file
58
src/Form/DataTransformer/StringToArrayTransformer.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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\Form\DataTransformer;
|
||||
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
|
||||
/**
|
||||
* @implements DataTransformerInterface<array, string>
|
||||
*/
|
||||
final class StringToArrayTransformer implements DataTransformerInterface
|
||||
{
|
||||
/**
|
||||
* @param non-empty-string $separator
|
||||
*/
|
||||
public function __construct(private readonly string $separator = ',')
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms an array of strings to a string.
|
||||
*
|
||||
* @param array<string> $value
|
||||
* @return string
|
||||
*/
|
||||
public function transform(mixed $value): mixed
|
||||
{
|
||||
if (empty($value)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return implode($this->separator, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a string to an array of tags.
|
||||
*
|
||||
* @param string|null $value
|
||||
* @return array<string>
|
||||
* @throws TransformationFailedException
|
||||
*/
|
||||
public function reverseTransform(mixed $value): mixed
|
||||
{
|
||||
// check for empty list
|
||||
if ('' === $value || null === $value) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_filter(array_unique(array_map('trim', explode($this->separator, $value))));
|
||||
}
|
||||
}
|
||||
@@ -14,16 +14,10 @@ use App\Repository\TagRepository;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
|
||||
class TagArrayToStringTransformer implements DataTransformerInterface
|
||||
final class TagArrayToStringTransformer implements DataTransformerInterface
|
||||
{
|
||||
/**
|
||||
* @var TagRepository
|
||||
*/
|
||||
private $tagRepository;
|
||||
|
||||
public function __construct(TagRepository $tagRepository)
|
||||
public function __construct(private TagRepository $tagRepository)
|
||||
{
|
||||
$this->tagRepository = $tagRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,7 +26,7 @@ class TagArrayToStringTransformer implements DataTransformerInterface
|
||||
* @param Tag[]|null $tags
|
||||
* @return string
|
||||
*/
|
||||
public function transform($tags)
|
||||
public function transform(mixed $tags): mixed
|
||||
{
|
||||
if (empty($tags)) {
|
||||
return '';
|
||||
@@ -50,7 +44,7 @@ class TagArrayToStringTransformer implements DataTransformerInterface
|
||||
* @return Tag[]
|
||||
* @throws TransformationFailedException
|
||||
*/
|
||||
public function reverseTransform($stringOfTags)
|
||||
public function reverseTransform(mixed $stringOfTags): mixed
|
||||
{
|
||||
// check for empty tag list
|
||||
if ('' === $stringOfTags || null === $stringOfTags) {
|
||||
|
||||
Reference in New Issue
Block a user