Getter::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Bdf\Collection\Util\Functor\Transformer;
4
5
/**
6
 * Call a getter on the element
7
 *
8
 * (i) This transformer do not add any prefix on the getter.
9
 *     If the method name is getName(), you must instantiate with `new Getter('getName')`
10
 *
11
 * <code>
12
 * $persons->stream()
13
 *     ->map(new Getter('name')) // Call method name() on each element, and return the result
14
 *     ->toArray()
15
 * ;
16
 * </code>
17
 */
18
final class Getter implements TransformerInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    private $name;
24
25
26
    /**
27
     * Getter constructor.
28
     *
29
     * @param string $name The method name
30
     */
31 7
    public function __construct(string $name)
32
    {
33 7
        $this->name = $name;
34 7
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 7
    public function __invoke($element, $key = null)
40
    {
41 7
        return $element->{$this->name}();
42
    }
43
}
44