1
|
|
|
<?php namespace GolfLeague\Storage\Match; |
2
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
4
|
|
|
use \Match as Match; |
5
|
|
|
use \Round as Round; |
6
|
|
|
use \Player as Player; |
7
|
|
|
|
8
|
|
|
class EloquentMatchRepository implements MatchRepository |
9
|
|
|
{ |
10
|
|
|
/*Return Score collections that include: |
11
|
|
|
* Player Name |
12
|
|
|
* Date |
13
|
|
|
* Total |
14
|
|
|
* Course |
15
|
|
|
* Multideminsional Array of Hole Numbers and scores * |
16
|
|
|
**/ |
17
|
|
|
public function __construct(Match $match, Player $player) |
18
|
|
|
{ |
19
|
|
|
$this->match = $match; |
|
|
|
|
20
|
|
|
$this->player = $player; |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function all() |
24
|
|
|
{ |
25
|
|
|
return Match::orderBy('date', 'DESC')->get(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function get($matchid) |
29
|
|
|
{ |
30
|
|
|
return Match::with('course', 'players', 'course.holes')->find($matchid); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getEditable($matchid) |
34
|
|
|
{ |
35
|
|
|
return Match::with('course', 'playersEdit', 'course.holes')->find($matchid); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getEditableMatches() |
39
|
|
|
{ |
40
|
|
|
$today = Carbon::today(); |
41
|
|
|
return Match::with('course', 'players')->where('date', '>=', $today)->get(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
public function getByDate($startDate, $endDate) |
46
|
|
|
{ |
47
|
|
|
//dd($startDate); |
48
|
|
|
return Match::with('course')->where('date', '>=', $startDate)->where('date', '<=', $endDate)->get(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getYears() |
52
|
|
|
{ |
53
|
|
|
$dates = Match::orderBy('date', 'DESC')->get(['date']); |
54
|
|
|
//return $dates; |
55
|
|
|
$years = $dates->map(function($years){ |
56
|
|
|
return substr($years->date, 0, 4); |
57
|
|
|
})->toArray(); |
58
|
|
|
|
59
|
|
|
return array_unique($years); |
60
|
|
|
} |
61
|
|
|
//Find Scores by Player Id |
62
|
|
|
public function find($playerId) |
63
|
|
|
{ |
64
|
|
|
//return Round::with('player', 'holescores')->where('player_id', '=', $playerId)->get(); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function create($matchdata) |
68
|
|
|
{ |
69
|
|
|
//enter match data to Matches table |
70
|
|
|
$this->match->date = $matchdata['date']; |
71
|
|
|
$this->match->course_id = $matchdata['course']; |
72
|
|
|
$this->match->purse = $matchdata['purse']; |
73
|
|
|
$this->match->skinsamoney = $matchdata['skinsamoney']; |
74
|
|
|
$this->match->skinsbmoney = $matchdata['skinsbmoney']; |
75
|
|
|
$this->match->grossmoney = $matchdata['grossmoney']; |
76
|
|
|
$this->match->netmoney = $matchdata['netmoney']; |
77
|
|
|
|
78
|
|
|
$this->match->save(); //save to match table |
79
|
|
|
|
80
|
|
|
foreach ($matchdata['player'] as $key => $player){ |
81
|
|
|
$currentPlayer = $this->player->find($player['player_id']); |
82
|
|
|
$attributes = array( |
83
|
|
|
"level_id" => $player['level_id'], |
84
|
|
|
"group" => $player['group'], |
85
|
|
|
"handicap" => $player['handicap'], |
86
|
|
|
"winnings" => $player['winnings'], |
87
|
|
|
); |
88
|
|
|
$currentPlayer->matches()->save($this->match, $attributes); //save match_player pivot data |
89
|
|
|
}// End foreach |
90
|
|
|
return $this->match->id; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function update($matchdata) |
94
|
|
|
{ |
95
|
|
|
//update course |
96
|
|
|
$currentMatch = $this->match->find($matchdata['match']); |
97
|
|
|
$currentMatch->course_id = $matchdata['course']; //update course |
98
|
|
|
$currentMatch->update(); |
99
|
|
|
|
100
|
|
|
//Remove players from match_players |
101
|
|
|
$currentMatch->players()->detach(); |
102
|
|
|
|
103
|
|
|
//Remove rounds |
104
|
|
|
Round::where('match_id', '=', $currentMatch->id)->delete(); |
105
|
|
|
|
106
|
|
|
//Add players to match_players |
107
|
|
|
foreach ($matchdata['player'] as $key => $player) { |
108
|
|
|
$currentPlayer = $this->player->find($player['player_id']); |
109
|
|
|
$attributes = array( |
110
|
|
|
"level_id" => $player['level_id'], |
111
|
|
|
"group" => $player['group'], |
112
|
|
|
"handicap" => $currentPlayer->handicap, |
113
|
|
|
"winnings" => 0, |
114
|
|
|
); |
115
|
|
|
$currentPlayer->matches()->attach($currentMatch, $attributes); //save match_player pivot data |
116
|
|
|
|
117
|
|
|
$newRound = array( |
118
|
|
|
'date' => $currentMatch->date, |
119
|
|
|
"player_id" => $player['player_id'], |
120
|
|
|
"course_id" => $player['group'], |
121
|
|
|
"match_id" => $currentMatch->course, |
122
|
|
|
"score" => 0, |
123
|
|
|
"esc" => 0 |
124
|
|
|
); |
125
|
|
|
//Create New round for player |
126
|
|
|
Round::create($newRound); |
127
|
|
|
|
128
|
|
|
}// End foreach |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function delete($matchId) |
132
|
|
|
{ |
133
|
|
|
return 'deleted'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
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: