Skip to content

Commit 501c7ba

Browse files
author
Wazabii
committed
New traverse methods
1 parent 2bf72c7 commit 501c7ba

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed

Format/FormatAbstract.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace MaplePHP\DTO\Format;
1111

12+
use ReflectionClass;
13+
1214
abstract class FormatAbstract
1315
{
1416
protected $value;
@@ -70,4 +72,21 @@ public function sprint(string $add)
7072
}
7173
return $this;
7274
}
75+
76+
/**
77+
* Access and return format class object
78+
* @param string $dtoClassName The DTO format class name
79+
* @return object
80+
*/
81+
public function format(string $dtoClassName): object
82+
{
83+
$name = ucfirst($dtoClassName);
84+
$className = "MaplePHP\\DTO\\Format\\{$name}";
85+
if (!class_exists($className)) {
86+
throw new InvalidArgumentException("The DTO Format class do not exist!", 1);
87+
}
88+
$reflect = new ReflectionClass($className);
89+
$instance = $reflect->newInstanceWithoutConstructor();
90+
return $instance->value($this->value);
91+
}
7392
}

Traverse.php

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace MaplePHP\DTO;
1111

1212
use MaplePHP\DTO\Format;
13+
use ReflectionClass;
14+
use InvalidArgumentException;
1315

1416
class 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

Comments
 (0)