ObjectSorter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 17
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A valueFor() 0 3 1
A methodNameFor() 0 3 1
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