Completed
Push — master ( ba2e48...30eea4 )
by Randy
02:38
created

ClassMapper::new()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
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 Dgame\Soap\Hydrator;
4
5
use Dgame\Variants\Variants;
6
7
/**
8
 * Class ClassMapper
9
 * @package Dgame\Soap\Hydrator
10
 */
11
final class ClassMapper
12
{
13
    /**
14
     * @var array
15
     */
16
    private $classmap = [];
17
    /**
18
     * @var array
19
     */
20
    private $pattern = [];
21
22
    /**
23
     * AutoLoad constructor.
24
     *
25
     * @param array $classmap
26
     */
27 9
    public function __construct(array $classmap = [])
28
    {
29 9
        $this->classmap = $classmap;
30 9
    }
31
32
    /**
33
     * @param string $name
34
     * @param string $class
35
     */
36
    public function appendClass(string $name, string $class)
37
    {
38
        $this->classmap[$name] = $class;
39
    }
40
41
    /**
42
     * @param string $pattern
43
     * @param string $class
44
     */
45 2
    public function appendPattern(string $pattern, string $class)
46
    {
47 2
        $this->pattern[$pattern] = $class;
48 2
    }
49
50
    /**
51
     * @param string $name
52
     *
53
     * @return Hydrate|null
0 ignored issues
show
Documentation introduced by
Should the return type not be \self|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
54
     */
55 9
    public function new(string $name)
56
    {
57 9
        if (($class = $this->findClassName($name)) !== null) {
58 9
            return Hydrate::new($name, $class);
59
        }
60
61 9
        return null;
62
    }
63
64
    /**
65
     * @param string $class
66
     *
67
     * @return null|string
68
     */
69 9
    private function findClassName(string $class)
70
    {
71 9
        $name = $this->getClassName($class);
72 9
        if ($name !== null) {
73 9
            return $name;
74
        }
75
76 9
        foreach ($this->pattern as $pattern => $name) {
77 2
            if (preg_match($pattern, $class) === 1) {
78 2
                return $this->getClassName($name);
79
            }
80
        }
81
82 9
        return null;
83
    }
84
85
    /**
86
     * @param string $class
87
     *
88
     * @return null|string
89
     */
90 9
    private function getClassName(string $class)
91
    {
92 9
        foreach ($this->searchClassName($class) as $name) {
93 9
            if ($this->existsClass($name)) {
94 9
                return $name;
95
            }
96
        }
97
98 9
        return null;
99
    }
100
101
    /**
102
     * @param string $class
103
     *
104
     * @return \Generator
105
     */
106 9
    private function searchClassName(string $class)
107
    {
108 9
        foreach (Variants::ofArguments($class)->withCamelSnakeCase() as $name) {
109 9
            if ($this->hasClassInClassmap($name)) {
110 9
                yield $this->classmap[$name];
111
            }
112
        }
113 9
    }
114
115
    /**
116
     * @param string $class
117
     *
118
     * @return bool
119
     */
120 9
    private function hasClassInClassmap(string $class): bool
121
    {
122 9
        return array_key_exists($class, $this->classmap);
123
    }
124
125
    /**
126
     * @param string $class
127
     *
128
     * @return bool
129
     */
130 9
    private function existsClass(string $class): bool
131
    {
132 9
        return class_exists($class);
133
    }
134
}