Skip to content

Commit 185d9bb

Browse files
authored
Let toPrettyJson() accepts options (#56876)
1 parent 2b85513 commit 185d9bb

File tree

14 files changed

+65
-21
lines changed

14 files changed

+65
-21
lines changed

src/Illuminate/Collections/Enumerable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,9 +1272,11 @@ public function toJson($options = 0);
12721272
/**
12731273
* Get the collection of items as pretty print formatted JSON.
12741274
*
1275+
*
1276+
* @param int $options
12751277
* @return string
12761278
*/
1277-
public function toPrettyJson();
1279+
public function toPrettyJson(int $options = 0);
12781280

12791281
/**
12801282
* Get a CachingIterator instance.

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,11 +987,12 @@ public function toJson($options = 0)
987987
/**
988988
* Get the collection of items as pretty print formatted JSON.
989989
*
990+
* @param int $options
990991
* @return string
991992
*/
992-
public function toPrettyJson()
993+
public function toPrettyJson(int $options = 0)
993994
{
994-
return $this->toJson(JSON_PRETTY_PRINT);
995+
return $this->toJson(JSON_PRETTY_PRINT | $options);
995996
}
996997

997998
/**

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,13 +1803,14 @@ public function toJson($options = 0)
18031803
/**
18041804
* Convert the model instance to pretty print formatted JSON.
18051805
*
1806+
* @param int $options
18061807
* @return string
18071808
*
18081809
* @throws \Illuminate\Database\Eloquent\JsonEncodingException
18091810
*/
1810-
public function toPrettyJson()
1811+
public function toPrettyJson(int $options = 0)
18111812
{
1812-
return $this->toJson(JSON_PRETTY_PRINT);
1813+
return $this->toJson(JSON_PRETTY_PRINT | $options);
18131814
}
18141815

18151816
/**

src/Illuminate/Http/Resources/Json/JsonResource.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,14 @@ public function toJson($options = 0)
163163
/**
164164
* Convert the resource to pretty print formatted JSON.
165165
*
166+
* @param int $options
166167
* @return string
167168
*
168169
* @throws \Illuminate\Database\Eloquent\JsonEncodingException
169170
*/
170-
public function toPrettyJson()
171+
public function toPrettyJson(int $options = 0)
171172
{
172-
return $this->toJson(JSON_PRETTY_PRINT);
173+
return $this->toJson(JSON_PRETTY_PRINT | $options);
173174
}
174175

175176
/**

src/Illuminate/Pagination/CursorPaginator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,12 @@ public function toJson($options = 0)
184184
/**
185185
* Convert the object to pretty print formatted JSON.
186186
*
187+
* @params int $options
188+
*
187189
* @return string
188190
*/
189-
public function toPrettyJson()
191+
public function toPrettyJson(int $options = 0)
190192
{
191-
return $this->toJson(JSON_PRETTY_PRINT);
193+
return $this->toJson(JSON_PRETTY_PRINT | $options);
192194
}
193195
}

src/Illuminate/Pagination/LengthAwarePaginator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,12 @@ public function toJson($options = 0)
246246
/**
247247
* Convert the object to pretty print formatted JSON.
248248
*
249+
* @params int $options
250+
*
249251
* @return string
250252
*/
251-
public function toPrettyJson()
253+
public function toPrettyJson(int $options = 0)
252254
{
253-
return $this->toJson(JSON_PRETTY_PRINT);
255+
return $this->toJson(JSON_PRETTY_PRINT | $options);
254256
}
255257
}

src/Illuminate/Pagination/Paginator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,12 @@ public function toJson($options = 0)
189189
/**
190190
* Convert the object to pretty print formatted JSON.
191191
*
192+
* @params int $options
193+
*
192194
* @return string
193195
*/
194-
public function toPrettyJson()
196+
public function toPrettyJson(int $options = 0)
195197
{
196-
return $this->toJson(JSON_PRETTY_PRINT);
198+
return $this->toJson(JSON_PRETTY_PRINT | $options);
197199
}
198200
}

src/Illuminate/Support/Fluent.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,13 @@ public function toJson($options = 0)
206206
/**
207207
* Convert the fluent instance to pretty print formatted JSON.
208208
*
209+
* @params int $options
210+
*
209211
* @return string
210212
*/
211-
public function toPrettyJson()
213+
public function toPrettyJson(int $options = 0)
212214
{
213-
return $this->toJson(JSON_PRETTY_PRINT);
215+
return $this->toJson(JSON_PRETTY_PRINT | $options);
214216
}
215217

216218
/**

src/Illuminate/Support/MessageBag.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,13 @@ public function toJson($options = 0)
434434
/**
435435
* Convert the object to pretty print formatted JSON.
436436
*
437+
* @params int $options
438+
*
437439
* @return string
438440
*/
439-
public function toPrettyJson()
441+
public function toPrettyJson(int $options = 0)
440442
{
441-
return $this->toJson(JSON_PRETTY_PRINT);
443+
return $this->toJson(JSON_PRETTY_PRINT | $options);
442444
}
443445

444446
/**

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3448,14 +3448,19 @@ public function testModelToJsonSucceedsWithPriorErrors(): void
34483448

34493449
public function testModelToPrettyJson(): void
34503450
{
3451-
$user = new EloquentModelStub(['name' => 'Mateus', 'active' => true]);
3451+
$user = new EloquentModelStub(['name' => 'Mateus', 'active' => true, 'number' => '123']);
34523452
$results = $user->toPrettyJson();
34533453
$expected = $user->toJson(JSON_PRETTY_PRINT);
34543454

34553455
$this->assertJsonStringEqualsJsonString($expected, $results);
34563456
$this->assertSame($expected, $results);
34573457
$this->assertStringContainsString("\n", $results);
34583458
$this->assertStringContainsString(' ', $results);
3459+
3460+
$results = $user->toPrettyJson(JSON_NUMERIC_CHECK);
3461+
$this->assertStringContainsString("\n", $results);
3462+
$this->assertStringContainsString(' ', $results);
3463+
$this->assertStringContainsString('"number": 123', $results);
34593464
}
34603465

34613466
public function testFillableWithMutators()

0 commit comments

Comments
 (0)