Parameter.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. namespace Doctrine\ORM\Query;
  20. use function trim;
  21. /**
  22. * Defines a Query Parameter.
  23. *
  24. * @link www.doctrine-project.org
  25. */
  26. class Parameter
  27. {
  28. /**
  29. * Returns the internal representation of a parameter name.
  30. *
  31. * @param string|int $name The parameter name or position.
  32. *
  33. * @return string The normalized parameter name.
  34. */
  35. public static function normalizeName($name)
  36. {
  37. return trim((string) $name, ':');
  38. }
  39. /**
  40. * The parameter name.
  41. *
  42. * @var string
  43. */
  44. private $name;
  45. /**
  46. * The parameter value.
  47. *
  48. * @var mixed
  49. */
  50. private $value;
  51. /**
  52. * The parameter type.
  53. *
  54. * @var mixed
  55. */
  56. private $type;
  57. /**
  58. * Whether the parameter type was explicitly specified or not
  59. *
  60. * @var bool
  61. */
  62. private $typeSpecified;
  63. /**
  64. * @param string $name Parameter name
  65. * @param mixed $value Parameter value
  66. * @param mixed $type Parameter type
  67. */
  68. public function __construct($name, $value, $type = null)
  69. {
  70. $this->name = self::normalizeName($name);
  71. $this->typeSpecified = $type !== null;
  72. $this->setValue($value, $type);
  73. }
  74. /**
  75. * Retrieves the Parameter name.
  76. *
  77. * @return string
  78. */
  79. public function getName()
  80. {
  81. return $this->name;
  82. }
  83. /**
  84. * Retrieves the Parameter value.
  85. *
  86. * @return mixed
  87. */
  88. public function getValue()
  89. {
  90. return $this->value;
  91. }
  92. /**
  93. * Retrieves the Parameter type.
  94. *
  95. * @return mixed
  96. */
  97. public function getType()
  98. {
  99. return $this->type;
  100. }
  101. /**
  102. * Defines the Parameter value.
  103. *
  104. * @param mixed $value Parameter value.
  105. * @param mixed $type Parameter type.
  106. */
  107. public function setValue($value, $type = null)
  108. {
  109. $this->value = $value;
  110. $this->type = $type ?: ParameterTypeInferer::inferType($value);
  111. }
  112. public function typeWasSpecified(): bool
  113. {
  114. return $this->typeSpecified;
  115. }
  116. }