Skip to content

Commit 70b4a54

Browse files
authored
Feat: iterable_values() (#33)
1 parent a002e1f commit 70b4a54

6 files changed

+138
-0
lines changed

Diff for: README.md

+19
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,25 @@ foreach (iterable_filter($generator(), $filter) as $item) {
128128
}
129129
```
130130

131+
iterable_values()
132+
--------------
133+
134+
Works like an `array_values` with an `array` or a `Traversable`.
135+
136+
```php
137+
use function BenTools\IterableFunctions\iterable_values;
138+
139+
$generator = function () {
140+
yield 'a' => 'a';
141+
yield 'b' => 'b';
142+
};
143+
144+
foreach (iterable_values($generator()) as $key => $value) {
145+
var_dump($key); // 0, 1
146+
var_dump($value); // a, b
147+
}
148+
```
149+
131150
Iterable fluent interface
132151
=========================
133152

Diff for: src/IterableObject.php

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ public function map(callable $mapper): self
7070
return new self(array_map($mapper, $this->iterable));
7171
}
7272

73+
/**
74+
* @return self<int, TValue>
75+
*/
76+
public function values(): self
77+
{
78+
return new self(new WithoutKeysTraversable($this->iterable));
79+
}
80+
7381
/** @return Traversable<TKey, TValue> */
7482
public function getIterator(): Traversable
7583
{

Diff for: src/WithoutKeysTraversable.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BenTools\IterableFunctions;
6+
7+
use Generator;
8+
use IteratorAggregate;
9+
10+
/**
11+
* @internal
12+
*
13+
* @template TKey
14+
* @template TValue
15+
* @implements IteratorAggregate<TKey, TValue>
16+
*/
17+
final class WithoutKeysTraversable implements IteratorAggregate
18+
{
19+
/** @var iterable<TKey, TValue> */
20+
private $iterable;
21+
22+
/**
23+
* @param iterable<TKey, TValue> $iterable
24+
*/
25+
public function __construct(iterable $iterable)
26+
{
27+
$this->iterable = $iterable;
28+
}
29+
30+
/**
31+
* @return Generator<TValue>
32+
*/
33+
public function getIterator(): Generator
34+
{
35+
foreach ($this->iterable as $value) {
36+
yield $value;
37+
}
38+
}
39+
}

Diff for: src/iterable-functions.php

+16
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ function iterable_reduce(iterable $iterable, callable $reduce, $initial = null)
114114
return $initial;
115115
}
116116

117+
/**
118+
* Yields iterable values (leaving out keys).
119+
*
120+
* @param iterable<TValue> $iterable
121+
*
122+
* @return iterable<int, TValue>
123+
*
124+
* @template TValue
125+
*/
126+
function iterable_values(iterable $iterable): iterable
127+
{
128+
$withoutKeys = iterable($iterable)->values();
129+
130+
return is_array($iterable) ? $withoutKeys->asArray() : $withoutKeys;
131+
}
132+
117133
/**
118134
* @param iterable<TKey, TValue>|null $iterable
119135
*

Diff for: tests/IterableObjectTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,11 @@ static function (string $value) {
116116
})($input),
117117
];
118118
});
119+
120+
it('strips key through values()', function (): void {
121+
$input = ['x' => 'zero', 'y' => 'one', 'z' => 'two'];
122+
123+
$iterableObject = iterable($input)->values();
124+
assertInstanceOf(IterableObject::class, $iterableObject);
125+
assertEquals(['zero', 'one', 'two'], $iterableObject->asArray());
126+
});

Diff for: tests/IterableValuesTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BenTools\IterableFunctions\Tests;
6+
7+
use ArrayIterator;
8+
use PHPUnit\Framework\Assert;
9+
10+
use function BenTools\IterableFunctions\iterable_values;
11+
use function it;
12+
use function PHPUnit\Framework\assertSame;
13+
14+
it(
15+
'gets only values of array',
16+
function (): void {
17+
$iterable = ['b' => true];
18+
19+
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
20+
foreach (iterable_values($iterable) as $key => $value) {
21+
}
22+
23+
if (! isset($key, $value)) {
24+
Assert::fail('No values were returned');
25+
}
26+
27+
assertSame(0, $key);
28+
assertSame(true, $value);
29+
}
30+
);
31+
32+
it(
33+
'gets values of Traversable object',
34+
function (): void {
35+
$iterable = new ArrayIterator(['b' => true]);
36+
37+
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
38+
foreach (iterable_values($iterable) as $key => $value) {
39+
}
40+
41+
if (! isset($key, $value)) {
42+
Assert::fail('No values were returned');
43+
}
44+
45+
assertSame(0, $key);
46+
assertSame(true, $value);
47+
}
48+
);

0 commit comments

Comments
 (0)