77
88abstract class Event extends ToArray implements Facade \Export
99{
10+ private $ debug = false ;
11+
1012 /**
1113 * Return the name of the event
1214 *
1315 * @return string
1416 */
1517 abstract public function getName (): string ;
1618
17- public static function fromArray ( array $ params = [] )
19+ public function debug ( $ on = true )
1820 {
19- $ event = static ::new ();
20-
21- $ insertables = array_unique (array_merge ($ event ->getParams (), $ event ->getRequiredParams ()));
22-
23- foreach ($ insertables as $ insertable ) {
24- if (!in_array ($ insertable , array_keys ($ params )) || is_null ($ param = $ params [$ insertable ])) {
25- continue ;
26- }
27-
28- $ callableName = implode ('' , array_map ('ucfirst ' , explode ('_ ' , $ insertable )));
29-
30- if (is_array ($ param )) {
31- $ callableName = substr ($ callableName , -1 ) === 's ' ? substr ($ callableName , 0 , -1 ) : $ callableName ;
32- foreach ($ param as $ paramRow ) {
33- if (method_exists ($ event , ($ method = 'add ' . $ callableName ))) {
34- $ event ->$ method ($ paramRow );
35- } elseif (method_exists ($ event , ($ method = 'set ' . $ callableName ))) {
36- $ event ->$ method ($ paramRow );
37- }
38- }
39- } else {
40- if (method_exists ($ event , ($ method = 'add ' . $ callableName ))) {
41- $ event ->$ method ($ param );
42- } elseif (method_exists ($ event , ($ method = 'set ' . $ callableName ))) {
43- $ event ->$ method ($ param );
44- }
45- }
46- }
47- return $ event ;
21+ $ this ->debug = boolval ($ on );
22+
23+ return $ this ;
4824 }
4925
5026 /**
@@ -96,6 +72,10 @@ public function toArray(bool $isParent = false): array
9672
9773 $ parent = parent ::toArray (true );
9874
75+ if ($ this ->debug ) {
76+ $ parent ['debug_mode ' ] = true ;
77+ }
78+
9979 if (!$ isParent && GA4Exception::hasStack ()) {
10080 throw GA4Exception::getFinalStack ();
10181 }
@@ -107,6 +87,39 @@ public function toArray(bool $isParent = false): array
10787 return $ return ;
10888 }
10989
90+ public static function fromArray (array $ params = [])
91+ {
92+ $ event = static ::new ();
93+
94+ $ insertables = array_unique (array_merge ($ event ->getParams (), $ event ->getRequiredParams ()));
95+
96+ foreach ($ insertables as $ insertable ) {
97+ if (!in_array ($ insertable , array_keys ($ params )) || is_null ($ param = $ params [$ insertable ])) {
98+ continue ;
99+ }
100+
101+ $ callableName = implode ('' , array_map ('ucfirst ' , explode ('_ ' , $ insertable )));
102+
103+ if (is_array ($ param )) {
104+ $ callableName = substr ($ callableName , -1 ) === 's ' ? substr ($ callableName , 0 , -1 ) : $ callableName ;
105+ foreach ($ param as $ paramRow ) {
106+ if (method_exists ($ event , ($ method = 'add ' . $ callableName ))) {
107+ $ event ->$ method ($ paramRow );
108+ } elseif (method_exists ($ event , ($ method = 'set ' . $ callableName ))) {
109+ $ event ->$ method ($ paramRow );
110+ }
111+ }
112+ } else {
113+ if (method_exists ($ event , ($ method = 'add ' . $ callableName ))) {
114+ $ event ->$ method ($ param );
115+ } elseif (method_exists ($ event , ($ method = 'set ' . $ callableName ))) {
116+ $ event ->$ method ($ param );
117+ }
118+ }
119+ }
120+ return $ event ;
121+ }
122+
110123 public static function new ()
111124 {
112125 return new static ();
0 commit comments