Completed
Push — master ( e80ea0...ba2e48 )
by Randy
02:42
created

ClassMapper::existsClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
/**
6
 * Class ClassMapper
7
 * @package Dgame\Soap\Hydrator
8
 */
9
final class ClassMapper
10
{
11
    /**
12
     * @var array
13
     */
14
    private $classmap = [];
15
    /**
16
     * @var array
17
     */
18
    private $pattern = [];
19
20
    /**
21
     * AutoLoad constructor.
22
     *
23
     * @param array $classmap
24
     */
25 9
    public function __construct(array $classmap = [])
26
    {
27 9
        $this->classmap = $classmap;
28 9
    }
29
30
    /**
31
     * @param string $name
32
     * @param string $class
33
     */
34
    public function appendClass(string $name, string $class)
35
    {
36
        $this->classmap[$name] = $class;
37
    }
38
39
    /**
40
     * @param string $pattern
41
     * @param string $class
42
     */
43 2
    public function appendPattern(string $pattern, string $class)
44
    {
45 2
        $this->pattern[$pattern] = $class;
46 2
    }
47
48
    /**
49
     * @param string $name
50
     *
51
     * @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...
52
     */
53 9
    public function new(string $name)
54
    {
55 9
        if (($class = $this->findClassName($name)) !== null) {
56 9
            return Hydrate::new($name, $class);
57
        }
58
59 9
        return null;
60
    }
61
62
    /**
63
     * @param string $class
64
     *
65
     * @return null|string
66
     */
67 9
    private function findClassName(string $class)
68
    {
69 9
        $name = $this->getClassName($class);
70 9
        if ($name !== null) {
71 9
            return $name;
72
        }
73
74 9
        foreach ($this->pattern as $pattern => $name) {
75 2
            if (preg_match($pattern, $class) === 1) {
76 2
                return $this->getClassName($name);
77
            }
78
        }
79
80 9
        return null;
81
    }
82
83
    /**
84
     * @param string $class
85
     *
86
     * @return null|string
87
     */
88 9
    private function getClassName(string $class)
89
    {
90 9
        foreach ($this->searchClassName($class) as $name) {
91 9
            if ($this->existsClass($name)) {
92 9
                return $name;
93
            }
94
        }
95
96 9
        return null;
97
    }
98
99
    /**
100
     * @param string $class
101
     *
102
     * @return \Generator
103
     */
104 9
    private function searchClassName(string $class)
105
    {
106 9
        $names = [ucfirst($class), lcfirst($class)];
107 9
        foreach ($names as $name) {
108 9
            if ($this->hasClassInClassmap($name)) {
109 9
                yield $this->classmap[$name];
110
            }
111
        }
112 9
    }
113
114
    /**
115
     * @param string $class
116
     *
117
     * @return bool
118
     */
119 9
    private function hasClassInClassmap(string $class): bool
120
    {
121 9
        return array_key_exists($class, $this->classmap);
122
    }
123
124
    /**
125
     * @param string $class
126
     *
127
     * @return bool
128
     */
129 9
    private function existsClass(string $class): bool
130
    {
131 9
        return class_exists($class);
132
    }
133
}