-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHigher_Order_Collection_Proxy.php
More file actions
46 lines (42 loc) · 1.05 KB
/
Copy pathHigher_Order_Collection_Proxy.php
File metadata and controls
46 lines (42 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
* Higher_Order_Collection_Proxy class file.
*
* @package Mantle
*/
namespace Mantle\Support;
/**
* Higher Order Collection Proxy
*
* @mixin Enumerable
*/
class Higher_Order_Collection_Proxy {
/**
* Create a new proxy instance.
*
* @param Enumerable $collection The collection being operated on.
* @param string $method The method being proxied.
*/
public function __construct( protected Enumerable $collection, protected string $method ) {}
/**
* Proxy accessing an attribute onto the collection items.
*
* @param string $key
*/
public function __get( string $key ): mixed {
return $this->collection->{ $this->method }(
fn ( $value ) => is_array( $value ) ? $value[ $key ] : $value->{$key}
);
}
/**
* Proxy a method call onto the collection items.
*
* @param string $method
* @param array<mixed> $parameters
*/
public function __call( string $method, array $parameters ): mixed {
return $this->collection->{ $this->method }(
fn ( $value ) => $value->{ $method }( ...$parameters )
);
}
}