Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver;
13
 
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpKernel\Attribute\MapDateTime;
16
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
17
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
18
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
 
21
/**
22
 * Convert DateTime instances from request attribute variable.
23
 *
24
 * @author Benjamin Eberlei <kontakt@beberlei.de>
25
 * @author Tim Goudriaan <tim@codedmonkey.com>
26
 */
27
final class DateTimeValueResolver implements ArgumentValueResolverInterface, ValueResolverInterface
28
{
29
    /**
30
     * @deprecated since Symfony 6.2, use resolve() instead
31
     */
32
    public function supports(Request $request, ArgumentMetadata $argument): bool
33
    {
34
        @trigger_deprecation('symfony/http-kernel', '6.2', 'The "%s()" method is deprecated, use "resolve()" instead.', __METHOD__);
35
 
36
        return is_a($argument->getType(), \DateTimeInterface::class, true) && $request->attributes->has($argument->getName());
37
    }
38
 
39
    public function resolve(Request $request, ArgumentMetadata $argument): array
40
    {
41
        if (!is_a($argument->getType(), \DateTimeInterface::class, true) || !$request->attributes->has($argument->getName())) {
42
            return [];
43
        }
44
 
45
        $value = $request->attributes->get($argument->getName());
46
        $class = \DateTimeInterface::class === $argument->getType() ? \DateTimeImmutable::class : $argument->getType();
47
 
48
        if ($value instanceof \DateTimeInterface) {
49
            return [$value instanceof $class ? $value : $class::createFromInterface($value)];
50
        }
51
 
52
        if ($argument->isNullable() && !$value) {
53
            return [null];
54
        }
55
 
56
        $format = null;
57
 
58
        if ($attributes = $argument->getAttributes(MapDateTime::class, ArgumentMetadata::IS_INSTANCEOF)) {
59
            $attribute = $attributes[0];
60
            $format = $attribute->format;
61
        }
62
 
63
        if (null !== $format) {
64
            $date = $class::createFromFormat($format, $value);
65
 
66
            if (($class::getLastErrors() ?: ['warning_count' => 0])['warning_count']) {
67
                $date = false;
68
            }
69
        } else {
70
            if (false !== filter_var($value, \FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
71
                $value = '@'.$value;
72
            }
73
            try {
74
                $date = new $class($value ?? 'now');
75
            } catch (\Exception) {
76
                $date = false;
77
            }
78
        }
79
 
80
        if (!$date) {
81
            throw new NotFoundHttpException(sprintf('Invalid date given for parameter "%s".', $argument->getName()));
82
        }
83
 
84
        return [$date];
85
    }
86
}