Skip to content

Commit 1c22b6d

Browse files
committed
added |column filter
1 parent f4c00ae commit 1c22b6d

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

‎src/Latte/Essential/CoreExtension.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public function getFilters(): array
125125
'ceil' => $this->filters->ceil(...),
126126
'checkUrl' => $this->filters->checkUrl(...),
127127
'clamp' => $this->filters->clamp(...),
128+
'column' => $this->filters->column(...),
128129
'dataStream' => $this->filters->dataStream(...),
129130
'datastream' => $this->filters->dataStream(...),
130131
'date' => $this->filters->date(...),

‎src/Latte/Essential/Filters.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,15 @@ public static function reverse(string|iterable $val, bool $preserveKeys = false)
500500
}
501501

502502

503+
/**
504+
* Returns the values from a single column in the input array.
505+
*/
506+
public static function column(iterable $data, string|int|null $columnKey, string|int|null $indexKey = null): array
507+
{
508+
return array_column(iterator_to_array($data), $columnKey, $indexKey);
509+
}
510+
511+
503512
/**
504513
* Chunks items by returning an array of arrays with the given number of items.
505514
*/

‎tests/filters/column.phpt‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* Test: Latte\Essential\Filters::column()
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Latte\Essential\Filters;
10+
use Tester\Assert;
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
$data = [
16+
['id' => 1, 'name' => 'John', 'age' => 30],
17+
['id' => 2, 'name' => 'Jane', 'age' => 25],
18+
['id' => 3, 'name' => 'Bob', 'age' => 35],
19+
];
20+
21+
test('extracts single column', function () use ($data) {
22+
Assert::same(['John', 'Jane', 'Bob'], Filters::column($data, 'name'));
23+
Assert::same([1, 2, 3], Filters::column($data, 'id'));
24+
Assert::same([30, 25, 35], Filters::column($data, 'age'));
25+
});
26+
27+
28+
test('extracts column with custom index', function () use ($data) {
29+
Assert::same([1 => 'John', 2 => 'Jane', 3 => 'Bob'], Filters::column($data, 'name', 'id'));
30+
Assert::same(['John' => 30, 'Jane' => 25, 'Bob' => 35], Filters::column($data, 'age', 'name'));
31+
});
32+
33+
34+
test('extracts all values when column is null', function () use ($data) {
35+
Assert::same([
36+
['id' => 1, 'name' => 'John', 'age' => 30],
37+
['id' => 2, 'name' => 'Jane', 'age' => 25],
38+
['id' => 3, 'name' => 'Bob', 'age' => 35],
39+
], Filters::column($data, null));
40+
});
41+
42+
43+
test('works with iterable', function () {
44+
$iterator = new ArrayIterator([
45+
['id' => 1, 'name' => 'Alice'],
46+
['id' => 2, 'name' => 'Bob'],
47+
]);
48+
49+
Assert::same(['Alice', 'Bob'], Filters::column($iterator, 'name'));
50+
});
51+
52+
53+
test('works with numeric keys', function () {
54+
$data = [
55+
[10, 'John', 30],
56+
[20, 'Jane', 25],
57+
[30, 'Bob', 35],
58+
];
59+
60+
Assert::same(['John', 'Jane', 'Bob'], Filters::column($data, 1));
61+
Assert::same([10, 20, 30], Filters::column($data, 0));
62+
});

0 commit comments

Comments
 (0)