1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gamer\Traits; |
4
|
|
|
|
5
|
|
|
use Gamer\Models\Transaction; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
trait Pointable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
12
|
|
|
*/ |
13
|
|
|
public function transactions($amount = null) |
14
|
|
|
{ |
15
|
|
|
return $this->morphMany(Transaction::class, 'pointable')->orderBy('created_at','desc')->take($amount); |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
// /** |
19
|
|
|
// * |
20
|
|
|
// * @return mix |
21
|
|
|
// */ |
22
|
|
|
// public function averagePoint($round= null) |
23
|
|
|
// { |
24
|
|
|
// if ($round) { |
25
|
|
|
// return $this->transactions() |
26
|
|
|
// ->selectRaw('ROUND(AVG(amount), '.$round.') as averagePointTransaction') |
27
|
|
|
// ->pluck('averagePointTransaction'); |
28
|
|
|
// } |
29
|
|
|
// |
30
|
|
|
// return $this->transactions() |
31
|
|
|
// ->selectRaw('AVG(amount) as averagePointTransaction') |
32
|
|
|
// ->pluck('averagePointTransaction'); |
33
|
|
|
// } |
34
|
|
|
// |
35
|
|
|
// /** |
36
|
|
|
// * |
37
|
|
|
// * @return mix |
38
|
|
|
// */ |
39
|
|
|
// public function countPoint(){ |
40
|
|
|
// return $this->transactions() |
41
|
|
|
// ->selectRaw('count(amount) as countTransactions') |
42
|
|
|
// ->pluck('countTransactions'); |
43
|
|
|
// } |
44
|
|
|
// |
45
|
|
|
// /** |
46
|
|
|
// * |
47
|
|
|
// * @return mix |
48
|
|
|
// */ |
49
|
|
|
// public function sumPoint() |
50
|
|
|
// { |
51
|
|
|
// return $this->transactions() |
52
|
|
|
// ->selectRaw('SUM(amount) as sumPointTransactions') |
53
|
|
|
// ->pluck('sumPointTransactions'); |
54
|
|
|
// } |
55
|
|
|
// |
56
|
|
|
// /** |
57
|
|
|
// * @param $max |
58
|
|
|
// * |
59
|
|
|
// * @return mix |
60
|
|
|
// */ |
61
|
|
|
// public function pointPercent($max = 5) |
62
|
|
|
// { |
63
|
|
|
// $transactions = $this->transactions(); |
64
|
|
|
// $quantity = $transactions->count(); |
65
|
|
|
// $total = $transactions->selectRaw('SUM(amount) as total')->pluck('total'); |
66
|
|
|
// return ($quantity * $max) > 0 ? $total / (($quantity * $max) / 100) : 0; |
67
|
|
|
// } |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* |
71
|
|
|
* @return mix |
72
|
|
|
*/ |
73
|
|
|
public function countTransactions(){ |
74
|
|
|
return $this->transactions() |
75
|
|
|
->count(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* @return double |
81
|
|
|
*/ |
82
|
|
|
public function currentPoints() |
83
|
|
|
{ |
84
|
|
|
return (new Transaction())->getCurrentPoints($this); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $amount |
89
|
|
|
* @param $message |
90
|
|
|
* @param $data |
91
|
|
|
* |
92
|
|
|
* @return static |
93
|
|
|
*/ |
94
|
|
|
public function addPoints($amount, $message, $data = null) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
return (new Transaction())->addTransaction($this, $amount, $message, $data = null); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.