1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Transformers; |
4
|
|
|
|
5
|
|
|
use App\Purchase; |
6
|
|
|
use League\Fractal\TransformerAbstract; |
7
|
|
|
|
8
|
|
|
class PurchaseTransformer extends TransformerAbstract |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* List of resources possible to include. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $availableIncludes = ['supplier', 'guitars']; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Turn this item object into a generic array. |
19
|
|
|
* |
20
|
|
|
* @param Purchase $purchase |
21
|
|
|
* |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
1 |
|
public function transform(Purchase $purchase) |
25
|
|
|
{ |
26
|
|
|
return [ |
27
|
1 |
|
'id' => (int) $purchase->id, |
28
|
1 |
|
'supplier_id' => (int) $purchase->supplier_id, |
29
|
1 |
|
'quantity' => (int) $purchase->quantity, |
30
|
1 |
|
'price' => (float) $purchase->price, |
31
|
1 |
|
'stored' => (int) $purchase->stored, |
32
|
1 |
|
'sold' => (int) $purchase->sold, |
33
|
|
|
'make' => [ |
34
|
1 |
|
'id' => (int) $purchase->make->id, |
35
|
1 |
|
'name' => (string) $purchase->make->name, |
36
|
1 |
|
], |
37
|
|
|
'purchased' => [ |
38
|
1 |
|
'timestamp' => $purchase->date_purchased->getTimestamp(), |
39
|
1 |
|
'date' => (string) $purchase->date_purchased->toFormattedDateString(), |
40
|
1 |
|
'readable' => (string) $purchase->date_purchased->diffForHumans(), |
41
|
1 |
|
], |
42
|
|
|
'delivery' => [ |
43
|
1 |
|
'timestamp' => $purchase->delivery_date->getTimestamp(), |
44
|
1 |
|
'date' => (string) $purchase->delivery_date->toFormattedDateString(), |
45
|
1 |
|
'readable' => (string) $purchase->delivery_date->diffForHumans(), |
46
|
1 |
|
], |
47
|
1 |
|
'status' => (string) $purchase->status, |
48
|
1 |
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Include supplier. |
53
|
|
|
* |
54
|
|
|
* @param Purchase $purchase |
55
|
|
|
* |
56
|
|
|
* @return \League\Fractal\Resource\Collection |
57
|
|
|
*/ |
58
|
|
|
public function includeSupplier(Purchase $purchase) |
59
|
|
|
{ |
60
|
|
|
return $this->item($purchase->supplier, new SupplierTransformer); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Include guitars. |
65
|
|
|
* |
66
|
|
|
* @param Purchase $purchase |
67
|
|
|
* |
68
|
|
|
* @return \League\Fractal\Resource\Collection |
69
|
|
|
*/ |
70
|
|
|
public function includeGuitars(Purchase $purchase) |
71
|
|
|
{ |
72
|
|
|
return $this->collection($purchase->guitars, new GuitarTransformer); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: