22
33namespace AlexWestergaard \PhpGa4 \Model ;
44
5+ use ArrayAccess ;
56use AlexWestergaard \PhpGa4 \Facade ;
67use AlexWestergaard \PhpGa4 \GA4Exception ;
8+ use AlexWestergaard \PhpGa4 \Helper \Helper ;
79
8- abstract class Event extends ToArray implements Facade \Export
10+ abstract class Event extends ToArray implements Facade \Export, ArrayAccess
911{
1012 private $ debug = false ;
1113
@@ -19,10 +21,60 @@ abstract public function getName(): string;
1921 public function debug ($ on = true )
2022 {
2123 $ this ->debug = boolval ($ on );
22-
24+
2325 return $ this ;
2426 }
2527
28+ public function offsetExists (mixed $ offset ): bool
29+ {
30+ return property_exists ($ this , Helper::snake ($ offset ));
31+ }
32+
33+ public function offsetGet (mixed $ offset ): mixed
34+ {
35+ $ callable = Helper::snake ($ offset );
36+ return $ this ->offsetExists ($ callable ) ? $ this ->$ callable : null ;
37+ }
38+
39+ public function offsetSet (mixed $ offset , mixed $ value ): void
40+ {
41+ $ callable = Helper::camel ($ offset );
42+ if ($ this ->offsetExists ($ offset )) {
43+ $ this ->$ callable = $ value ;
44+ if (method_exists ($ this , ($ method = 'add ' . $ callable ))) {
45+ $ this ->$ method ($ value );
46+ } elseif (method_exists ($ this , ($ method = 'set ' . $ callable ))) {
47+ $ this ->$ method ($ value );
48+ }
49+
50+ if (is_array ($ value )) {
51+ $ callable = substr ($ callable , -1 ) === 's ' ? substr ($ callable , 0 , -1 ) : $ callable ;
52+
53+ foreach ($ value as $ paramRow ) {
54+ if (method_exists ($ this , ($ method = 'add ' . $ callable ))) {
55+ $ this ->$ method ($ paramRow );
56+ } elseif (method_exists ($ this , ($ method = 'set ' . $ callable ))) {
57+ $ this ->$ method ($ paramRow );
58+ }
59+ }
60+ } else {
61+ if (method_exists ($ this , ($ method = 'add ' . $ callable ))) {
62+ $ this ->$ method ($ value );
63+ } elseif (method_exists ($ this , ($ method = 'set ' . $ callable ))) {
64+ $ this ->$ method ($ value );
65+ }
66+ }
67+ }
68+ }
69+
70+ public function offsetUnset (mixed $ offset ): void
71+ {
72+ $ var = Helper::snake ($ offset );
73+ if ($ this ->offsetExists ($ offset )) {
74+ $ this ->$ var = null ;
75+ }
76+ }
77+
2678 /**
2779 * @param GA4Exception $childErrors
2880 */
@@ -31,7 +83,7 @@ public function toArray(bool $isParent = false): array
3183 $ return = [];
3284
3385 if (!method_exists ($ this , 'getName ' )) {
34- GA4Exception::push ("'self:: getName()' does not exist " );
86+ GA4Exception::push ("'getName()' does not exist " );
3587 } else {
3688 $ name = $ this ->getName ();
3789 if (empty ($ name )) {
@@ -40,30 +92,7 @@ public function toArray(bool $isParent = false): array
4092 GA4Exception::push ("Name ' {$ name }' can not be longer than 40 characters " );
4193 } elseif (preg_match ('/[^\w\d\-]/ ' , $ name )) {
4294 GA4Exception::push ("Name ' {$ name }' can only be letters, numbers, underscores and dashes " );
43- } elseif (in_array ($ name , [
44- 'ad_activeview ' ,
45- 'ad_click ' ,
46- 'ad_exposure ' ,
47- 'ad_impression ' ,
48- 'ad_query ' ,
49- 'adunit_exposure ' ,
50- 'app_clear_data ' ,
51- 'app_install ' ,
52- 'app_update ' ,
53- 'app_remove ' ,
54- 'error ' ,
55- 'first_open ' ,
56- 'first_visit ' ,
57- 'in_app_purchase ' ,
58- 'notification_dismiss ' ,
59- 'notification_foreground ' ,
60- 'notification_open ' ,
61- 'notification_receive ' ,
62- 'os_update ' ,
63- 'screen_view ' ,
64- 'session_start ' ,
65- 'user_engagement ' ,
66- ])) {
95+ } elseif (in_array ($ name , Helper::RESERVED_EVENT_NAMES )) {
6796 GA4Exception::push ("Name ' {$ name }' is reserved " );
6897 } else {
6998 $ return ['name ' ] = $ name ;
@@ -98,7 +127,7 @@ public static function fromArray(array $params = [])
98127 continue ;
99128 }
100129
101- $ callableName = implode ( '' , array_map ( ' ucfirst ' , explode ( ' _ ' , $ insertable)) );
130+ $ callableName = Helper:: camel ( $ insertable );
102131
103132 if (is_array ($ param )) {
104133 $ callableName = substr ($ callableName , -1 ) === 's ' ? substr ($ callableName , 0 , -1 ) : $ callableName ;
@@ -117,6 +146,7 @@ public static function fromArray(array $params = [])
117146 }
118147 }
119148 }
149+
120150 return $ event ;
121151 }
122152
0 commit comments