Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Minor Builder cleanup #13884

Merged
merged 1 commit into from
Jun 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ public function orWhereHas($relation, Closure $callback, $operator = '>=', $coun
* @param string $operator
* @param int $count
* @param string $boolean
* @return \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder|static
*/
protected function addHasWhere(Builder $hasQuery, Relation $relation, $operator, $count, $boolean)
{
Expand Down Expand Up @@ -983,7 +983,7 @@ protected function whereCountQuery(QueryBuilder $query, $operator = '>=', $count
* Merge the constraints from a relation query to the current query.
*
* @param \Illuminate\Database\Eloquent\Builder $relation
* @return void
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function mergeModelDefinedRelationConstraints(Builder $relation)
{
Expand Down Expand Up @@ -1194,10 +1194,10 @@ protected function shouldNestWheresForScope(QueryBuilder $query, $originalWhereC
* Nest where conditions by slicing them at the given where count.
*
* @param \Illuminate\Database\Query\Builder $query
* @param int|array $whereCounts
* @param int $originalWhereCount
* @return void
*/
protected function nestWheresForScope(QueryBuilder $query, $whereCounts)
protected function nestWheresForScope(QueryBuilder $query, $originalWhereCount)
{
// Here, we totally remove all of the where clauses since we are going to
// rebuild them as nested queries by slicing the groups of wheres into
Expand All @@ -1206,34 +1206,22 @@ protected function nestWheresForScope(QueryBuilder $query, $whereCounts)

$query->wheres = [];

// We will construct where offsets by adding the outer most offsets to the
// collection (0 and total where count) while also flattening the array
// and extracting unique values, ensuring that all wheres are sliced.
$whereOffsets = collect([0, $whereCounts, count($allWheres)])->flatten()->unique();

$sliceFrom = $whereOffsets->shift();

foreach ($whereOffsets as $sliceTo) {
$this->sliceWhereConditions(
$query, $allWheres, $sliceFrom, $sliceTo
);

$sliceFrom = $sliceTo;
}
$this->sliceWhereConditions($query, $allWheres, 0, $originalWhereCount);
$this->sliceWhereConditions($query, $allWheres, $originalWhereCount);
}

/**
* Create a slice of where conditions at the given offsets and nest them if needed.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $wheres
* @param int $sliceFrom
* @param int $sliceTo
* @param int $offset
* @param int $length
* @return void
*/
protected function sliceWhereConditions(QueryBuilder $query, array $wheres, $sliceFrom, $sliceTo)
protected function sliceWhereConditions(QueryBuilder $query, $wheres, $offset, $length = null)
{
$whereSlice = array_slice($wheres, $sliceFrom, $sliceTo - $sliceFrom);
$whereSlice = array_slice($wheres, $offset, $length);

$whereBooleans = collect($whereSlice)->pluck('boolean');

Expand Down