|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheIconic\Tracking\GoogleAnalytics\Parameters; |
|
4
|
|
|
|
|
5
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Exception\InvalidCompoundParameterException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class CompoundParameter |
|
9
|
|
|
* |
|
10
|
|
|
* A compound parameter represents a set of parameters that are part of a collection. |
|
11
|
|
|
* There is a parameter name mapper that maps a human readable name that is used for passing the data |
|
12
|
|
|
* into the real name that goes in the payload. |
|
13
|
|
|
* |
|
14
|
|
|
* @package TheIconic\Tracking\GoogleAnalytics\Parameters |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class CompoundParameter implements CompoundParameterInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Maps a human readable name used when initializing the compound parameter into |
|
20
|
|
|
* the real name used in the payload. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $parameterNameMapper = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Contains the required parameters for the compound parameters. |
|
28
|
|
|
* They are in the same human readable name as in the mapper above. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $requiredParameters = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* After translating the human readable names into the payload ones, this collections |
|
36
|
|
|
* contains the map for the payload names and the values to be sent. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $parameters = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* After translating the human readable names into the payload ones, this collections |
|
44
|
|
|
* contains the map for the payload names and the values to be sent. |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $readableParameters = []; |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Validates the required parameters are passed, then translates using the mapper to later save |
|
53
|
|
|
* along with the values. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $compoundData |
|
56
|
|
|
* |
|
57
|
|
|
* @throws InvalidCompoundParameterException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(array $compoundData) |
|
60
|
|
|
{ |
|
61
|
|
|
foreach ($this->requiredParameters as $requiredParameter) { |
|
62
|
|
|
if (!array_key_exists($requiredParameter, $compoundData)) { |
|
63
|
|
|
throw new InvalidCompoundParameterException( |
|
64
|
|
|
$requiredParameter . ' is required for ' . get_class($this) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->saveCompoundParameterData($compoundData); |
|
70
|
|
|
$this->readableParameters = $compoundData; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritDoc |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getParameters() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->parameters; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @inheritDoc |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getReadableParameters() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->readableParameters; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Translates the human readable names into the payload ones and saves them along with the values. |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $compoundData |
|
93
|
|
|
* @throws \InvalidArgumentException |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function saveCompoundParameterData(array $compoundData) |
|
96
|
|
|
{ |
|
97
|
|
|
foreach ($compoundData as $name => $value) { |
|
98
|
|
|
$matchExists = false; |
|
99
|
|
|
foreach ($this->parameterNameMapper as $regex => $parameterName) { |
|
100
|
|
|
if (preg_match($regex, $name, $matches) === 1) { |
|
101
|
|
|
$parameterLastIndex = ''; |
|
102
|
|
|
|
|
103
|
|
|
if (isset($matches[1])) { |
|
104
|
|
|
$parameterLastIndex = $matches[1]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$matchExists = true; |
|
108
|
|
|
$this->parameters[$parameterName . $parameterLastIndex] = $value; |
|
109
|
|
|
|
|
110
|
|
|
break; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (!$matchExists) { |
|
115
|
|
|
throw new \InvalidArgumentException("Unknown parameter $name for " . get_class($this) . ' data'); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|