@@ -8,19 +8,14 @@ class UtmParameter
8
8
{
9
9
/**
10
10
* Bag containing all UTM-Parameters.
11
- *
12
- * @var array
13
11
*/
14
- public array | null $ parameters ;
12
+ public ? array $ parameters ;
15
13
16
14
/**
17
15
* Utm Parameter Session Key.
18
- *
19
- * @var string
20
16
*/
21
17
public string $ sessionKey ;
22
18
23
-
24
19
public function __construct (array $ parameters = [])
25
20
{
26
21
$ this ->sessionKey = config ('utm-parameter.session_key ' );
@@ -29,36 +24,32 @@ public function __construct(array $parameters = [])
29
24
30
25
/**
31
26
* Bootstrap UtmParameter.
32
- *
33
- * @param Request $request
34
- *
35
- * @return UtmParameter
36
27
*/
37
- public function boot (Request $ request )
28
+ public function boot (Request $ request ): self
38
29
{
39
30
$ this ->parameters = $ this ->useRequestOrSession ($ request );
31
+
40
32
return $ this ;
41
33
}
42
34
43
35
/**
44
36
* Check which Parameters should be used.
45
- *
46
- * @param \Illuminate\Http\Request $request
47
- * @return array
48
37
*/
49
- public function useRequestOrSession (Request $ request )
38
+ public function useRequestOrSession (Request $ request ): ? array
50
39
{
51
40
$ currentRequestParameter = $ this ->getParameter ($ request );
52
41
$ sessionParameter = session ($ this ->sessionKey );
53
42
54
- if (!empty ($ currentRequestParameter ) && empty ($ sessionParameter )) {
43
+ if (! empty ($ currentRequestParameter ) && empty ($ sessionParameter )) {
55
44
session ([$ this ->sessionKey => $ currentRequestParameter ]);
45
+
56
46
return $ currentRequestParameter ;
57
47
}
58
48
59
- if (!empty ($ currentRequestParameter ) && !empty ($ sessionParameter ) && config ('utm-parameter.override_utm_parameters ' )) {
49
+ if (! empty ($ currentRequestParameter ) && ! empty ($ sessionParameter ) && config ('utm-parameter.override_utm_parameters ' )) {
60
50
$ mergedParameters = array_merge ($ sessionParameter , $ currentRequestParameter );
61
51
session ([$ this ->sessionKey => $ mergedParameters ]);
52
+
62
53
return $ mergedParameters ;
63
54
}
64
55
@@ -67,27 +58,21 @@ public function useRequestOrSession(Request $request)
67
58
68
59
/**
69
60
* Retrieve all UTM-Parameter.
70
- *
71
- * @return array
72
61
*/
73
- public function all ()
62
+ public function all (): array
74
63
{
75
64
return session ($ this ->sessionKey ) ?? [];
76
65
}
77
66
78
67
/**
79
68
* Retrieve a UTM-Parameter by key.
80
- *
81
- * @param string $key
82
- *
83
- * @return string|null
84
69
*/
85
- public function get (string $ key )
70
+ public function get (string $ key ): ? string
86
71
{
87
72
$ parameters = $ this ->all ();
88
73
$ key = $ this ->ensureUtmPrefix ($ key );
89
74
90
- if (!array_key_exists ($ key , $ parameters )) {
75
+ if (! array_key_exists ($ key , $ parameters )) {
91
76
return null ;
92
77
}
93
78
@@ -97,17 +82,14 @@ public function get(string $key)
97
82
/**
98
83
* Determine if a UTM-Parameter exists.
99
84
*
100
- * @param string $key
101
- * @param string $value
102
- *
103
- * @return bool
85
+ * @param string $value
104
86
*/
105
- public function has (string $ key , $ value = null )
87
+ public function has (string $ key , $ value = null ): bool
106
88
{
107
89
$ parameters = $ this ->all ();
108
90
$ key = $ this ->ensureUtmPrefix ($ key );
109
91
110
- if (!array_key_exists ($ key , $ parameters )) {
92
+ if (! array_key_exists ($ key , $ parameters )) {
111
93
return false ;
112
94
}
113
95
@@ -120,17 +102,13 @@ public function has(string $key, $value = null)
120
102
121
103
/**
122
104
* Determine if a value contains inside the key.
123
- *
124
- * @param string $key
125
- * @param string $value
126
- * @return bool
127
105
*/
128
- public function contains (string $ key , string $ value )
106
+ public function contains (string $ key , string $ value ): bool
129
107
{
130
108
$ parameters = $ this ->all ();
131
109
$ key = $ this ->ensureUtmPrefix ($ key );
132
110
133
- if (!array_key_exists ($ key , $ parameters ) || !is_string ($ value )) {
111
+ if (! array_key_exists ($ key , $ parameters ) || ! is_string ($ value )) {
134
112
return false ;
135
113
}
136
114
@@ -139,44 +117,38 @@ public function contains(string $key, string $value)
139
117
140
118
/**
141
119
* Clear and remove utm session.
142
- *
143
- * @return bool
144
120
*/
145
- public function clear ()
121
+ public function clear (): bool
146
122
{
147
123
session ()->forget ($ this ->sessionKey );
148
124
$ this ->parameters = null ;
125
+
149
126
return true ;
150
127
}
151
128
152
129
/**
153
130
* Retrieve all UTM-Parameter from the URI.
154
- *
155
- * @return array
156
131
*/
157
- protected function getParameter (Request $ request )
132
+ protected function getParameter (Request $ request ): array
158
133
{
159
134
$ allowedKeys = config ('utm-parameter.allowed_utm_parameters ' , [
160
- 'utm_source ' , 'utm_medium ' , 'utm_campaign ' , 'utm_term ' , 'utm_content '
135
+ 'utm_source ' , 'utm_medium ' , 'utm_campaign ' , 'utm_term ' , 'utm_content ' ,
161
136
]);
162
137
163
138
return collect ($ request ->all ())
164
139
->filter (fn ($ value , $ key ) => substr ($ key , 0 , 4 ) === 'utm_ ' )
165
140
->filter (fn ($ value , $ key ) => in_array ($ key , $ allowedKeys ))
166
141
->mapWithKeys (fn ($ value , $ key ) => [
167
- htmlspecialchars ($ key , ENT_QUOTES , 'UTF-8 ' ) => htmlspecialchars ($ value , ENT_QUOTES , 'UTF-8 ' )
142
+ htmlspecialchars ($ key , ENT_QUOTES , 'UTF-8 ' ) => htmlspecialchars ($ value , ENT_QUOTES , 'UTF-8 ' ),
168
143
])
169
144
->toArray ();
170
145
}
171
146
172
147
/**
173
148
* Ensure the key to start with 'utm_'.
174
- *
175
- * @param string $key
176
- * @return string
177
149
*/
178
150
protected function ensureUtmPrefix (string $ key ): string
179
151
{
180
- return str_starts_with ($ key , 'utm_ ' ) ? $ key : 'utm_ ' . $ key ;
152
+ return str_starts_with ($ key , 'utm_ ' ) ? $ key : 'utm_ ' . $ key ;
181
153
}
182
154
}
0 commit comments