1<?php
2/*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\ORM\Query;
21
22use function trim;
23
24/**
25 * Defines a Query Parameter.
26 *
27 * @link    www.doctrine-project.org
28 * @since   2.3
29 * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
30 */
31class Parameter
32{
33    /**
34     * Returns the internal representation of a parameter name.
35     *
36     * @param string|int $name The parameter name or position.
37     *
38     * @return string The normalized parameter name.
39     */
40    public static function normalizeName($name)
41    {
42        return trim((string) $name, ':');
43    }
44
45    /**
46     * The parameter name.
47     *
48     * @var string
49     */
50    private $name;
51
52    /**
53     * The parameter value.
54     *
55     * @var mixed
56     */
57    private $value;
58
59    /**
60     * The parameter type.
61     *
62     * @var mixed
63     */
64    private $type;
65
66    /**
67     * Whether the parameter type was explicitly specified or not
68     *
69     * @var bool
70     */
71    private $typeSpecified;
72
73    /**
74     * Constructor.
75     *
76     * @param string $name  Parameter name
77     * @param mixed  $value Parameter value
78     * @param mixed  $type  Parameter type
79     */
80    public function __construct($name, $value, $type = null)
81    {
82        $this->name          = self::normalizeName($name);
83        $this->typeSpecified = $type !== null;
84
85        $this->setValue($value, $type);
86    }
87
88    /**
89     * Retrieves the Parameter name.
90     *
91     * @return string
92     */
93    public function getName()
94    {
95        return $this->name;
96    }
97
98    /**
99     * Retrieves the Parameter value.
100     *
101     * @return mixed
102     */
103    public function getValue()
104    {
105        return $this->value;
106    }
107
108    /**
109     * Retrieves the Parameter type.
110     *
111     * @return mixed
112     */
113    public function getType()
114    {
115        return $this->type;
116    }
117
118    /**
119     * Defines the Parameter value.
120     *
121     * @param mixed $value Parameter value.
122     * @param mixed $type  Parameter type.
123     */
124    public function setValue($value, $type = null)
125    {
126        $this->value = $value;
127        $this->type  = $type ?: ParameterTypeInferer::inferType($value);
128    }
129
130    public function typeWasSpecified() : bool
131    {
132        return $this->typeSpecified;
133    }
134}
135