Skip to content

Commit 987a3d0

Browse files
committed
- new function: tap()
- new function: extended() - new interface: `Tapped`
1 parent 6b49c1a commit 987a3d0

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

Interfaces.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,5 @@ public function unserialize(string $value): mixed;
384384
*/
385385
public function type(): string;
386386
}
387+
388+
interface Tapped {}

functions.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Koded\Stdlib\Serializer\{JsonSerializer, XmlSerializer};
1818
use RecursiveDirectoryIterator;
1919
use RecursiveIteratorIterator;
20+
use stdClass;
2021
use function array_diff_assoc;
2122
use function array_key_exists;
2223
use function array_product;
@@ -57,9 +58,9 @@ function arguments(...$values): Argument
5758
* with optional arbitrary number of arguments.
5859
*
5960
* @param array ...$values
60-
* @return Argument
61+
* @return ExtendedArguments
6162
*/
62-
function extended_arguments(...$values): Argument
63+
function extended(...$values): ExtendedArguments
6364
{
6465
return new ExtendedArguments(...$values);
6566
}
@@ -76,6 +77,31 @@ function value(...$values): Data
7677
return new Immutable(...$values);
7778
}
7879

80+
/**
81+
* Do something with an object or array inside the callable,
82+
* and return that value.
83+
*
84+
* @param mixed $value The value that is tapped into the callback
85+
* @param callable|null $callable Callback that can modify the value
86+
* @return mixed The tapped value
87+
*/
88+
function tap(mixed $value, callable $callable = null): mixed
89+
{
90+
if (false === is_null($callable)) {
91+
$callable($value);
92+
return $value;
93+
}
94+
return new class($value) extends stdClass implements Tapped {
95+
public function __construct(private mixed $value) {}
96+
public function __call($method, $arguments) {
97+
// @codeCoverageIgnoreStart
98+
$this->value->{$method}(...$arguments);
99+
return $this->value;
100+
// @codeCoverageIgnoreEnd
101+
}
102+
};
103+
}
104+
79105
/**
80106
* Transforms simple CamelCaseName into camel_case_name (lower case underscored).
81107
*

0 commit comments

Comments
 (0)