Skip to content

Commit 2f7deb9

Browse files
committed
added code commenting
1 parent f68d3a9 commit 2f7deb9

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/Presenter.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,27 @@ public function __toString() : string
2727

2828
abstract public function present() : array;
2929

30+
/**
31+
* get transformer name, this method can be override
32+
*
33+
* @return null|string
34+
*/
3035
public function transformer()
3136
{
3237
return null;
3338
}
3439

35-
public function handle()
40+
/**
41+
* handle data based on presented data
42+
*
43+
* @return array
44+
*/
45+
public function handle() : array
3646
{
3747
if ($this->isCollection($this->data)) {
3848
$generatedData = [];
3949
foreach ($this->data as $property => $data) {
40-
$generatedData[] = $this->transform($this->process($data));
50+
$generatedData[$property] = $this->transform($this->process($data));
4151
}
4252

4353
return $generatedData;
@@ -46,6 +56,12 @@ public function handle()
4656
return $this->transform($this->process($this->data));
4757
}
4858

59+
/**
60+
* process data based presented data model
61+
*
62+
* @param array $data
63+
* @return array
64+
*/
4965
public function process(array $data) : array
5066
{
5167
$present = $this->present();
@@ -72,6 +88,12 @@ public function process(array $data) : array
7288
return $record;
7389
}
7490

91+
/**
92+
* transform given data based on transformer.
93+
*
94+
* @param array $data
95+
* @return array
96+
*/
7597
protected function transform(array $data) : array
7698
{
7799
$transformerClass = $this->transformer();
@@ -84,11 +106,21 @@ protected function transform(array $data) : array
84106
return $data;
85107
}
86108

109+
/**
110+
* get generated data as json string
111+
*
112+
* @return string
113+
*/
87114
public function toJson() : string
88115
{
89116
return json_encode($this->generatedData);
90117
}
91118

119+
/**
120+
* get full set of data as array
121+
*
122+
* @return array
123+
*/
92124
public function get() : array
93125
{
94126
return $this->generatedData;
@@ -108,6 +140,4 @@ protected function isCollection(array $arr) : bool
108140

109141
return isset($arr[0]) && is_array($arr[0]);
110142
}
111-
112-
113143
}

src/Transformer.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,54 @@ public function __construct(array $data)
1414
$this->transform();
1515
}
1616

17-
public function __invoke() : array
17+
public function __invoke() : array
1818
{
1919
return $this->getData();
2020
}
2121

22+
/**
23+
* transform given data with desired methods
24+
*
25+
* @return void
26+
*/
2227
public function transform()
2328
{
2429
foreach ($this->data as $key => $value) {
2530
$this->generatedData[$key] = $this->callPropertyFunction($key, $value);
2631
}
2732
}
2833

34+
/**
35+
* check then current property need to be processed
36+
*
37+
* @param string $property
38+
* @return bool
39+
*/
2940
protected function isPropertyNeedProcess(string $property) : bool
3041
{
3142
$method = $this->getPropertyFunction($property);
3243

3344
return method_exists($this, $method);
3445
}
3546

47+
/**
48+
* get property guessed function name
49+
*
50+
* @param string $property
51+
* @return string
52+
*/
3653
protected function getPropertyFunction(string $property) : string
3754
{
3855
return 'get'. to_camel_case($property) . 'Property';
3956
}
4057

58+
/**
59+
* call property function if exists
60+
*
61+
* @param string $property
62+
* @param $value
63+
* @return mixed
64+
*/
4165
protected function callPropertyFunction(string $property, $value)
4266
{
4367
if ($this->isPropertyNeedProcess($property)) {
@@ -47,11 +71,22 @@ protected function callPropertyFunction(string $property, $value)
4771
return $value;
4872
}
4973

74+
/**
75+
* get property value from data
76+
*
77+
* @param string $property
78+
* @return array|mixed|null
79+
*/
5080
public function getProperty(string $property)
5181
{
5282
return get_from_array($this->data, $property);
5383
}
5484

85+
/**
86+
* get full set of data
87+
*
88+
* @return array
89+
*/
5590
public function getData() : array
5691
{
5792
return $this->generatedData;

0 commit comments

Comments
 (0)