1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Serializard\NormalizerContainer; |
3
|
|
|
|
4
|
|
|
use Thunder\Serializard\Exception\InvalidClassNameException; |
5
|
|
|
use Thunder\Serializard\Exception\InvalidNormalizerException; |
6
|
|
|
use Thunder\Serializard\Exception\NormalizerConflictException; |
7
|
|
|
use Thunder\Serializard\Exception\NormalizerNotFoundException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
final class FallbackNormalizerContainer implements NormalizerContainerInterface |
13
|
20 |
|
{ |
14
|
|
|
private $default; |
15
|
20 |
|
private $handlers = []; |
16
|
1 |
|
private $interfaces = []; |
17
|
|
|
private $aliases = []; |
18
|
|
|
|
19
|
19 |
View Code Duplication |
public function add($class, $handler) |
|
|
|
|
20
|
15 |
|
{ |
21
|
15 |
|
if(false === is_callable($handler)) { |
22
|
19 |
|
throw new InvalidNormalizerException(sprintf('Invalid handler for class %s!', $class)); |
23
|
3 |
|
} |
24
|
3 |
|
|
25
|
3 |
|
if(class_exists($class)) { |
26
|
1 |
|
$this->aliases[$class] = $class; |
27
|
|
|
$this->handlers[$class] = $handler; |
28
|
18 |
|
} elseif(interface_exists($class)) { |
29
|
|
|
$this->aliases[$class] = $class; |
30
|
2 |
|
$this->interfaces[$class] = $handler; |
31
|
|
|
} else { |
32
|
2 |
|
throw new InvalidClassNameException(sprintf('Given value %s is neither class nor interface name!', $class)); |
33
|
|
|
} |
34
|
2 |
|
} |
35
|
1 |
|
|
36
|
|
View Code Duplication |
public function addAlias($alias, $class) |
|
|
|
|
37
|
|
|
{ |
38
|
1 |
|
$handler = $this->getHandler($class); |
39
|
1 |
|
|
40
|
1 |
|
if(null === $handler) { |
41
|
|
|
throw new NormalizerNotFoundException(sprintf('Handler for class %s does not exist!', $class)); |
42
|
17 |
|
} |
43
|
|
|
|
44
|
17 |
|
$this->handlers[$alias] = $handler; |
45
|
12 |
|
$this->aliases[$alias] = $this->aliases[$class]; |
46
|
|
|
} |
47
|
|
|
|
48
|
7 |
View Code Duplication |
public function getHandler($class) |
|
|
|
|
49
|
7 |
|
{ |
50
|
2 |
|
if(array_key_exists($class, $this->handlers)) { |
51
|
|
|
return $this->handlers[$class]; |
52
|
|
|
} |
53
|
5 |
|
|
54
|
5 |
|
$parents = array_intersect(array_keys($this->handlers), class_parents($class)); |
55
|
3 |
|
if($parents) { |
|
|
|
|
56
|
1 |
|
return $this->handlers[array_pop($parents)]; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
$interfaces = array_intersect(array_keys($this->interfaces), array_values(class_implements($class))); |
60
|
|
|
if($interfaces) { |
|
|
|
|
61
|
|
|
if(count($interfaces) > 1) { |
62
|
2 |
|
throw new NormalizerConflictException(sprintf('Class %s implements interfaces with colliding handlers!', $class)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->interfaces[array_shift($interfaces)]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->default; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setDefault($handler) |
72
|
|
|
{ |
73
|
|
|
if(false === is_callable($handler)) { |
74
|
|
|
throw new InvalidNormalizerException('Default normalizer handler must be callable!'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->default = $handler; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function hasDefault() |
81
|
|
|
{ |
82
|
|
|
return null !== $this->default; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getDefault() |
86
|
|
|
{ |
87
|
|
|
return $this->default; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.