Skip to content

Commit e8be38f

Browse files
committed
fixed default value issues
1 parent f9a4d69 commit e8be38f

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

helpers/presento.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ function to_camel_case(string $string, $delimiter = '_') : string
2727
if (!function_exists('get_from_array')) {
2828
function get_from_array($map, string $node)
2929
{
30+
if ($map === null) {
31+
return $map;
32+
}
33+
3034
if (!is_array($map)) {
3135
return $map;
3236
}
3337

3438
if (empty($node)) {
3539
return $map;
3640
}
41+
3742
if ($node) {
3843
$terminate = false;
3944
$path = explode('.', $node);

src/Presenter.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct($data = null, string $transformer = null)
2626
$this->generatedData = $this->handle();
2727
}
2828

29-
public function __invoke() : array
29+
public function __invoke()
3030
{
3131
return $this->generatedData;
3232
}
@@ -36,6 +36,19 @@ public function __toString() : string
3636
return json_encode($this->generatedData);
3737
}
3838

39+
public function setPresent(array $present)
40+
{
41+
$this->presentScheme = $present;
42+
return $this;
43+
}
44+
45+
46+
public function setTransformer(string $transformer)
47+
{
48+
$this->transformer = $transformer;
49+
return $this;
50+
}
51+
3952
abstract public function present() : array;
4053

4154
/**
@@ -63,13 +76,7 @@ public function handle()
6376
if (is_collection($this->data)) {
6477
$generatedData = [];
6578
foreach ($this->data as $property => $data) {
66-
if (!$this->isBlank($data)) {
67-
$generatedData[$property] = $this->transform($this->process($this->convert($data)));
68-
}
69-
70-
if ($this->isBlank($data)) {
71-
$generatedData[$property] = $this->handleDefault($this->convert($data));
72-
}
79+
$generatedData[$property] = $this->handleDefault($this->convert($data));
7380
}
7481

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

8188
protected function handleDefault($data)
8289
{
83-
84-
if (is_null($this->default) || $this->default == '') {
85-
return $this->default;
90+
if (!$this->isBlank($data)) {
91+
return $this->transform($this->process($data));
8692
}
8793

88-
if (is_array($this->default) && count($this->default)>0) {
94+
if (is_array($this->default) && count($this->default) > 0) {
8995
$this->presentScheme = $this->default;
9096
return $this->transform($this->process($data));
9197
}

0 commit comments

Comments
 (0)