summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Query/Parameter.php
blob: 43eb7a41239c8c07cdbbf7c9b3818fc06d7c40f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Query;

use function trim;

/**
 * Defines a Query Parameter.
 *
 * @link    www.doctrine-project.org
 */
class Parameter
{
    /**
     * Returns the internal representation of a parameter name.
     */
    public static function normalizeName(int|string $name): string
    {
        return trim((string) $name, ':');
    }

    /**
     * The parameter name.
     */
    private readonly string $name;

    /**
     * The parameter value.
     */
    private mixed $value;

    /**
     * The parameter type.
     */
    private mixed $type;

    /**
     * Whether the parameter type was explicitly specified or not
     */
    private readonly bool $typeSpecified;

    public function __construct(int|string $name, mixed $value, mixed $type = null)
    {
        $this->name          = self::normalizeName($name);
        $this->typeSpecified = $type !== null;

        $this->setValue($value, $type);
    }

    /**
     * Retrieves the Parameter name.
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * Retrieves the Parameter value.
     */
    public function getValue(): mixed
    {
        return $this->value;
    }

    /**
     * Retrieves the Parameter type.
     */
    public function getType(): mixed
    {
        return $this->type;
    }

    /**
     * Defines the Parameter value.
     */
    public function setValue(mixed $value, mixed $type = null): void
    {
        $this->value = $value;
        $this->type  = $type ?: ParameterTypeInferer::inferType($value);
    }

    public function typeWasSpecified(): bool
    {
        return $this->typeSpecified;
    }
}