|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the HRis Software package. |
|
5
|
|
|
* |
|
6
|
|
|
* HRis - Human Resource and Payroll System |
|
7
|
|
|
* |
|
8
|
|
|
* @link http://github.com/HB-Co/HRis |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace HRis\Api\Transformers; |
|
11
|
|
|
|
|
12
|
|
|
use League\Fractal\TransformerAbstract; |
|
13
|
|
|
|
|
14
|
|
|
class BaseTransformer extends TransformerAbstract |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
private $validParams = ['order']; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Base Collection transformer with params. |
|
23
|
|
|
* |
|
24
|
|
|
* @param $model |
|
25
|
|
|
* @param $transformer |
|
26
|
|
|
* @param $params |
|
27
|
|
|
* |
|
28
|
|
|
* @return \League\Fractal\Resource\Collection |
|
29
|
|
|
* |
|
30
|
|
|
* @throws \Exception |
|
31
|
|
|
* |
|
32
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
public function transformCollection($model, $transformer, $params) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($params === null) { |
|
37
|
|
|
return $this->collection($model, $transformer); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// Optional params validation |
|
41
|
|
|
$usedParams = array_keys(iterator_to_array($params)); |
|
42
|
|
|
if ($invalidParams = array_diff($usedParams, $this->validParams)) { |
|
|
|
|
|
|
43
|
|
|
throw new \Exception(sprintf( |
|
44
|
|
|
'Invalid param(s): "%s". Valid param(s): "%s"', |
|
45
|
|
|
implode(',', $usedParams), |
|
46
|
|
|
implode(',', $this->validParams) |
|
47
|
|
|
)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Processing |
|
51
|
|
|
list($orderCol, $orderBy) = $params->get('order'); |
|
52
|
|
|
|
|
53
|
|
|
$model = $model |
|
54
|
|
|
->orderBy($orderCol, $orderBy) |
|
55
|
|
|
->get(); |
|
56
|
|
|
|
|
57
|
|
|
return $this->collection($model, $transformer); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.