Skip to content

Commit

Permalink
fixed default value issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nahid committed Aug 27, 2019
1 parent f9a4d69 commit e8be38f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
5 changes: 5 additions & 0 deletions helpers/presento.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ function to_camel_case(string $string, $delimiter = '_') : string
if (!function_exists('get_from_array')) {
function get_from_array($map, string $node)
{
if ($map === null) {
return $map;
}

if (!is_array($map)) {
return $map;
}

if (empty($node)) {
return $map;
}

if ($node) {
$terminate = false;
$path = explode('.', $node);
Expand Down
30 changes: 18 additions & 12 deletions src/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct($data = null, string $transformer = null)
$this->generatedData = $this->handle();
}

public function __invoke() : array
public function __invoke()
{
return $this->generatedData;
}
Expand All @@ -36,6 +36,19 @@ public function __toString() : string
return json_encode($this->generatedData);
}

public function setPresent(array $present)
{
$this->presentScheme = $present;
return $this;
}


public function setTransformer(string $transformer)
{
$this->transformer = $transformer;
return $this;
}

abstract public function present() : array;

/**
Expand Down Expand Up @@ -63,13 +76,7 @@ public function handle()
if (is_collection($this->data)) {
$generatedData = [];
foreach ($this->data as $property => $data) {
if (!$this->isBlank($data)) {
$generatedData[$property] = $this->transform($this->process($this->convert($data)));
}

if ($this->isBlank($data)) {
$generatedData[$property] = $this->handleDefault($this->convert($data));
}
$generatedData[$property] = $this->handleDefault($this->convert($data));
}

return $generatedData;
Expand All @@ -80,12 +87,11 @@ public function handle()

protected function handleDefault($data)
{

if (is_null($this->default) || $this->default == '') {
return $this->default;
if (!$this->isBlank($data)) {
return $this->transform($this->process($data));
}

if (is_array($this->default) && count($this->default)>0) {
if (is_array($this->default) && count($this->default) > 0) {
$this->presentScheme = $this->default;
return $this->transform($this->process($data));
}
Expand Down

0 comments on commit e8be38f

Please sign in to comment.