Skip to content

Commit

Permalink
Add native types to QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Feb 9, 2022
1 parent 3a43f92 commit 93c79c9
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 390 deletions.
77 changes: 15 additions & 62 deletions lib/Doctrine/ORM/Query/Expr/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,103 +20,56 @@ class Join
public const WITH = 'WITH';

/**
* @var string
* @psalm-var self::INNER_JOIN|self::LEFT_JOIN
*/
protected $joinType;

/** @var string */
protected $join;

/** @var string|null */
protected $alias;

/**
* @var string|null
* @psalm-var self::ON|self::WITH|null
*/
protected $conditionType;

/** @var string|Comparison|Composite|null */
protected $condition;

/** @var string|null */
protected $indexBy;

/**
* @param string $joinType The condition type constant. Either INNER_JOIN or LEFT_JOIN.
* @param string $join The relationship to join.
* @param string|null $alias The alias of the join.
* @param string|null $conditionType The condition type constant. Either ON or WITH.
* @param string|Comparison|Composite|null $condition The condition for the join.
* @param string|null $indexBy The index for the join.
* @psalm-param self::INNER_JOIN|self::LEFT_JOIN $joinType
* @psalm-param self::ON|self::WITH|null $conditionType
*/
public function __construct($joinType, $join, $alias = null, $conditionType = null, $condition = null, $indexBy = null)
{
$this->joinType = $joinType;
$this->join = $join;
$this->alias = $alias;
$this->conditionType = $conditionType;
$this->condition = $condition;
$this->indexBy = $indexBy;
public function __construct(
protected string $joinType,
protected string $join,
protected ?string $alias = null,
protected ?string $conditionType = null,
protected string|Comparison|Composite|null $condition = null,
protected ?string $indexBy = null
) {
}

/**
* @return string
* @psalm-return self::INNER_JOIN|self::LEFT_JOIN
*/
public function getJoinType()
public function getJoinType(): string
{
return $this->joinType;
}

/**
* @return string
*/
public function getJoin()
public function getJoin(): string
{
return $this->join;
}

/**
* @return string|null
*/
public function getAlias()
public function getAlias(): ?string
{
return $this->alias;
}

/**
* @return string|null
* @psalm-return self::ON|self::WITH|null
*/
public function getConditionType()
public function getConditionType(): ?string
{
return $this->conditionType;
}

/**
* @return string|Comparison|Composite|null
*/
public function getCondition()
public function getCondition(): string|Comparison|Composite|null
{
return $this->condition;
}

/**
* @return string|null
*/
public function getIndexBy()
public function getIndexBy(): ?string
{
return $this->indexBy;
}

/**
* @return string
*/
public function __toString()
public function __toString(): string
{
return strtoupper($this->joinType) . ' JOIN ' . $this->join
. ($this->alias ? ' ' . $this->alias : '')
Expand Down
48 changes: 10 additions & 38 deletions lib/Doctrine/ORM/Query/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,33 @@ class Parameter
{
/**
* Returns the internal representation of a parameter name.
*
* @param string|int $name The parameter name or position.
*
* @return string The normalized parameter name.
*/
public static function normalizeName($name)
public static function normalizeName(int|string $name): string
{
return trim((string) $name, ':');
}

/**
* The parameter name.
*
* @var string
*/
private $name;
private string $name;

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

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

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

/**
* @param string|int $name Parameter name
* @param mixed $value Parameter value
* @param mixed $type Parameter type
*/
public function __construct($name, $value, $type = null)
public function __construct(int|string $name, mixed $value, mixed $type = null)
{
$this->name = self::normalizeName($name);
$this->typeSpecified = $type !== null;
Expand All @@ -68,43 +51,32 @@ public function __construct($name, $value, $type = null)

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

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

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

/**
* Defines the Parameter value.
*
* @param mixed $value Parameter value.
* @param mixed $type Parameter type.
*
* @return void
*/
public function setValue($value, $type = null)
public function setValue(mixed $value, mixed $type = null): void
{
$this->value = $value;
$this->type = $type ?: ParameterTypeInferer::inferType($value);
Expand Down
12 changes: 6 additions & 6 deletions lib/Doctrine/ORM/Query/ParameterTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@
*
* @link www.doctrine-project.org
*/
class ParameterTypeInferer
final class ParameterTypeInferer
{
/**
* Infers type of a given value, returning a compatible constant:
* - Type (\Doctrine\DBAL\Types\Type::*)
* - Connection (\Doctrine\DBAL\Connection::PARAM_*)
*
* @param mixed $value Parameter value.
*
* @return int|string Parameter type constant.
*/
public static function inferType($value)
public static function inferType(mixed $value): int|string
{
if (is_int($value)) {
return Types::INTEGER;
Expand Down Expand Up @@ -74,4 +70,8 @@ public static function inferType($value)

return ParameterType::STRING;
}

private function __construct()
{
}
}
Loading

0 comments on commit 93c79c9

Please sign in to comment.