|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 11/27/15 |
|
5
|
|
|
* Time: 7:47 PM. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace NilPortugues\Laravel5\JsonApi\Eloquent; |
|
12
|
|
|
|
|
13
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
15
|
|
|
use NilPortugues\Api\JsonApi\Http\Factory\RequestFactory; |
|
16
|
|
|
use NilPortugues\Laravel5\JsonApi\JsonApiSerializer; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class EloquentHelper. |
|
20
|
|
|
*/ |
|
21
|
|
|
trait EloquentHelper |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param JsonApiSerializer $serializer |
|
25
|
|
|
* @param Builder $builder |
|
26
|
|
|
* |
|
27
|
|
|
* @return Builder |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function paginate(JsonApiSerializer $serializer, Builder $builder) |
|
30
|
|
|
{ |
|
31
|
|
|
self::sort($serializer, $builder, $builder->getModel()); |
|
32
|
|
|
|
|
33
|
|
|
$request = RequestFactory::create(); |
|
34
|
|
|
|
|
35
|
|
|
$builder->paginate( |
|
36
|
|
|
$request->getPage()->size(), |
|
37
|
|
|
self::columns($serializer, $request->getFields()->get()), |
|
38
|
|
|
'page', |
|
39
|
|
|
$request->getPage()->number() |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
return $builder; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param JsonApiSerializer $serializer |
|
47
|
|
|
* @param Builder $builder |
|
48
|
|
|
* @param Model $model |
|
49
|
|
|
* |
|
50
|
|
|
* @return Builder |
|
51
|
|
|
*/ |
|
52
|
|
|
protected static function sort(JsonApiSerializer $serializer, Builder $builder, Model $model) |
|
53
|
|
|
{ |
|
54
|
|
|
$mapping = $serializer->getTransformer()->getMappingByClassName(get_class($model)); |
|
55
|
|
|
$sorts = RequestFactory::create()->getSort()->sorting(); |
|
56
|
|
|
|
|
57
|
|
|
if (!empty($sorts)) { |
|
58
|
|
|
$aliased = $mapping->getAliasedProperties(); |
|
59
|
|
|
|
|
60
|
|
|
$sortsFields = str_replace(array_values($aliased), array_keys($aliased), array_keys($sorts)); |
|
61
|
|
|
$sorts = array_combine($sortsFields, array_values($sorts)); |
|
62
|
|
|
|
|
63
|
|
|
foreach ($sorts as $field => $direction) { |
|
64
|
|
|
$builder->orderBy($field, ($direction === 'ascending') ? 'ASC' : 'DESC'); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $builder; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param JsonApiSerializer $serializer |
|
73
|
|
|
* @param array $fields |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
protected static function columns(JsonApiSerializer $serializer, array $fields) |
|
78
|
|
|
{ |
|
79
|
|
|
$filterColumns = []; |
|
80
|
|
|
|
|
81
|
|
|
foreach ($serializer->getTransformer()->getMappings() as $mapping) { |
|
82
|
|
|
$classAlias = $mapping->getClassAlias(); |
|
83
|
|
|
|
|
84
|
|
|
if (!empty($fields[$classAlias])) { |
|
85
|
|
|
$className = $mapping->getClassName(); |
|
86
|
|
|
$aliased = $mapping->getAliasedProperties(); |
|
87
|
|
|
|
|
88
|
|
|
/** @var \Illuminate\Database\Eloquent\Model $model * */ |
|
89
|
|
|
$model = new $className(); |
|
90
|
|
|
$columns = $fields[$classAlias]; |
|
91
|
|
|
|
|
92
|
|
|
if (count($aliased) > 0) { |
|
93
|
|
|
$columns = str_replace(array_values($aliased), array_keys($aliased), $columns); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
foreach ($columns as &$column) { |
|
97
|
|
|
$filterColumns[] = sprintf('%s.%s', $model->getTable(), $column); |
|
98
|
|
|
} |
|
99
|
|
|
$filterColumns[] = sprintf('%s.%s', $model->getTable(), $model->getKeyName()); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return (count($filterColumns) > 0) ? $filterColumns : ['*']; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|