1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Definition\ConfigurationProcessor; |
5
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Link; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Abstract processor |
10
|
|
|
* Contains shared methods |
11
|
|
|
* |
12
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Definition\ConfigurationProcessor |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractProcessor implements ConfigurationProcessorInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Pattern of "resource" parameter of link annotation |
18
|
|
|
*/ |
19
|
|
|
const RESOURCE_PATTERN = '~^(?<repository>[a-z_][a-z0-9_]*)\.(?<link>[a-z_][a-z0-9_]*)$~i'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Create link |
23
|
|
|
* |
24
|
|
|
* @param string $name |
25
|
|
|
* @param array $data |
26
|
|
|
* @return Link |
27
|
|
|
*/ |
28
|
2 |
|
public function createLink(string $name, array $data): Link |
29
|
|
|
{ |
30
|
2 |
|
if (! preg_match(self::RESOURCE_PATTERN, $data['resource'], $matches)) { |
31
|
|
|
throw new \LogicException(sprintf('Invalid resource definition: "%s"', $data['resource'])); |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
$link = new Link( |
35
|
2 |
|
$name, |
36
|
2 |
|
$matches['repository'], |
37
|
2 |
|
$matches['link'] |
38
|
|
|
); |
39
|
|
|
|
40
|
2 |
|
$link->setParameters($data['parameters']); |
41
|
2 |
|
$link->setMetadata($data['metadata']); |
42
|
|
|
|
43
|
2 |
|
return $link; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Resolve getter of related object |
48
|
|
|
* |
49
|
|
|
* @param \ReflectionClass $reflection |
50
|
|
|
* @param string $name |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
protected function resolveGetter(\ReflectionClass $reflection, string $name) |
54
|
|
|
{ |
55
|
|
View Code Duplication |
foreach (['get', 'is'] as $prefix) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$getter = $prefix . ucfirst($name); |
58
|
|
|
|
59
|
|
|
if ($reflection->hasMethod($getter) && $reflection->getMethod($getter)->isPublic()) { |
60
|
|
|
return $getter; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
throw new \LogicException(sprintf( |
65
|
|
|
'Getter-method for "%s" cannot be resolved automatically. ' . |
66
|
|
|
'Probably there is no get%2$s() or is%2$s() method or it is not public.', |
67
|
|
|
$name, ucfirst($name) |
68
|
|
|
)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Resolve getter of related object |
73
|
|
|
* |
74
|
|
|
* @param \ReflectionClass $reflection |
75
|
|
|
* @param string $name |
76
|
|
|
* @return string | null |
77
|
|
|
*/ |
78
|
16 |
|
protected function resolveSetter(\ReflectionClass $reflection, string $name) |
79
|
|
|
{ |
80
|
16 |
|
$setter = 'set' . ucfirst($name); |
81
|
|
|
|
82
|
16 |
|
if ($reflection->hasMethod($setter) && $reflection->getMethod($setter)->isPublic()) { |
83
|
|
|
return $setter; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
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.