|
1
|
|
|
<?php namespace GolfLeague\Storage\Match; |
|
2
|
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
|
4
|
|
|
use \Match as Match; |
|
5
|
|
|
use \Player as Player; |
|
6
|
|
|
|
|
7
|
|
|
class EloquentMatchRepository implements MatchRepository |
|
8
|
|
|
{ |
|
9
|
|
|
/*Return Score collections that include: |
|
10
|
|
|
* Player Name |
|
11
|
|
|
* Date |
|
12
|
|
|
* Total |
|
13
|
|
|
* Course |
|
14
|
|
|
* Multideminsional Array of Hole Numbers and scores * |
|
15
|
|
|
**/ |
|
16
|
|
|
public function __construct(Match $match, Player $player) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->match = $match; |
|
|
|
|
|
|
19
|
|
|
$this->player = $player; |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function all() |
|
23
|
|
|
{ |
|
24
|
|
|
return Match::orderBy('date', 'DESC')->get(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function get($matchid) |
|
28
|
|
|
{ |
|
29
|
|
|
return Match::with('course', 'players', 'course.holes')->find($matchid); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getEditable($matchid) |
|
33
|
|
|
{ |
|
34
|
|
|
return Match::with('course', 'playersEdit', 'course.holes')->find($matchid); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getEditableMatches() |
|
38
|
|
|
{ |
|
39
|
|
|
$today = Carbon::now(); |
|
40
|
|
|
return Match::with('course', 'players')->where('date', '>=', $today)->get(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
public function getByDate($startDate, $endDate) |
|
45
|
|
|
{ |
|
46
|
|
|
//dd($startDate); |
|
47
|
|
|
return Match::with('course')->where('date', '>=', $startDate)->where('date', '<=', $endDate)->get(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getYears() |
|
51
|
|
|
{ |
|
52
|
|
|
$dates = Match::orderBy('date', 'DESC')->get(['date']); |
|
53
|
|
|
//return $dates; |
|
54
|
|
|
$years = $dates->map(function($years){ |
|
55
|
|
|
return substr($years->date, 0, 4); |
|
56
|
|
|
})->toArray(); |
|
57
|
|
|
|
|
58
|
|
|
return array_unique($years); |
|
59
|
|
|
} |
|
60
|
|
|
//Find Scores by Player Id |
|
61
|
|
|
public function find($playerId) |
|
62
|
|
|
{ |
|
63
|
|
|
//return Round::with('player', 'holescores')->where('player_id', '=', $playerId)->get(); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function create($matchdata) |
|
67
|
|
|
{ |
|
68
|
|
|
//enter match data to Matches table |
|
69
|
|
|
$this->match->date = $matchdata['date']; |
|
70
|
|
|
$this->match->course_id = $matchdata['course']; |
|
71
|
|
|
$this->match->purse = $matchdata['purse']; |
|
72
|
|
|
$this->match->skinsamoney = $matchdata['skinsamoney']; |
|
73
|
|
|
$this->match->skinsbmoney = $matchdata['skinsbmoney']; |
|
74
|
|
|
$this->match->grossmoney = $matchdata['grossmoney']; |
|
75
|
|
|
$this->match->netmoney = $matchdata['netmoney']; |
|
76
|
|
|
|
|
77
|
|
|
$this->match->save(); //save to match table |
|
78
|
|
|
|
|
79
|
|
|
foreach ($matchdata['player'] as $key => $player){ |
|
80
|
|
|
$currentPlayer = $this->player->find($player['player_id']); |
|
81
|
|
|
$attributes = array( |
|
82
|
|
|
"level_id" => $player['level_id'], |
|
83
|
|
|
"group" => $player['group'], |
|
84
|
|
|
"handicap" => $player['handicap'], |
|
85
|
|
|
"winnings" => $player['winnings'], |
|
86
|
|
|
); |
|
87
|
|
|
$currentPlayer->matches()->save($this->match, $attributes); //save match_player pivot data |
|
88
|
|
|
}// End foreach |
|
89
|
|
|
return $this->match->id; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function update($matchdata) |
|
93
|
|
|
{ |
|
94
|
|
|
//return $matchdata; |
|
95
|
|
|
foreach ($matchdata['player'] as $player) { |
|
96
|
|
|
// update match_player pivot tables |
|
97
|
|
|
Match::find($matchdata['match']) |
|
98
|
|
|
->players() |
|
99
|
|
|
->updateExistingPivot( |
|
100
|
|
|
$player['player_id'], |
|
101
|
|
|
[ |
|
102
|
|
|
'level_id' => $player['level_id'], |
|
103
|
|
|
'group' => $player['group'] |
|
104
|
|
|
] |
|
105
|
|
|
); |
|
106
|
|
|
//delete rounds for the match |
|
107
|
|
|
Round::where('match_id', '=', $matchdata['match']) |
|
108
|
|
|
->where('player_id', '=', $player['player_id']); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function delete($matchId) |
|
114
|
|
|
{ |
|
115
|
|
|
return 'deleted'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: