Passed
Push — master ( 5ad58b...7269b1 )
by Chris
07:31
created

PropertyResolver::resolvePropertyAccessor()   A

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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace WebTheory\Collection\Resolution;
4
5
use LogicException;
6
use WebTheory\Collection\Contracts\PropertyResolverInterface;
7
8
class PropertyResolver implements PropertyResolverInterface
9
{
10
    protected array $accessors = [];
11
12 90
    public function __construct(array $accessors = [])
13
    {
14 90
        $this->accessors = $accessors;
15
    }
16
17 90
    public function resolveProperty(object $object, string $property)
18
    {
19 90
        $accessor = $this->resolvePropertyAccessor($property);
20
21 90
        if ($accessor && method_exists($object, $accessor)) {
22 9
            return $object->{$accessor}();
23
        }
24
25 90
        if (property_exists($object, $property)) {
26 90
            return $object->$property;
27
        }
28
29 3
        throw new LogicException(
30 3
            sprintf(
31
                'No method of access for "%s" in %s has been defined.',
32
                $property,
33 3
                get_class($object)
34
            )
35
        );
36
    }
37
38 90
    protected function resolvePropertyAccessor(string $property): ?string
39
    {
40 90
        return $this->accessors[$property] ?? null;
41
    }
42
}
43