Skip to content

Commit c2744ae

Browse files
committed
added: node traveler, property method transform system
1 parent 85d2ebb commit c2744ae

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

helpers/presento.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ function to_camel_case(string $string) : string
3737
*
3838
* @param $map
3939
* @param string $node
40+
* @param string $nodeTraveler
4041
* @return mixed|null
4142
*/
42-
function get_from_array($map, string $node)
43+
function get_from_array($map, string $node, $nodeTraveler = '.')
4344
{
44-
if ($map === null || !is_array($map) || empty($node) || $node == '.') {
45+
if ($map === null || !is_array($map) || empty($node) || $node == $nodeTraveler) {
4546
return $map;
4647
}
4748

48-
$path = explode('.', $node);
49+
$path = explode($nodeTraveler, $node);
4950

5051
foreach ($path as $val) {
5152
if (!is_array($map)) {

src/Presenter.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ abstract class Presenter
3636
*/
3737
protected $isProcessed = false;
3838

39-
public function __construct($data = null, string $transformer = null)
39+
/**
40+
* @var string
41+
*/
42+
protected $traveler;
43+
44+
public function __construct($data = null, string $transformer = null, $nodeTraveler = '.')
4045
{
46+
$this->traveler = $nodeTraveler;
4147
$this->presentScheme = $this->present();
4248
$this->data = $this->init($data);
4349

@@ -47,6 +53,8 @@ public function __construct($data = null, string $transformer = null)
4753
}
4854
}
4955

56+
abstract public function present() : array;
57+
5058
public function __invoke()
5159
{
5260
return $this->get();
@@ -103,7 +111,17 @@ public function setDefault($value) : self
103111
return $this;
104112
}
105113

106-
abstract public function present() : array;
114+
public function setNodeTraveler(string $traveler) : self
115+
{
116+
$this->traveler = $traveler;
117+
118+
return $this;
119+
}
120+
121+
public function getNodeTraveler() : string
122+
{
123+
return $this->traveler;
124+
}
107125

108126
/**
109127
* get transformer name, this method can be override
@@ -205,7 +223,7 @@ public function process($data)
205223
$params = $value[$class];
206224
$arrData = array_shift($params) ?? '.';
207225
$transformer = array_shift($params);
208-
$args = [get_from_array($data, $arrData), $transformer] + $params;
226+
$args = [get_from_array($data, $arrData, $this->getNodeTraveler()), $transformer] + $params;
209227

210228
$presenter = new $class(... $args);
211229
$newVal = $value;
@@ -215,7 +233,7 @@ public function process($data)
215233

216234
$record[$key] = $newVal;
217235
} else {
218-
$record[$key] = $value ? get_from_array($data, $value) : $value;
236+
$record[$key] = $value ? get_from_array($data, $value, $this->getNodeTraveler()) : $value;
219237
}
220238
}
221239

@@ -237,7 +255,7 @@ protected function transform($data)
237255
$transformerClass = $this->transformer;
238256

239257
if (!is_null($transformerClass)) {
240-
$transformer = new $transformerClass($data);
258+
$transformer = new $transformerClass($data, $this->getNodeTraveler());
241259
return $transformer();
242260
}
243261

src/Transformer.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ abstract class Transformer
1111
/**
1212
* @var null | string
1313
*/
14-
protected $propertyMethodTransform = 'to_studly_case';
14+
protected $propertyMethodTransformAs = 'to_studly_case';
1515
private $data = [];
16+
/**
17+
* @var string
18+
*/
19+
protected $traveler;
1620

17-
public function __construct(array $data)
21+
public function __construct(array $data, string $nodeTraveler = '.')
1822
{
23+
$this->traveler = $nodeTraveler;
1924
$this->data = $data;
2025
$this->transform();
2126
}
@@ -69,15 +74,15 @@ protected function getPropertyFunction(string $property) : string
6974
*/
7075
protected function propertyMethodTransform($property)
7176
{
72-
if (!$this->propertyMethodTransform) {
73-
return $property;
77+
if (!$this->propertyMethodTransformAs) {
78+
return ucfirst($property);
7479
}
7580

76-
if(function_exists($this->propertyMethodTransform)) {
77-
return call_user_func($this->propertyMethodTransform, $property);
81+
if(function_exists($this->propertyMethodTransformAs)) {
82+
return call_user_func($this->propertyMethodTransformAs, $property);
7883
}
7984

80-
throw new BadPropertyTransformerMethodException($this->propertyMethodTransform);
85+
throw new BadPropertyTransformerMethodException($this->propertyMethodTransformAs);
8186
}
8287

8388
/**
@@ -104,7 +109,7 @@ protected function callPropertyFunction(string $property, $value)
104109
*/
105110
public function getProperty(string $property)
106111
{
107-
return get_from_array($this->data, $property);
112+
return get_from_array($this->data, $property, $this->traveler);
108113
}
109114

110115
/**

0 commit comments

Comments
 (0)