1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ /*
6
+ * This file is part of the SolidWorx Lodash-PHP project.
7
+ *
8
+ * @author Pierre du Plessis <open-source@solidworx.co>
9
+ * @copyright Copyright (c) 2017
10
+ */
11
+
12
+ namespace _ ;
13
+
14
+ use function _ \internal \arrayMap ;
15
+ use function _ \internal \baseFlatten ;
16
+ use function _ \internal \baseRest ;
17
+ use function _ \internal \baseUnary ;
18
+
19
+ /**
20
+ * Creates a function that invokes `func` with its arguments transformed.
21
+ *
22
+ * @static
23
+ * @memberOf _
24
+ * @category Function
25
+ *
26
+ * @param callable $func The function to wrap.
27
+ * @param callable[] $transforms The argument transforms.
28
+ *
29
+ * @return callable the new function.
30
+ *
31
+ * @example
32
+ * <code>
33
+ * function doubled($n) {
34
+ * return $n * 2;
35
+ * }
36
+ *
37
+ * function square($n) {
38
+ * return $n * $n;
39
+ * }
40
+ *
41
+ * $func = overArgs(function($x, $y) {
42
+ * return [$x, $y];
43
+ * }, ['square', 'doubled']);
44
+ *
45
+ * $func(9, 3);
46
+ * // => [81, 6]
47
+ *
48
+ * $func(10, 5);
49
+ * // => [100, 10]
50
+ * </code>
51
+ */
52
+ function overArgs (callable $ func , array $ transforms ): callable
53
+ {
54
+ return baseRest (function ($ func , $ transforms ) {
55
+ $ transforms = (count ($ transforms ) == 1 && \is_array ($ transforms [0 ]))
56
+ ? arrayMap ($ transforms [0 ], baseUnary ('\_\internal\baseIteratee ' ))
57
+ : arrayMap (baseFlatten ($ transforms , 1 ), baseUnary ('\_\internal\baseIteratee ' ));
58
+
59
+ $ funcsLength = \count ($ transforms );
60
+
61
+ return baseRest (function (...$ args ) use ($ funcsLength , $ transforms , $ func ) {
62
+ $ index = -1 ;
63
+ $ length = \min (\count ($ args ), $ funcsLength );
64
+
65
+ while (++$ index < $ length ) {
66
+ $ args [$ index ] = $ transforms [$ index ]($ args [$ index ]);
67
+ }
68
+
69
+ return $ func (...$ args );
70
+ });
71
+ })($ func , $ transforms );
72
+ }
0 commit comments