1010namespace MaplePHP \DTO ;
1111
1212use MaplePHP \DTO \Format ;
13+ use ReflectionClass ;
14+ use InvalidArgumentException ;
1315
1416class Traverse extends DynamicDataAbstract
1517{
@@ -55,6 +57,47 @@ public function getRaw()
5557 {
5658 return $ this ->raw ;
5759 }
60+
61+ /**
62+ * Add a data to new object column/name
63+ * @param string $columnName The new column name
64+ * @param mixed $value The added value
65+ */
66+ public function add (string $ columnName , mixed $ value ): self
67+ {
68+ $ this ->{$ columnName } = $ value ;
69+ return $ this ;
70+ }
71+
72+ /**
73+ * Combine multiple objects at the same level
74+ * @param string $columnName The new column name
75+ * @param array $columns Columns to be combine
76+ * @param string $sep Comibining seperator (default is a space)
77+ * @return self
78+ */
79+ public function combine (...$ spread ): self
80+ {
81+ $ mixedA = isset ($ spread [0 ]) ? $ spread [0 ] : null ;
82+ $ mixedB = isset ($ spread [1 ]) ? $ spread [1 ] : null ;
83+ $ columns = (is_array ($ mixedA )) ? $ mixedA : $ mixedB ;
84+ $ columnName = (is_string ($ mixedA )) ? $ mixedA : null ;
85+ $ sep = (isset ($ spread [2 ]) && is_string ($ spread [2 ]) ? $ spread [2 ] : ((is_string ($ mixedB )) ? $ mixedB : " " ));
86+
87+ $ new = array ();
88+ foreach ($ columns as $ colKey ) {
89+ $ new [] = (string )$ this ->{$ colKey };
90+ }
91+
92+ $ value = implode ($ sep , $ new );
93+ if (!is_null ($ columnName )) {
94+ $ this ->{$ columnName } = $ value ;
95+ } else {
96+ $ this ->row = $ value ;
97+ }
98+
99+ return $ this ;
100+ }
58101
59102
60103 /**
@@ -160,6 +203,23 @@ public function sprint(string $add)
160203 return $ this ;
161204 }
162205
206+ /**
207+ * Access and return format class object
208+ * @param string $dtoClassName The DTO format class name
209+ * @return object
210+ */
211+ public function format (string $ dtoClassName ): object
212+ {
213+ $ name = ucfirst ($ dtoClassName );
214+ $ className = "MaplePHP \\DTO \\Format \\{$ name }" ;
215+ if (!class_exists ($ className )) {
216+ throw new InvalidArgumentException ("The DTO Format class do not exist! " , 1 );
217+ }
218+ $ reflect = new ReflectionClass ($ className );
219+ $ instance = $ reflect ->newInstanceWithoutConstructor ();
220+ return $ instance ->value ($ this ->row );
221+ }
222+
163223 /**
164224 * Traverse factory
165225 * If you want
@@ -171,14 +231,7 @@ public function __call($method, $args)
171231 $ this ->raw = $ this ->row ;
172232
173233 if (count ($ args ) > 0 ) {
174- $ name = ucfirst ($ args [0 ]);
175- $ className = "MaplePHP \\DTO \\Format \\{$ name }" ;
176- if (!class_exists ($ className )) {
177- throw new \Exception ("The DTO Format class do not exist! " , 1 );
178- }
179- $ reflect = new \ReflectionClass ($ className );
180- $ instance = $ reflect ->newInstanceWithoutConstructor ();
181- return $ instance ->value ($ this ->row );
234+ return $ this ->format ($ args [0 ]);
182235 }
183236
184237 if (is_array ($ this ->row ) || is_object ($ this ->row )) {
0 commit comments