1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Resources; |
4
|
|
|
|
5
|
|
|
use Flugg\Responder\Contracts\Resources\ResourceKeyResolver as ResourceKeyResolverContract; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Traversable; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class is responsible for resolving resource keys. |
11
|
|
|
* |
12
|
|
|
* @package flugger/laravel-responder |
13
|
|
|
* @author Alexander Tømmerås <[email protected]> |
14
|
|
|
* @license The MIT License |
15
|
|
|
*/ |
16
|
|
|
class ResourceKeyResolver implements ResourceKeyResolverContract |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Transformable to resource key mappings. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $bindings = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Register a transformable to resource key binding. |
27
|
|
|
* |
28
|
|
|
* @param string|array $transformable |
29
|
|
|
* @param string $resourceKey |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
2 |
|
public function bind($transformable, string $resourceKey) |
33
|
|
|
{ |
34
|
2 |
|
$this->bindings = array_merge($this->bindings, is_array($transformable) ? $transformable : [ |
35
|
2 |
|
$transformable => $resourceKey, |
36
|
|
|
]); |
37
|
2 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Resolve a resource key from the given data. |
41
|
|
|
* |
42
|
|
|
* @param mixed $data |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
50 |
View Code Duplication |
public function resolve($data) |
|
|
|
|
46
|
|
|
{ |
47
|
50 |
|
$transformable = $this->resolveTransformableItem($data); |
48
|
|
|
|
49
|
50 |
|
if (is_object($transformable) && key_exists(get_class($transformable), $this->bindings)) { |
50
|
2 |
|
return $this->bindings[get_class($transformable)]; |
51
|
|
|
} |
52
|
|
|
|
53
|
48 |
|
if ($transformable instanceof Model) { |
54
|
43 |
|
return $this->resolveFromModel($transformable); |
55
|
|
|
} |
56
|
|
|
|
57
|
5 |
|
return 'data'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Resolve a resource key from the given model. |
62
|
|
|
* |
63
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
43 |
|
public function resolveFromModel(Model $model) |
67
|
|
|
{ |
68
|
43 |
|
if (method_exists($model, 'getResourceKey')) { |
69
|
2 |
|
return $model->getResourceKey(); |
70
|
|
|
} |
71
|
|
|
|
72
|
41 |
|
return $model->getTable(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Resolve a transformable item from the given data. |
77
|
|
|
* |
78
|
|
|
* @param mixed $data |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
50 |
|
protected function resolveTransformableItem($data) |
82
|
|
|
{ |
83
|
50 |
|
if (is_array($data) || $data instanceof Traversable) { |
84
|
13 |
|
foreach ($data as $item) { |
85
|
12 |
|
return $item; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
38 |
|
return $data; |
90
|
|
|
} |
91
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.