Skip to content

Commit f9a4d69

Browse files
committed
added: default value for presenter
1 parent 3284f8d commit f9a4d69

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

helpers/presento.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ function to_camel_case(string $string, $delimiter = '_') : string
2525
}
2626

2727
if (!function_exists('get_from_array')) {
28-
function get_from_array(array $map, string $node)
28+
function get_from_array($map, string $node)
2929
{
30+
if (!is_array($map)) {
31+
return $map;
32+
}
33+
3034
if (empty($node)) {
3135
return $map;
3236
}
@@ -57,7 +61,7 @@ function get_from_array(array $map, string $node)
5761
* @param array $arr
5862
* @return bool
5963
*/
60-
function is_collection(array $arr) : bool
64+
function is_collection($arr) : bool
6165
{
6266
if (!is_array($arr)) {
6367
return false;

src/Presenter.php

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
namespace Nahid\Presento;
44

55

6+
use Countable;
7+
68
abstract class Presenter
79
{
810
protected $transformer = null;
911
protected $data = [];
1012
protected $generatedData = [];
13+
protected $default = null;
14+
protected $presentScheme;
1115

12-
public function __construct(array $data, string $transformer = null)
16+
public function __construct($data = null, string $transformer = null)
1317
{
14-
$this->data = $data;
18+
$this->presentScheme = $this->present();
19+
$this->data = $this->convert($data);
20+
1521
$this->transformer = $this->transformer();
1622
if (!is_null($transformer)) {
1723
$this->transformer = $transformer;
@@ -42,23 +48,49 @@ public function transformer()
4248
return null;
4349
}
4450

51+
public function convert($data)
52+
{
53+
return $data;
54+
}
55+
4556
/**
4657
* handle data based on presented data
4758
*
4859
* @return array
4960
*/
50-
public function handle() : array
61+
public function handle()
5162
{
5263
if (is_collection($this->data)) {
5364
$generatedData = [];
5465
foreach ($this->data as $property => $data) {
55-
$generatedData[$property] = $this->transform($this->process($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+
}
5673
}
5774

5875
return $generatedData;
5976
}
6077

61-
return $this->transform($this->process($this->data));
78+
return $this->handleDefault($this->convert($this->data));
79+
}
80+
81+
protected function handleDefault($data)
82+
{
83+
84+
if (is_null($this->default) || $this->default == '') {
85+
return $this->default;
86+
}
87+
88+
if (is_array($this->default) && count($this->default)>0) {
89+
$this->presentScheme = $this->default;
90+
return $this->transform($this->process($data));
91+
}
92+
93+
return $this->default;
6294
}
6395

6496
/**
@@ -67,9 +99,9 @@ public function handle() : array
6799
* @param array $data
68100
* @return array
69101
*/
70-
public function process(array $data) : array
102+
public function process($data)
71103
{
72-
$present = $this->present();
104+
$present = $this->presentScheme;
73105
$record = [];
74106

75107
if (count($present) == 0) {
@@ -107,8 +139,12 @@ public function process(array $data) : array
107139
* @param array $data
108140
* @return array
109141
*/
110-
protected function transform(array $data) : array
142+
protected function transform($data)
111143
{
144+
if (!is_array($data)) {
145+
return $data;
146+
}
147+
112148
$transformerClass = $this->transformer;
113149

114150
if (!is_null($transformerClass)) {
@@ -138,4 +174,25 @@ public function get() : array
138174
{
139175
return $this->generatedData;
140176
}
177+
178+
public function isBlank($value)
179+
{
180+
if (is_null($value)) {
181+
return true;
182+
}
183+
184+
if (is_string($value)) {
185+
return trim($value) === '';
186+
}
187+
188+
if (is_numeric($value) || is_bool($value)) {
189+
return false;
190+
}
191+
192+
if ($value instanceof Countable) {
193+
return count($value) === 0;
194+
}
195+
196+
return empty($value);
197+
}
141198
}

0 commit comments

Comments
 (0)