This commit is contained in:
Yuri Kuznetsov
2022-11-14 13:07:44 +02:00
parent 6f6c08f9a3
commit 550bdaba4f
5 changed files with 60 additions and 106 deletions

View File

@@ -49,7 +49,7 @@ class AndGroupBuilder
$value = $item->getRawValue();
if ($item instanceof AndGroup) {
$this->raw = self::normilizeRaw($this->raw);
$this->raw = self::normalizeRaw($this->raw);
$this->raw[] = $item->getRawValue();
@@ -62,7 +62,7 @@ class AndGroupBuilder
return $this;
}
$this->raw = self::normilizeRaw($this->raw);
$this->raw = self::normalizeRaw($this->raw);
$this->raw[] = [$key => $value];
@@ -75,8 +75,8 @@ class AndGroupBuilder
public function merge(AndGroup $andGroup): self
{
$this->raw = array_merge(
self::normilizeRaw($this->raw),
self::normilizeRaw($andGroup->getRawValue())
self::normalizeRaw($this->raw),
self::normalizeRaw($andGroup->getRawValue())
);
return $this;
@@ -86,7 +86,7 @@ class AndGroupBuilder
* @param array<mixed,mixed> $raw
* @return array<mixed,mixed>
*/
private static function normilizeRaw(array $raw): array
private static function normalizeRaw(array $raw): array
{
if (count($raw) === 1 && array_keys($raw)[0] !== 0) {
return [$raw];

View File

@@ -49,7 +49,7 @@ class OrGroupBuilder
$value = $item->getRawValue();
if ($item instanceof AndGroup) {
$this->raw = self::normilizeRaw($this->raw);
$this->raw = self::normalizeRaw($this->raw);
$this->raw[] = $value;
@@ -62,7 +62,7 @@ class OrGroupBuilder
return $this;
}
$this->raw = self::normilizeRaw($this->raw);
$this->raw = self::normalizeRaw($this->raw);
$this->raw[] = [$key => $value];
@@ -75,8 +75,8 @@ class OrGroupBuilder
public function merge(OrGroup $orGroup): self
{
$this->raw = array_merge(
self::normilizeRaw($this->raw),
self::normilizeRaw($orGroup->getRawValue())
self::normalizeRaw($this->raw),
self::normalizeRaw($orGroup->getRawValue())
);
return $this;
@@ -86,7 +86,7 @@ class OrGroupBuilder
* @param array<mixed,mixed> $raw
* @return array<mixed,mixed>
*/
private static function normilizeRaw(array $raw): array
private static function normalizeRaw(array $raw): array
{
if (count($raw) === 1 && array_keys($raw)[0] !== 0) {
return [$raw];

View File

@@ -152,7 +152,7 @@ class SelectBuilder implements Builder
/** @phpstan-var mixed $select */
if (is_array($select)) {
$this->params['select'] = $this->normilizeSelectExpressionArray($select);
$this->params['select'] = $this->normalizeSelectExpressionArray($select);
return $this;
}
@@ -194,7 +194,7 @@ class SelectBuilder implements Builder
/** @phpstan-var mixed $groupBy */
if (is_array($groupBy)) {
$this->params['groupBy'] = $this->normilizeExpressionItemArray($groupBy);
$this->params['groupBy'] = $this->normalizeExpressionItemArray($groupBy);
return $this;
}
@@ -295,7 +295,7 @@ class SelectBuilder implements Builder
* @param array<Expression|Selection|mixed[]> $itemList
* @return array<array{0:string,1?:string}|string>
*/
private function normilizeSelectExpressionArray(array $itemList): array
private function normalizeSelectExpressionArray(array $itemList): array
{
$resultList = [];

View File

@@ -124,7 +124,7 @@ trait SelectingBuilderTrait
}
if (is_array($orderBy)) {
$this->params['orderBy'] = $this->normilizeOrderExpressionItemArray(
$this->params['orderBy'] = $this->normalizeOrderExpressionItemArray(
$orderBy,
$direction ?? Order::ASC
);
@@ -160,61 +160,12 @@ trait SelectingBuilderTrait
*
* @param Join|string $target
* A relation name or table. A relation name should be in camelCase, a table in CamelCase.
* @param string|null $alias An alias.
* @param WhereItem|array<mixed,mixed>|null $conditions Join conditions.
* @param ?string $alias An alias.
* @param WhereItem|array<mixed, mixed>|null $conditions Join conditions.
*/
public function join($target, ?string $alias = null, $conditions = null): self
{
if ($target instanceof Join) {
$alias = $alias ?? $target->getAlias();
$conditions = $conditions ?? $target->getConditions();
$target = $target->getTarget();
}
/** @phpstan-var mixed $conditions */
/** @phpstan-var mixed $target */
if ($conditions !== null && !is_array($conditions) && !$conditions instanceof WhereItem) {
throw new InvalidArgumentException("Conditions must be WhereItem or array.");
}
if ($conditions instanceof WhereItem) {
$conditions = $conditions->getRaw();
}
if (empty($this->params['joins'])) {
$this->params['joins'] = [];
}
if (is_array($target)) {
$joinList = $target;
foreach ($joinList as $item) {
$this->params['joins'][] = $item;
}
return $this;
}
if (is_null($alias) && is_null($conditions) && $this->hasJoinAlias($target)) {
return $this;
}
if (is_null($alias) && is_null($conditions)) {
$this->params['joins'][] = $target;
return $this;
}
if (is_null($conditions)) {
$this->params['joins'][] = [$target, $alias];
return $this;
}
$this->params['joins'][] = [$target, $alias, $conditions];
return $this;
return $this->joinInternal('joins', $target, $alias, $conditions);
}
/**
@@ -222,10 +173,22 @@ trait SelectingBuilderTrait
*
* @param Join|string $target
* A relation name or table. A relation name should be in camelCase, a table in CamelCase.
* @param ?string $alias An alias.
* @param WhereItem|array<mixed, mixed>|null $conditions Join conditions.
*/
public function leftJoin($target, ?string $alias = null, $conditions = null): self
{
return $this->joinInternal('leftJoins', $target, $alias, $conditions);
}
/**
* @param 'leftJoins'|'joins' $type
* @param Join|string $target
* A relation name or table. A relation name should be in camelCase, a table in CamelCase.
* @param string|null $alias An alias.
* @param WhereItem|array<mixed,mixed>|null $conditions Join conditions.
*/
public function leftJoin($target, ?string $alias = null, $conditions = null): self
private function joinInternal(string $type, $target, ?string $alias = null, $conditions = null): self
{
if ($target instanceof Join) {
$alias = $alias ?? $target->getAlias();
@@ -244,69 +207,44 @@ trait SelectingBuilderTrait
$conditions = $conditions->getRaw();
}
if (empty($this->params['leftJoins'])) {
$this->params['leftJoins'] = [];
if (empty($this->params[$type])) {
$this->params[$type] = [];
}
if (is_array($target)) {
$joinList = $target;
foreach ($joinList as $item) {
$this->params['leftJoins'][] = $item;
$this->params[$type][] = $item;
}
return $this;
}
if (is_null($alias) && is_null($conditions) && $this->hasLeftJoinAlias($target)) {
if (is_null($alias) && is_null($conditions) && $this->hasJoinAliasInternal($type, $target)) {
return $this;
}
if (is_null($alias) && is_null($conditions)) {
$this->params['leftJoins'][] = $target;
$this->params[$type][] = $target;
return $this;
}
if (is_null($conditions)) {
$this->params['leftJoins'][] = [$target, $alias];
$this->params[$type][] = [$target, $alias];
return $this;
}
$this->params['leftJoins'][] = [$target, $alias, $conditions];
$this->params[$type][] = [$target, $alias, $conditions];
return $this;
}
/**
* Whether an alias is in left joins.
*/
public function hasLeftJoinAlias(string $alias): bool
private function hasJoinAliasInternal(string $type, string $alias): bool
{
$leftJoins = $this->params['leftJoins'] ?? [];
if (in_array($alias, $leftJoins)) {
return true;
}
foreach ($leftJoins as $item) {
if (is_array($item) && count($item) > 1) {
if ($item[1] === $alias) {
return true;
}
}
}
return false;
}
/**
* Whether an alias is in joins.
*/
public function hasJoinAlias(string $alias): bool
{
$joins = $this->params['joins'] ?? [];
$joins = $this->params[$type] ?? [];
if (in_array($alias, $joins)) {
return true;
@@ -323,11 +261,27 @@ trait SelectingBuilderTrait
return false;
}
/**
* Whether an alias is in left joins.
*/
public function hasLeftJoinAlias(string $alias): bool
{
return $this->hasJoinAliasInternal('leftJoins', $alias);
}
/**
* Whether an alias is in joins.
*/
public function hasJoinAlias(string $alias): bool
{
return $this->hasJoinAliasInternal('joins', $alias);
}
/**
* @param array<Expression|mixed[]> $itemList
* @return array<array{0:string,1?:string}|string>
*/
private function normilizeExpressionItemArray(array $itemList): array
private function normalizeExpressionItemArray(array $itemList): array
{
$resultList = [];
@@ -364,9 +318,9 @@ trait SelectingBuilderTrait
/**
* @param array<Order|mixed[]|string> $itemList
* @param string|bool|null $direction
* @return array<array{string,string|bool}>
* @return array<array{string, string|bool}>
*/
private function normilizeOrderExpressionItemArray(array $itemList, $direction): array
private function normalizeOrderExpressionItemArray(array $itemList, $direction): array
{
$resultList = [];

View File

@@ -371,7 +371,7 @@ abstract class BaseQueryComposer implements QueryComposer
*/
protected function createInsertQuery(?array $params): string
{
$params = $this->normilizeInsertParams($params ?? []);
$params = $this->normalizeInsertParams($params ?? []);
$entityType = $params['into'];
@@ -498,7 +498,7 @@ abstract class BaseQueryComposer implements QueryComposer
* @param array<string,mixed> $params
* @return array<string,mixed>
*/
protected function normilizeInsertParams(array $params): array
protected function normalizeInsertParams(array $params): array
{
$columns = $params['columns'] ?? null;