Completed
Push — master ( 07e238...0f15f1 )
by Pavel
09:14
created

PublicPropertyMapper::getReflection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace ScayTrase\Api\Cruds;
4
5
final class PublicPropertyMapper implements PropertyMapperInterface
6
{
7
    /** @var \ReflectionClass[] */
8
    private $reflections = [];
9
10
    /** {@inheritdoc} */
11 2
    public function getEntityProperty($className, $apiProperty)
12
    {
13 2
        if (in_array($apiProperty, $this->getApiProperties($className), true)) {
14 2
            return $apiProperty;
15
        }
16
17
        return null;
18
    }
19
20
    /** {@inheritdoc} */
21
    public function getApiProperty($className, $objectProperty)
22
    {
23
        if (in_array($objectProperty, $this->getEntityProperties($className), true)) {
24
            return $objectProperty;
25
        }
26
27
        return null;
28
    }
29
30
    /** {@inheritdoc} */
31 2
    public function getApiProperties($className)
32
    {
33 2
        return $this->getEntityProperties($className);
34
    }
35
36
    /** {@inheritdoc} */
37 2
    public function getEntityProperties($className)
38
    {
39 2
        return array_map(
40 2
            [$this, 'getPropertyName'],
41 2
            $this->getReflection($className)->getProperties(\ReflectionProperty::IS_PUBLIC)
42 2
        );
43
    }
44
45
    /**
46
     * @param $className
47
     *
48
     * @return \ReflectionClass
49
     */
50 2
    private function getReflection($className)
51
    {
52 2
        if (!array_key_exists($className, $this->reflections)) {
53 2
            $this->reflections[$className] = new \ReflectionClass($className);
54 2
        }
55
56 2
        return $this->reflections[$className];
57
    }
58
59 2
    private function getPropertyName(\ReflectionProperty $property)
60
    {
61 2
        return $property->name;
62
    }
63
}
64