1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Definition\ConfigurationProcessor; |
5
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Definition; |
7
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Attribute; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Processor of attributes |
11
|
|
|
* |
12
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Definition\AnnotationProcessor |
13
|
|
|
*/ |
14
|
|
|
class AttributeProcessor extends AbstractProcessor |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Pattern of "type" parameter of attribute annotation |
18
|
|
|
*/ |
19
|
|
|
const DATATYPE_PATTERN = '~^(?<type>[a-z_][a-z0-9_]*(?:\.[a-z_][a-z0-9_]*)*)\s*(?:\((?<params>[^\)]*)\))?(?<many>\[\])?$~i'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
17 |
View Code Duplication |
public function process(array $config, Definition $definition) |
|
|
|
|
25
|
|
|
{ |
26
|
17 |
|
if (! isset($config['attributes'])) { |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
17 |
|
$reflection = new \ReflectionClass($definition->getClass()); |
31
|
|
|
|
32
|
17 |
|
foreach ($config['attributes'] as $name => $data) |
33
|
|
|
{ |
34
|
17 |
|
$attribute = $this->createAttribute($reflection, $name, $data); |
35
|
|
|
|
36
|
16 |
|
$definition->addAttribute($attribute); |
37
|
|
|
} |
38
|
16 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create attribute |
42
|
|
|
* |
43
|
|
|
* @param string $name |
44
|
|
|
* @param array $data |
45
|
|
|
* @return Attribute |
46
|
|
|
*/ |
47
|
17 |
|
protected function createAttribute(\ReflectionClass $reflection, string $name, array $data): Attribute |
48
|
|
|
{ |
49
|
17 |
|
$getter = isset($data['getter']) |
50
|
17 |
|
? $data['getter'] |
51
|
17 |
|
: $this->resolveGetter($reflection, $name); |
52
|
|
|
|
53
|
17 |
|
$setter = isset($data['setter']) |
54
|
1 |
|
? $data['setter'] |
55
|
17 |
|
: $this->resolveSetter($reflection, $name); |
56
|
|
|
|
57
|
17 |
|
$attribute = new Attribute($name, $getter); |
58
|
|
|
|
59
|
17 |
|
if ($setter !== null) { |
60
|
1 |
|
$attribute->setSetter($setter); |
61
|
|
|
} |
62
|
|
|
|
63
|
17 |
|
$this->processAttributeOptions($data, $attribute); |
64
|
|
|
|
65
|
16 |
|
return $attribute; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Process optional properties of attribute |
70
|
|
|
* |
71
|
|
|
* @param array $data |
72
|
|
|
* @param Attribute $attribute |
73
|
|
|
*/ |
74
|
17 |
|
protected function processAttributeOptions(array $data, Attribute $attribute) |
75
|
|
|
{ |
76
|
17 |
|
if (isset($data['type'])) { |
77
|
17 |
|
$this->processDataType($data['type'], $attribute); |
78
|
|
|
} |
79
|
|
|
|
80
|
16 |
|
if (isset($data['many'])) { |
81
|
|
|
$attribute->setMany($data['many']); |
82
|
|
|
} |
83
|
|
|
|
84
|
16 |
|
if (isset($data['processNull'])) { |
85
|
1 |
|
$attribute->setProcessNull($data['processNull']); |
86
|
|
|
} |
87
|
16 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Process data-type |
91
|
|
|
* |
92
|
|
|
* @param string $definition |
93
|
|
|
* @param Attribute $attribute |
94
|
|
|
*/ |
95
|
17 |
View Code Duplication |
protected function processDataType(string $definition, Attribute $attribute) |
|
|
|
|
96
|
|
|
{ |
97
|
17 |
|
if (! preg_match(self::DATATYPE_PATTERN, $definition, $matches)) { |
98
|
1 |
|
throw new \LogicException(sprintf('Data-type definition "%s" is invalid.', $definition)); |
99
|
|
|
} |
100
|
|
|
|
101
|
16 |
|
$attribute->setType($matches['type']); |
102
|
|
|
|
103
|
16 |
|
if (! empty($matches['many'])) { |
104
|
5 |
|
$attribute->setMany(); |
105
|
|
|
} |
106
|
|
|
|
107
|
16 |
|
if (empty($matches['params'])) { |
108
|
11 |
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
$parameters = explode(',', $matches['params']); |
112
|
5 |
|
$parameters = array_map('trim', $parameters); |
113
|
|
|
|
114
|
5 |
|
$attribute->setTypeParameters($parameters); |
115
|
|
|
} |
116
|
|
|
} |
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.