LocalizationAwareObjectAccess   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 40
c 1
b 0
f 1
dl 0
loc 83
rs 10
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSetterMapping() 0 24 6
B getGetterMapping() 0 28 8
A __construct() 0 5 1
1
<?php
2
3
4
namespace Apie\ObjectAccessNormalizer\ObjectAccess;
5
6
use Apie\ObjectAccessNormalizer\Getters\GetterInterface;
7
use Apie\ObjectAccessNormalizer\Getters\ReflectionLocalizedGetterMethod;
8
use Apie\ObjectAccessNormalizer\Interfaces\LocalizationAwareInterface;
9
use Apie\ObjectAccessNormalizer\Setters\ReflectionLocalizedSetterMethod;
10
use Apie\ObjectAccessNormalizer\Setters\SetterInterface;
11
use ReflectionClass;
12
13
class LocalizationAwareObjectAccess extends ObjectAccess
14
{
15
    /**
16
     * @var GetterInterface[][][]
17
     */
18
    private $getterCache = [];
19
20
    /**
21
     * @var SetterInterface[][][]
22
     */
23
    private $setterCache = [];
24
25
    /**
26
     * @var LocalizationAwareInterface
27
     */
28
    private $localizationAware;
29
30
    /**
31
     * @var callable
32
     */
33
    private $conversionFn;
34
35
    public function __construct(LocalizationAwareInterface  $localizationAware, callable $conversionFn, bool $publicOnly = true, bool $disabledConstructor = false)
36
    {
37
        $this->localizationAware = $localizationAware;
38
        $this->conversionFn = $conversionFn;
39
        parent::__construct($publicOnly, $disabledConstructor);
40
    }
41
42
    protected function getGetterMapping(ReflectionClass $reflectionClass): array
43
    {
44
        $className= $reflectionClass->getName();
45
        if (isset($this->getterCache[$className])) {
46
            return $this->getterCache[$className];
47
        }
48
49
        $attributes = parent::getGetterMapping($reflectionClass);
50
51
        $reflectionMethods = $this->getAvailableMethods($reflectionClass);
52
        foreach ($reflectionMethods as $method) {
53
            if ($method->getNumberOfRequiredParameters() === 1
54
                && !$method->isStatic()
55
                && preg_match('/^(get|has|is)[A-Z0-9]/i', $method->name)) {
56
                $fieldName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
57
                $attributes[$fieldName][] = new ReflectionLocalizedGetterMethod(
58
                    $method,
59
                    $this->localizationAware,
60
                    $this->conversionFn
61
                );
62
            }
63
        }
64
65
        foreach ($attributes as &$methods) {
66
            $this->sort($methods);
67
        }
68
69
        return $this->getterCache[$className] = $attributes;
70
    }
71
72
    protected function getSetterMapping(ReflectionClass $reflectionClass): array
73
    {
74
        $className= $reflectionClass->getName();
75
        if (isset($this->setterCache[$className])) {
76
            return $this->setterCache[$className];
77
        }
78
79
        $attributes = parent::getSetterMapping($reflectionClass);
80
81
        $reflectionMethods = $this->getAvailableMethods($reflectionClass);
82
        foreach ($reflectionMethods as $method) {
83
            if ($method->getNumberOfRequiredParameters() === 2
84
                && !$method->isStatic()
85
                && preg_match('/^(set)[A-Z0-9]/i', $method->name)) {
86
                $fieldName = lcfirst(substr($method->name, 3));
87
                $attributes[$fieldName][] = new ReflectionLocalizedSetterMethod(
88
                    $method,
89
                    $this->localizationAware,
90
                    $this->conversionFn
91
                );
92
            }
93
        }
94
95
        return $this->setterCache[$className] = $attributes;
96
    }
97
}
98