Skip to content

Commit 5a66b2f

Browse files
committed
Add Collection::select
1 parent 4c7d10a commit 5a66b2f

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

src/Collection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,20 @@ public function column($key)
200200
yield Util\Arrays::get($item, $key);
201201
}
202202
}
203+
204+
/**
205+
* Return an iterable generator containing only the fields specified in the $keys array.
206+
*
207+
* @param array $keys The list of field names to be returned.
208+
*
209+
* @return \Generator
210+
*/
211+
public function select(array $keys)
212+
{
213+
foreach ($this as $item) {
214+
$result = array_fill_keys($keys, null);
215+
Util\Arrays::copyIfKeysExist($item, $result, $keys);
216+
yield $result;
217+
}
218+
}
203219
}

tests/CollectionTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,93 @@ public function column()
259259
iterator_to_array($collection->column('key'))
260260
);
261261
}
262+
263+
/**
264+
* Verifies basic behavior of select().
265+
*
266+
* @test
267+
* @covers ::select
268+
*
269+
* @return void
270+
*/
271+
public function select()
272+
{
273+
$authentication = Authentication::createClientCredentials('not under test', 'not under test');
274+
$client = new Client(new CollectionAdapter(), $authentication, 'not under test');
275+
$collection = new Collection($client, 'basic', ['limit' => 3]);
276+
$this->assertSame(
277+
[
278+
['key' => 0],
279+
['key' => 1],
280+
['key' => 2],
281+
['key' => 3],
282+
['key' => 4],
283+
],
284+
iterator_to_array($collection->select(['key']))
285+
);
286+
}
287+
288+
/**
289+
* Verifies behavior of select() with multiple keys.
290+
*
291+
* @test
292+
* @covers ::select
293+
*
294+
* @return void
295+
*/
296+
public function selectMultipleKeys()
297+
{
298+
$adapter = new CollectionAdapter();
299+
$adapter->results = [
300+
['id' => 1, 'name' => 'Sam', 'score' => 99],
301+
['id' => 2, 'name' => 'Bob', 'score' => 83],
302+
['id' => 3, 'name' => 'Jon', 'score' => 75],
303+
['id' => 4, 'name' => 'Ted', 'score' => 64],
304+
];
305+
$authentication = Authentication::createClientCredentials('not under test', 'not under test');
306+
$client = new Client($adapter, $authentication, 'not under test');
307+
$collection = new Collection($client, 'basic', ['limit' => 3]);
308+
$this->assertSame(
309+
[
310+
['id' => 1, 'score' => 99],
311+
['id' => 2, 'score' => 83],
312+
['id' => 3, 'score' => 75],
313+
['id' => 4, 'score' => 64],
314+
],
315+
iterator_to_array($collection->select(['id', 'score']))
316+
);
317+
}
318+
319+
/**
320+
* Verifies behavior of select() when results have missing keys.
321+
*
322+
* @test
323+
* @covers ::select
324+
*
325+
* @return void
326+
*/
327+
public function selectMissingKeys()
328+
{
329+
$adapter = new CollectionAdapter();
330+
$adapter->results = [
331+
['id' => 1, 'name' => 'Sam', 'score' => 99],
332+
['id' => 2, 'name' => 'Bob'],
333+
['id' => 3, 'name' => 'Jon', 'score' => 75],
334+
['id' => 4, 'score' => 64],
335+
];
336+
$authentication = Authentication::createClientCredentials('not under test', 'not under test');
337+
$client = new Client($adapter, $authentication, 'not under test');
338+
$collection = new Collection($client, 'basic', ['limit' => 3]);
339+
$this->assertSame(
340+
[
341+
['name' => 'Sam', 'score' => 99],
342+
['name' => 'Bob', 'score' => null],
343+
['name' => 'Jon', 'score' => 75],
344+
['name' => null, 'score' => 64],
345+
],
346+
iterator_to_array($collection->select(['name', 'score']))
347+
);
348+
}
262349
}
263350

264351
final class CollectionAdapter implements Adapter

0 commit comments

Comments
 (0)