Skip to content

Commit 7121ba0

Browse files
committed
feat: apply specific namespace
1 parent 69b0d99 commit 7121ba0

9 files changed

+193
-417
lines changed

src/IterableObject.php

Lines changed: 23 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace BenTools\IterableFunctions;
44

5-
use Closure;
65
use EmptyIterator;
7-
use InvalidArgumentException;
86
use IteratorAggregate;
97
use Traversable;
108

9+
/**
10+
* @internal
11+
*/
1112
final class IterableObject implements IteratorAggregate
1213
{
1314
/**
@@ -18,113 +19,50 @@ final class IterableObject implements IteratorAggregate
1819
/**
1920
* @var callable
2021
*/
21-
private $filter;
22+
private $filterFn;
2223

2324
/**
2425
* @var callable
2526
*/
26-
private $map;
27+
private $mapFn;
2728

28-
/**
29-
* IterableObject constructor.
30-
* @param iterable|array|Traversable $iterable
31-
* @param callable|null $filter
32-
* @param callable|null $map
33-
* @throws InvalidArgumentException
34-
*/
35-
public function __construct($iterable, $filter = null, $map = null)
29+
public function __construct(?iterable $iterable = null, ?callable $filter = null, ?callable $map = null)
3630
{
37-
if (null === $iterable) {
38-
$iterable = new EmptyIterator();
39-
}
40-
if (!is_iterable($iterable)) {
41-
throw new InvalidArgumentException(
42-
sprintf('Expected array or Traversable, got %s', is_object($iterable) ? get_class($iterable) : gettype($iterable))
43-
);
44-
}
45-
46-
// Cannot rely on callable type-hint on PHP 5.3
47-
if (null !== $filter && !is_callable($filter) && !$filter instanceof Closure) {
48-
throw new InvalidArgumentException(
49-
sprintf('Expected callable, got %s', is_object($filter) ? get_class($filter) : gettype($filter))
50-
);
51-
}
52-
53-
if (null !== $map && !is_callable($map) && !$map instanceof Closure) {
54-
throw new InvalidArgumentException(
55-
sprintf('Expected callable, got %s', is_object($map) ? get_class($map) : gettype($map))
56-
);
57-
}
58-
59-
$this->iterable = $iterable;
60-
$this->filter = $filter;
61-
$this->map = $map;
31+
$this->iterable = $iterable ?? new EmptyIterator();
32+
$this->filterFn = $filter;
33+
$this->mapFn = $map;
6234
}
6335

64-
/**
65-
* @param callable $filter
66-
* @return self
67-
*/
68-
public function filter($filter)
36+
public function filter(callable $filter): self
6937
{
70-
return new self($this->iterable, $filter, $this->map);
38+
return new self($this->iterable, $filter, $this->mapFn);
7139
}
7240

73-
/**
74-
* @param callable $map
75-
* @return self
76-
*/
77-
public function map($map)
41+
public function map(callable $map): self
7842
{
79-
return new self($this->iterable, $this->filter, $map);
43+
return new self($this->iterable, $this->filterFn, $map);
8044
}
8145

82-
/**
83-
* @param callable $filter
84-
* @return self
85-
* @deprecated Use IterableObject::filter instead.
86-
*/
87-
public function withFilter($filter)
88-
{
89-
return $this->filter($filter);
90-
}
91-
92-
/**
93-
* @param callable $map
94-
* @return self
95-
* @deprecated Use IterableObject::map instead.
96-
*/
97-
public function withMap($map)
98-
{
99-
return $this->map($map);
100-
}
101-
102-
/**
103-
* @inheritdoc
104-
*/
105-
public function getIterator()
46+
public function getIterator(): Traversable
10647
{
10748
$iterable = $this->iterable;
108-
if (null !== $this->filter) {
109-
$iterable = iterable_filter($iterable, $this->filter);
49+
if (null !== $this->filterFn) {
50+
$iterable = iterable_filter($iterable, $this->filterFn);
11051
}
111-
if (null !== $this->map) {
112-
$iterable = iterable_map($iterable, $this->map);
52+
if (null !== $this->mapFn) {
53+
$iterable = iterable_map($iterable, $this->mapFn);
11354
}
11455
return iterable_to_traversable($iterable);
11556
}
11657

117-
/**
118-
* @return array
119-
*/
120-
public function asArray()
58+
public function asArray(): array
12159
{
12260
$iterable = $this->iterable;
123-
if (null !== $this->filter) {
124-
$iterable = iterable_filter($iterable, $this->filter);
61+
if (null !== $this->filterFn) {
62+
$iterable = iterable_filter($iterable, $this->filterFn);
12563
}
126-
if (null !== $this->map) {
127-
$iterable = iterable_map($iterable, $this->map);
64+
if (null !== $this->mapFn) {
65+
$iterable = iterable_map($iterable, $this->mapFn);
12866
}
12967
return iterable_to_array($iterable);
13068
}

0 commit comments

Comments
 (0)