1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WSW\SiftScience\Support\Traits\Transformers; |
4
|
|
|
|
5
|
|
|
use WSW\SiftScience\Collections\Items; |
6
|
|
|
use WSW\SiftScience\Collections\PaymentMethods; |
7
|
|
|
use WSW\SiftScience\Collections\Promotions; |
8
|
|
|
use WSW\SiftScience\Entities\Address; |
9
|
|
|
use WSW\SiftScience\Entities\CreditPoint; |
10
|
|
|
use WSW\SiftScience\Entities\Discount; |
11
|
|
|
use WSW\SiftScience\Transformers\Entities\AddressTransformer; |
12
|
|
|
use WSW\SiftScience\Transformers\Entities\CreditPointTransformer; |
13
|
|
|
use WSW\SiftScience\Transformers\Entities\DiscountTransformer; |
14
|
|
|
use WSW\SiftScience\Transformers\Entities\ItemTransformer; |
15
|
|
|
use WSW\SiftScience\Transformers\Entities\PaymentMethodTransformer; |
16
|
|
|
use WSW\SiftScience\Transformers\Entities\PromotionTransformer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait Relationships |
20
|
|
|
* |
21
|
|
|
* @package WSW\SiftScience\Support\Traits\Transformers |
22
|
|
|
* @author Ronaldo Matos Rodrigues <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
trait Relationships |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param $address|null |
28
|
|
|
* |
29
|
|
|
* @return array|null |
30
|
|
|
*/ |
31
|
|
|
protected function address($address = null) |
32
|
|
|
{ |
33
|
|
|
return (!$address instanceof Address) ? null : $this->loadItem($address, new AddressTransformer()); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Discount|null $discount |
38
|
|
|
* |
39
|
|
|
* @return array|null |
40
|
|
|
*/ |
41
|
|
|
protected function discount($discount = null) |
42
|
|
|
{ |
43
|
|
|
return (!$discount instanceof Discount) ? null : $this->loadItem($discount, new DiscountTransformer); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param CreditPoint|null $creditPoint |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
protected function creditPoint($creditPoint = null) |
52
|
|
|
{ |
53
|
|
|
return (!$creditPoint instanceof CreditPoint) |
54
|
|
|
? null |
55
|
|
|
: $this->loadItem($creditPoint, new CreditPointTransformer); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param PaymentMethods|null $collection |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
protected function paymentMethods($collection = null) |
64
|
|
|
{ |
65
|
|
|
return (!$collection instanceof PaymentMethods) |
66
|
|
|
? null |
67
|
|
|
: $this->loadCollection($collection->getValues(), new PaymentMethodTransformer); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Items|null $items |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
protected function items($items) |
76
|
|
|
{ |
77
|
|
|
return (!$items instanceof Items) |
78
|
|
|
? null |
79
|
|
|
: $this->loadCollection($items->getValues(), new ItemTransformer); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Promotions|null $promotions |
84
|
|
|
* |
85
|
|
|
* @return array|null |
86
|
|
|
*/ |
87
|
|
|
protected function promotions(Promotions $promotions = null) |
88
|
|
|
{ |
89
|
|
|
return (!$promotions instanceof Promotions) |
90
|
|
|
? null |
91
|
|
|
: $this->loadCollection($promotions->getValues(), new PromotionTransformer); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.