1 | <?php |
||
15 | class MatchQueryBuilder extends QueryBuilder |
||
16 | { |
||
17 | /** |
||
18 | * Only include matches where a specific team/player played |
||
19 | * |
||
20 | * @param Team|Player $participant The team/player which played the matches |
||
21 | * @param string $result The outcome of the matches (win, draw or loss) |
||
22 | * @return self |
||
23 | */ |
||
24 | 2 | public function with($participant, $result = null) |
|
25 | { |
||
26 | 2 | if (!$participant || !$participant->isValid()) { |
|
27 | return $this; |
||
28 | } |
||
29 | |||
30 | 2 | if ($participant instanceof Team) { |
|
31 | 2 | $team_a_query = "team_a = ?"; |
|
32 | 2 | $team_b_query = "team_b = ?"; |
|
33 | } elseif ($participant instanceof Player) { |
||
34 | $team_a_query = "FIND_IN_SET(?, team_a_players)"; |
||
35 | $team_b_query = "FIND_IN_SET(?, team_b_players)"; |
||
36 | } else { |
||
37 | throw new InvalidArgumentException("Invalid model provided"); |
||
38 | } |
||
39 | |||
40 | switch ($result) { |
||
41 | 2 | case "wins": |
|
42 | 2 | case "win": |
|
43 | 2 | case "victory": |
|
44 | 2 | case "victories": |
|
45 | 1 | $query = "($team_a_query AND team_a_points > team_b_points) OR ($team_b_query AND team_b_points > team_a_points)"; |
|
46 | 1 | break; |
|
47 | 2 | case "loss": |
|
48 | 2 | case "lose": |
|
49 | 2 | case "losses": |
|
50 | 2 | case "defeat": |
|
51 | 2 | case "defeats": |
|
52 | $query = "($team_a_query AND team_b_points > team_a_points) OR ($team_b_query AND team_a_points > team_b_points)"; |
||
53 | break; |
||
54 | 2 | case "draw": |
|
55 | 2 | case "draws": |
|
56 | 2 | case "tie": |
|
57 | 2 | case "ties": |
|
58 | $query = "($team_a_query OR $team_b_query) AND team_a_points = team_b_points"; |
||
59 | break; |
||
60 | default: |
||
61 | 2 | $query = "$team_a_query OR $team_b_query"; |
|
62 | } |
||
63 | |||
64 | 2 | $this->conditions[] = $query; |
|
65 | 2 | $this->parameters[] = $participant->getId(); |
|
66 | 2 | $this->parameters[] = $participant->getId(); |
|
67 | |||
68 | 2 | return $this; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Group results by day |
||
73 | * |
||
74 | * @return $this |
||
75 | */ |
||
76 | 1 | public function groupByMonth() |
|
82 | |||
83 | /** |
||
84 | * Get a count for each month's matches |
||
85 | * |
||
86 | * @param Team $team The team in question |
||
87 | * @return array |
||
88 | */ |
||
89 | 1 | public function getSummary(Team $team) |
|
116 | } |
||
117 |