Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 24, 2021
1 parent 9ae83ff commit 31ebfc8
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/Illuminate/Pagination/AbstractCursorPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,30 +196,32 @@ public function getParametersForItem($item)
->flip()
->map(function ($_, $parameterName) use ($item) {
if ($item instanceof ArrayAccess || is_array($item)) {
return $this->castParameter($item[$parameterName] ?? $item[Str::afterLast($parameterName, '.')]);
return $this->ensureParameterIsPrimitive(
$item[$parameterName] ?? $item[Str::afterLast($parameterName, '.')]
);
} elseif (is_object($item)) {
return $this->castParameter($item->{$parameterName} ?? $item->{Str::afterLast($parameterName, '.')});
return $this->ensureParameterIsPrimitive(
$item->{$parameterName} ?? $item->{Str::afterLast($parameterName, '.')}
);
}

throw new Exception('Only arrays and objects are supported when cursor paginating items.');
})->toArray();
}

/**
* Casts the given item parameter. When the given parameter is an object and can
* be cast to a string, the stringified representation will be returned,
* otherwise the original parameter will be returned.
* Ensure the parameter is a primitive type.
*
* This can resolve issues that arise the developer uses a value object for an attribute.
*
* @param mixed $parameter
* @return string
* @return mixed
*/
public function castParameter($parameter)
protected function ensureParameterIsPrimitive($parameter)
{
if (is_object($parameter) && method_exists($parameter, '__toString')) {
return (string) $parameter;
}

return $parameter;
return is_object($parameter) && method_exists($parameter, '__toString')
? (string) $parameter
: $parameter;
}

/**
Expand Down

0 comments on commit 31ebfc8

Please sign in to comment.