Skip to content

Commit

Permalink
added: node traveler, property method transform system
Browse files Browse the repository at this point in the history
  • Loading branch information
nahid committed Jul 28, 2020
1 parent 85d2ebb commit c2744ae
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
7 changes: 4 additions & 3 deletions helpers/presento.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ function to_camel_case(string $string) : string
*
* @param $map
* @param string $node
* @param string $nodeTraveler
* @return mixed|null
*/
function get_from_array($map, string $node)
function get_from_array($map, string $node, $nodeTraveler = '.')
{
if ($map === null || !is_array($map) || empty($node) || $node == '.') {
if ($map === null || !is_array($map) || empty($node) || $node == $nodeTraveler) {
return $map;
}

$path = explode('.', $node);
$path = explode($nodeTraveler, $node);

foreach ($path as $val) {
if (!is_array($map)) {
Expand Down
28 changes: 23 additions & 5 deletions src/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ abstract class Presenter
*/
protected $isProcessed = false;

public function __construct($data = null, string $transformer = null)
/**
* @var string
*/
protected $traveler;

public function __construct($data = null, string $transformer = null, $nodeTraveler = '.')
{
$this->traveler = $nodeTraveler;
$this->presentScheme = $this->present();
$this->data = $this->init($data);

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

abstract public function present() : array;

public function __invoke()
{
return $this->get();
Expand Down Expand Up @@ -103,7 +111,17 @@ public function setDefault($value) : self
return $this;
}

abstract public function present() : array;
public function setNodeTraveler(string $traveler) : self
{
$this->traveler = $traveler;

return $this;
}

public function getNodeTraveler() : string
{
return $this->traveler;
}

/**
* get transformer name, this method can be override
Expand Down Expand Up @@ -205,7 +223,7 @@ public function process($data)
$params = $value[$class];
$arrData = array_shift($params) ?? '.';
$transformer = array_shift($params);
$args = [get_from_array($data, $arrData), $transformer] + $params;
$args = [get_from_array($data, $arrData, $this->getNodeTraveler()), $transformer] + $params;

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

$record[$key] = $newVal;
} else {
$record[$key] = $value ? get_from_array($data, $value) : $value;
$record[$key] = $value ? get_from_array($data, $value, $this->getNodeTraveler()) : $value;
}
}

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

if (!is_null($transformerClass)) {
$transformer = new $transformerClass($data);
$transformer = new $transformerClass($data, $this->getNodeTraveler());
return $transformer();
}

Expand Down
21 changes: 13 additions & 8 deletions src/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ abstract class Transformer
/**
* @var null | string
*/
protected $propertyMethodTransform = 'to_studly_case';
protected $propertyMethodTransformAs = 'to_studly_case';
private $data = [];
/**
* @var string
*/
protected $traveler;

public function __construct(array $data)
public function __construct(array $data, string $nodeTraveler = '.')
{
$this->traveler = $nodeTraveler;
$this->data = $data;
$this->transform();
}
Expand Down Expand Up @@ -69,15 +74,15 @@ protected function getPropertyFunction(string $property) : string
*/
protected function propertyMethodTransform($property)
{
if (!$this->propertyMethodTransform) {
return $property;
if (!$this->propertyMethodTransformAs) {
return ucfirst($property);
}

if(function_exists($this->propertyMethodTransform)) {
return call_user_func($this->propertyMethodTransform, $property);
if(function_exists($this->propertyMethodTransformAs)) {
return call_user_func($this->propertyMethodTransformAs, $property);
}

throw new BadPropertyTransformerMethodException($this->propertyMethodTransform);
throw new BadPropertyTransformerMethodException($this->propertyMethodTransformAs);
}

/**
Expand All @@ -104,7 +109,7 @@ protected function callPropertyFunction(string $property, $value)
*/
public function getProperty(string $property)
{
return get_from_array($this->data, $property);
return get_from_array($this->data, $property, $this->traveler);
}

/**
Expand Down

0 comments on commit c2744ae

Please sign in to comment.