ObjectSorter::valueFor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Sorting;
5
6
/**
7
 * Sorts a collection of objects, accessing fields through public methods.
8
 *
9
 * The object sorter accepts an optional map to translate fields to method names.
10
 *
11
 * @author  Stratadox
12
 * @package Stratadox\Sorting
13
 */
14
class ObjectSorter extends ElementSorter
15
{
16
    private $methodNameFor;
17
18
    public function __construct(array $methodNames = [])
19
    {
20
        $this->methodNameFor = $methodNames;
21
    }
22
23
    protected function valueFor($element, string $field)
24
    {
25
        return $element->{$this->methodNameFor($field)}();
26
    }
27
28
    private function methodNameFor(string $field): string
29
    {
30
        return $this->methodNameFor[$field] ?? $field;
31
    }
32
}
33