Skip to content

Commit 60cfc71

Browse files
authored
Merge pull request #35 from chadicus/fea/array-column
Add Collection::column()
2 parents 101b33f + 589c3e1 commit 60cfc71

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/Collection.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,18 @@ public function current()
186186

187187
return $this->_result[$this->_position];
188188
}
189+
190+
/**
191+
* Returns the values from a single field this collection, identified by the given $key.
192+
*
193+
* @param string $key The name of the field for which the values will be returned.
194+
*
195+
* @return iterable
196+
*/
197+
public function column($key)
198+
{
199+
foreach ($this as $item) {
200+
yield Util\Arrays::get($item, $key);
201+
}
202+
}
189203
}

tests/CollectionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,23 @@ public function oneItemCollection()
242242
$this->assertSame(['id' => '0', 'key' => 0], $item);
243243
}
244244
}
245+
246+
/**
247+
* Verifies basic behavior of column().
248+
*
249+
* @test
250+
* @covers ::column
251+
*/
252+
public function column()
253+
{
254+
$authentication = Authentication::createClientCredentials('not under test', 'not under test');
255+
$client = new Client(new CollectionAdapter(), $authentication, 'not under test');
256+
$collection = new Collection($client, 'basic', ['limit' => 3]);
257+
$this->assertSame(
258+
[0, 1, 2, 3, 4],
259+
iterator_to_array($collection->column('key'))
260+
);
261+
}
245262
}
246263

247264
final class CollectionAdapter implements Adapter

0 commit comments

Comments
 (0)