3
3
namespace Nahid \Presento ;
4
4
5
5
6
+ use Countable ;
7
+
6
8
abstract class Presenter
7
9
{
8
10
protected $ transformer = null ;
9
11
protected $ data = [];
10
12
protected $ generatedData = [];
13
+ protected $ default = null ;
14
+ protected $ presentScheme ;
11
15
12
- public function __construct (array $ data , string $ transformer = null )
16
+ public function __construct ($ data = null , string $ transformer = null )
13
17
{
14
- $ this ->data = $ data ;
18
+ $ this ->presentScheme = $ this ->present ();
19
+ $ this ->data = $ this ->convert ($ data );
20
+
15
21
$ this ->transformer = $ this ->transformer ();
16
22
if (!is_null ($ transformer )) {
17
23
$ this ->transformer = $ transformer ;
@@ -42,23 +48,49 @@ public function transformer()
42
48
return null ;
43
49
}
44
50
51
+ public function convert ($ data )
52
+ {
53
+ return $ data ;
54
+ }
55
+
45
56
/**
46
57
* handle data based on presented data
47
58
*
48
59
* @return array
49
60
*/
50
- public function handle () : array
61
+ public function handle ()
51
62
{
52
63
if (is_collection ($ this ->data )) {
53
64
$ generatedData = [];
54
65
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
+ }
56
73
}
57
74
58
75
return $ generatedData ;
59
76
}
60
77
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 ;
62
94
}
63
95
64
96
/**
@@ -67,9 +99,9 @@ public function handle() : array
67
99
* @param array $data
68
100
* @return array
69
101
*/
70
- public function process (array $ data ) : array
102
+ public function process ($ data )
71
103
{
72
- $ present = $ this ->present () ;
104
+ $ present = $ this ->presentScheme ;
73
105
$ record = [];
74
106
75
107
if (count ($ present ) == 0 ) {
@@ -107,8 +139,12 @@ public function process(array $data) : array
107
139
* @param array $data
108
140
* @return array
109
141
*/
110
- protected function transform (array $ data ) : array
142
+ protected function transform ($ data )
111
143
{
144
+ if (!is_array ($ data )) {
145
+ return $ data ;
146
+ }
147
+
112
148
$ transformerClass = $ this ->transformer ;
113
149
114
150
if (!is_null ($ transformerClass )) {
@@ -138,4 +174,25 @@ public function get() : array
138
174
{
139
175
return $ this ->generatedData ;
140
176
}
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
+ }
141
198
}
0 commit comments