Skip to content
Prev Previous commit
Next Next commit
Add toPrettyJson to CursorPaginator, LengthAwarePaginator and Paginator
  • Loading branch information
WendellAdriel committed Aug 21, 2025
commit be8f53cd6dcc7ccb572da569ff783ef1f32ae822
10 changes: 10 additions & 0 deletions src/Illuminate/Pagination/CursorPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,14 @@ public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}

/**
* Convert the object to a pretty print formatted JSON.
*
* @return string
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}
}
10 changes: 10 additions & 0 deletions src/Illuminate/Pagination/LengthAwarePaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,14 @@ public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}

/**
* Convert the object to a pretty print formatted JSON.
*
* @return string
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}
}
10 changes: 10 additions & 0 deletions src/Illuminate/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,14 @@ public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}

/**
* Convert the object to a pretty print formatted JSON.
*
* @return string
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}
}
22 changes: 22 additions & 0 deletions tests/Pagination/CursorPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ public function testReturnEmptyCursorWhenItemsAreEmpty()
], $p->toArray());
}

public function testCursorPaginatorToJson()
{
$paginator = new CursorPaginator([['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4]], 2, null);
$results = $paginator->toJson();
$expected = json_encode($paginator->toArray());

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
}

public function testCursorPaginatorToPrettyJson()
{
$paginator = new CursorPaginator([['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4]], 2, null);
$results = $paginator->toPrettyJson();
$expected = $paginator->toJson(JSON_PRETTY_PRINT);

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
$this->assertStringContainsString("\n", $results);
$this->assertStringContainsString(' ', $results);
}

protected function getCursor($params, $isNext = true)
{
return (new Cursor($params, $isNext))->encode();
Expand Down
22 changes: 22 additions & 0 deletions tests/Pagination/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,26 @@ public function testCanTransformPaginatorItems()
$this->assertInstanceOf(Paginator::class, $p);
$this->assertSame(['1', '2', '3'], $p->items());
}

public function testPaginatorToJson()
{
$p = new Paginator(['item1', 'item2', 'item3'], 3, 1);
$results = $p->toJson();
$expected = json_encode($p->toArray());

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
}

public function testPaginatorToPrettyJson()
{
$p = new Paginator(['item1', 'item2', 'item3'], 3, 1);
$results = $p->toPrettyJson();
$expected = $p->toJson(JSON_PRETTY_PRINT);

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
$this->assertStringContainsString("\n", $results);
$this->assertStringContainsString(' ', $results);
}
}