1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Fractal; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use League\Fractal\Resource\Item; |
7
|
|
|
use League\Fractal\Resource\Collection; |
8
|
|
|
use League\Fractal\TransformerAbstract as FractalTransformer; |
9
|
|
|
|
10
|
|
|
abstract class TransformerAbstract extends FractalTransformer |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $itemIncludes = [ |
16
|
|
|
// 'foo' => FooTransformer::class |
|
|
|
|
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $collectionIncludes = [ |
23
|
|
|
// 'bar' => BarTransformer::class |
|
|
|
|
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* TransformerAbstract constructor. |
28
|
|
|
*/ |
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->availableIncludes = array_merge($this->availableIncludes, |
32
|
|
|
array_keys($this->itemIncludes), |
33
|
|
|
array_keys($this->collectionIncludes)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Simulate 'include{$resource}()' methods. |
38
|
|
|
* |
39
|
|
|
* @param string $method |
40
|
|
|
* @param array $arguments |
41
|
|
|
* @return Collection|Item |
42
|
|
|
*/ |
43
|
|
|
public function __call($method, $arguments) |
44
|
|
|
{ |
45
|
|
|
$baseResource = $arguments[0] ?? null; |
46
|
|
|
$relatedResource = Str::snake(preg_replace('/^include/', '', $method)); |
47
|
|
|
|
48
|
|
|
if (array_key_exists($relatedResource, $this->itemIncludes)) { |
49
|
|
|
return $this->item($baseResource->{$relatedResource}, new $this->itemIncludes[$relatedResource]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (array_key_exists($relatedResource, $this->collectionIncludes)) { |
53
|
|
|
return $this->collection( |
54
|
|
|
$baseResource->{$relatedResource}, new $this->collectionIncludes[$relatedResource]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
throw new \BadMethodCallException('Unknown method "'.$method.'" called on '.static::class.'.'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.