1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\Transformer; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The transformation rules parser. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
class Parser |
12
|
|
|
{ |
13
|
|
|
const KEY_SEPARATOR = ' '; |
14
|
|
|
const TRANSFORMATION_SEPARATOR = '|'; |
15
|
|
|
const PARAMETER_LIST = ':'; |
16
|
|
|
const PARAMETER_SEPARATOR = ','; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The raw rules to parse. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $rawRules; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set the dependencies. |
27
|
|
|
* |
28
|
|
|
* @param string|null $rawRules |
29
|
|
|
* @param string|null $customKey |
30
|
|
|
*/ |
31
|
51 |
|
public function __construct(string $rawRules = null, string $customKey = null) |
32
|
|
|
{ |
33
|
51 |
|
$this->rawRules = $this->resolveRawRules($rawRules, $customKey); |
34
|
51 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Resolve the raw rules to parse |
38
|
|
|
* |
39
|
|
|
* @param string|null $rawRules |
40
|
|
|
* @param string|null $customKey |
41
|
|
|
* @return string|null |
42
|
|
|
*/ |
43
|
51 |
|
protected function resolveRawRules(string $rawRules = null, string $customKey = null) |
44
|
|
|
{ |
45
|
51 |
|
if ($customKey === null) { |
46
|
51 |
|
return $rawRules; |
47
|
|
|
} |
48
|
|
|
|
49
|
33 |
|
return $rawRules === null ? $customKey : $customKey . static::KEY_SEPARATOR . $rawRules; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Retrieve the key for the current transformation |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
36 |
|
public function parseKey(): string |
58
|
|
|
{ |
59
|
36 |
|
return (string)Str::before($this->rawRules, static::KEY_SEPARATOR); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieve a map with transformations and parameters |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
36 |
|
public function parseTransformations(): array |
68
|
|
|
{ |
69
|
36 |
|
if (!$this->hasTransformations()) { |
70
|
27 |
|
return []; |
71
|
|
|
} |
72
|
|
|
|
73
|
36 |
|
$transformations = []; |
74
|
|
|
|
75
|
36 |
|
foreach ($this->getRawTransformations() as $rawTransformation) { |
76
|
36 |
|
$transformation = $this->parseTransformationName($rawTransformation); |
77
|
36 |
|
$transformations[$transformation] = $this->parseParameters($rawTransformation); |
78
|
|
|
} |
79
|
|
|
|
80
|
36 |
|
return $transformations; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Determine whether the rules contain transformations |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
39 |
|
public function hasTransformations(): bool |
89
|
|
|
{ |
90
|
39 |
|
return Str::contains($this->rawRules, static::KEY_SEPARATOR); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Retrieve the transformations to parse |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
36 |
|
protected function getRawTransformations(): array |
99
|
|
|
{ |
100
|
36 |
|
$rawTransformations = Str::after($this->rawRules, static::KEY_SEPARATOR); |
101
|
|
|
|
102
|
36 |
|
return explode(static::TRANSFORMATION_SEPARATOR, $rawTransformations); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retrieve the parsed transformation name |
107
|
|
|
* |
108
|
|
|
* @param string $rawTransformation |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
39 |
|
public function parseTransformationName(string $rawTransformation): string |
112
|
|
|
{ |
113
|
39 |
|
if (!$this->hasParameters($rawTransformation)) { |
114
|
39 |
|
return $rawTransformation; |
115
|
|
|
} |
116
|
|
|
|
117
|
30 |
|
$position = strrpos($rawTransformation, static::PARAMETER_LIST); |
118
|
|
|
|
119
|
30 |
|
return substr($rawTransformation, 0, $position); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Retrieve the parameters of the given transformation to parse |
124
|
|
|
* |
125
|
|
|
* @param string $rawTransformation |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
39 |
|
public function parseParameters(string $rawTransformation): array |
129
|
|
|
{ |
130
|
39 |
|
if (!$this->hasParameters($rawTransformation)) { |
131
|
39 |
|
return []; |
132
|
|
|
} |
133
|
|
|
|
134
|
30 |
|
$position = strrpos($rawTransformation, static::PARAMETER_LIST); |
135
|
30 |
|
$rawParameters = substr($rawTransformation, $position + 1); |
136
|
|
|
|
137
|
30 |
|
return explode(static::PARAMETER_SEPARATOR, $rawParameters); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Determine whether the given transformation to parse has parameters |
142
|
|
|
* |
143
|
|
|
* @param string $rawTransformation |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
45 |
|
public function hasParameters(string $rawTransformation): bool |
147
|
|
|
{ |
148
|
|
|
// Remove instances of :: to avoid confusing parameters with static methods |
149
|
45 |
|
$sanitised = preg_replace('/::/', '', $rawTransformation); |
150
|
|
|
|
151
|
45 |
|
return Str::contains($sanitised, static::PARAMETER_LIST); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|