Skip to content

Commit bff6262

Browse files
committed
Add toJson() method to Collection
1 parent 0c7a73b commit bff6262

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Collection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ public function toArray(): array
212212
return $this->items;
213213
}
214214

215+
/**
216+
* Get collection as JSON.
217+
*
218+
* @return string
219+
*/
220+
public function toJson(): string
221+
{
222+
return json_encode($this);
223+
}
224+
215225
/**
216226
* Flatten collection with dot (.) separator.
217227
*

tests/CollectionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ public function testToArray($key, $value)
124124
$this->assertEquals($values, array_values($arr));
125125
}
126126

127+
/**
128+
* @covers Collection::toJson
129+
* @dataProvider collectionItemsProvider
130+
*/
131+
public function testToJson($key, $value)
132+
{
133+
$this->collection->set($key, $value);
134+
$json = json_encode($this->collection);
135+
136+
$this->assertEquals($json, $this->collection->toJson());
137+
}
138+
127139
/**
128140
* @covers Collection::is
129141
* @dataProvider isCollectionItemsProvider

0 commit comments

Comments
 (0)