• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarlepp / symfony-flex-backend / #5622

22 Feb 2025 10:59PM UTC coverage: 100.0%. Remained the same
#5622

Pull #2945

php-coveralls

web-flow
Merge ca05c026b into d9edad4e5
Pull Request #2945: Chore(ci) - GitHub actions image update

2299 of 2299 relevant lines covered (100.0%)

92.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/AutoMapper/RestRequestMapper.php
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/AutoMapper/RestRequestMapper.php
5
 *
6
 * @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
7
 */
8

9
namespace App\AutoMapper;
10

11
use App\DTO\RestDtoInterface;
12
use AutoMapperPlus\MapperInterface;
13
use InvalidArgumentException;
14
use LengthException;
15
use Override;
16
use ReflectionClass;
17
use ReflectionNamedType;
18
use Symfony\Component\HttpFoundation\Request;
19
use function array_filter;
20
use function gettype;
21
use function is_object;
22
use function method_exists;
23
use function sprintf;
24
use function ucfirst;
25

26
/**
27
 * @package App\AutoMapper
28
 * @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
29
 */
30
abstract class RestRequestMapper implements MapperInterface
31
{
32
    /**
33
     * Properties to map to destination object.
34
     *
35
     * @var array<int, non-empty-string>
36
     */
37
    protected static array $properties = [];
38

39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @psalm-param array<array-key, mixed>|object $source
43
     * @psalm-param array<int, mixed> $context
44
     */
45
    #[Override]
46
    public function map($source, string $targetClass, array $context = []): RestDtoInterface
47
    {
48
        /** @psalm-var class-string $targetClass */
49
        $destination = new $targetClass();
86✔
50

51
        return $this->mapToObject($source, $destination, $context);
86✔
52
    }
53

54
    /**
55
     * {@inheritdoc}
56
     *
57
     * @psalm-param array<array-key, mixed>|object $source
58
     * @psalm-param object $destination
59
     * @psalm-param array<int, mixed> $context
60
     */
61
    #[Override]
62
    public function mapToObject($source, $destination, array $context = []): RestDtoInterface
63
    {
64
        if (!is_object($source)) {
123✔
65
            throw new InvalidArgumentException(
1✔
66
                sprintf(
1✔
67
                    'RestRequestMapper expects that $source is Request object, "%s" provided',
1✔
68
                    gettype($source),
1✔
69
                )
1✔
70
            );
1✔
71
        }
72

73
        if (!$source instanceof Request) {
122✔
74
            throw new InvalidArgumentException(
1✔
75
                sprintf(
1✔
76
                    'RestRequestMapper expects that $source is Request object, "%s" provided',
1✔
77
                    $source::class,
1✔
78
                )
1✔
79
            );
1✔
80
        }
81

82
        if (!$destination instanceof RestDtoInterface) {
121✔
83
            throw new InvalidArgumentException(
1✔
84
                sprintf(
1✔
85
                    'RestRequestMapper expects that $destination is instance of RestDtoInterface object, "%s" provided',
1✔
86
                    $destination::class,
1✔
87
                )
1✔
88
            );
1✔
89
        }
90

91
        if (static::$properties === []) {
120✔
92
            throw new LengthException(
1✔
93
                sprintf(
1✔
94
                    'RestRequestMapper expects that mapper "%s::$properties" contains properties to convert',
1✔
95
                    static::class,
1✔
96
                )
1✔
97
            );
1✔
98
        }
99

100
        return $this->getObject($source, $destination);
119✔
101
    }
102

103
    private function getObject(Request $request, RestDtoInterface $restDto): RestDtoInterface
104
    {
105
        $reflectionClass = new ReflectionClass($restDto::class);
119✔
106

107
        foreach ($this->getValidProperties($request) as $property) {
119✔
108
            $setter = 'set' . ucfirst($property);
32✔
109
            $transformer = 'transform' . ucfirst($property);
32✔
110
            $type = $reflectionClass->getProperty($property)->getType();
32✔
111

112
            $value = $type instanceof ReflectionNamedType && $type->getName() === 'array'
32✔
113
                ? $request->request->all($property)
8✔
114
                : $request->request->get($property);
24✔
115

116
            if (method_exists($this, $transformer)) {
32✔
117
                /** @var int|string|object|array<mixed>|null $value */
118
                $value = $this->{$transformer}($value);
24✔
119
            }
120

121
            $restDto->{$setter}($value);
32✔
122
        }
123

124
        return $restDto;
119✔
125
    }
126

127
    /**
128
     * @return array<int, non-empty-string>
129
     */
130
    private function getValidProperties(Request $request): array
131
    {
132
        return array_filter(static::$properties, static fn ($property) => $request->request->has($property));
119✔
133
    }
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc