Issues (31)

Attribute/RequestBody.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Attribute;
6
7
use FRZB\Component\RequestMapper\Helper\PropertyHelper;
8
use JetBrains\PhpStorm\Immutable;
9
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
10
11
/** @final */
12
#[Immutable]
13
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)]
14
class RequestBody
15
{
16
    public const DEFAULT_VALIDATION_GROUPS = ['Default'];
17
    public const DEFAULT_SERIALIZER_CONTEXT = [AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true];
18
19
    public readonly array $validationGroups;
20
    public readonly array $serializerContext;
21
22
    public function __construct(
23
        public readonly ?string $requestClass = null,
24
        public readonly ?string $argumentName = null,
25
        public readonly bool $isValidationNeeded = true,
26
        array $serializerContext = [],
27
        array $validationGroups = [],
28
        bool $useDefaultValidationGroup = true,
29
        bool $useDefaultSerializerContext = true,
30
    ) {
31
        $this->validationGroups = $useDefaultValidationGroup ? [...$validationGroups, ...self::DEFAULT_VALIDATION_GROUPS] : $validationGroups;
0 ignored issues
show
The property validationGroups is declared read-only in FRZB\Component\RequestMapper\Attribute\RequestBody.
Loading history...
32
        $this->serializerContext = $useDefaultSerializerContext ? [...$serializerContext, ...self::DEFAULT_SERIALIZER_CONTEXT] : $serializerContext;
0 ignored issues
show
The property serializerContext is declared read-only in FRZB\Component\RequestMapper\Attribute\RequestBody.
Loading history...
33
    }
34
35
    public function equals(object $object): bool
36
    {
37
        return match (true) {
38
            $object instanceof \ReflectionParameter => PropertyHelper::getTypeName($object) === $this->requestClass && $object->getName() === $this->argumentName,
39
            $object instanceof self => $object->requestClass === $this->requestClass && $object->argumentName === $this->argumentName,
40
            default => false
41
        };
42
    }
43
}
44