HigherOrderCollectionProxy   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 0 4 1
A __get() 0 4 2
1
<?php
2
3
namespace MiotApi\Util\Collection;
4
5
/**
6
 * @mixin \MiotApi\Util\Collection\Collection
7
 */
8
class HigherOrderCollectionProxy
9
{
10
    /**
11
     * The collection being operated on.
12
     *
13
     * @var \MiotApi\Util\Collection\Collection
14
     */
15
    protected $collection;
16
17
    /**
18
     * The method being proxied.
19
     *
20
     * @var string
21
     */
22
    protected $method;
23
24
    /**
25
     * Create a new proxy instance.
26
     *
27
     * @param \MiotApi\Util\Collection\Collection $collection
28
     * @param string                              $method
29
     *
30
     * @return void
31
     */
32
    public function __construct(Collection $collection, $method)
33
    {
34
        $this->method = $method;
35
        $this->collection = $collection;
36
    }
37
38
    /**
39
     * Proxy accessing an attribute onto the collection items.
40
     *
41
     * @param string $key
42
     *
43
     * @return mixed
44
     */
45
    public function __get($key)
46
    {
47
        return $this->collection->{$this->method}(function ($value) use ($key) {
48
            return is_array($value) ? $value[$key] : $value->{$key};
49
        });
50
    }
51
52
    /**
53
     * Proxy a method call onto the collection items.
54
     *
55
     * @param string $method
56
     * @param array  $parameters
57
     *
58
     * @return mixed
59
     */
60
    public function __call($method, $parameters)
61
    {
62
        return $this->collection->{$this->method}(function ($value) use ($method, $parameters) {
63
            return $value->{$method}(...$parameters);
64
        });
65
    }
66
}
67