@@ -44,11 +44,19 @@ discard block |
||
44 | 44 | private $groups = []; |
45 | 45 | |
46 | 46 | function __construct(string $what = 'points', string $how = '>', $val = 0, $groups = []){ |
47 | - if (in_array(strtolower($what), ['points', 'score', 'wins', 'draws', 'losses', 'second', 'third', 'team', 'notprogressed', 'progressed'])) $this->what = strtolower($what); |
|
48 | - if (in_array($how, ['>', '<', '>=', '<=', '=', '!='])) $this->how = $how; |
|
49 | - if ((gettype($val) === 'integer' && strtolower($what) !== 'team') || ($val instanceof Team && strtolower($what) === 'team')) $this->val = $val; |
|
47 | + if (in_array(strtolower($what), ['points', 'score', 'wins', 'draws', 'losses', 'second', 'third', 'team', 'notprogressed', 'progressed'])) { |
|
48 | + $this->what = strtolower($what); |
|
49 | + } |
|
50 | + if (in_array($how, ['>', '<', '>=', '<=', '=', '!='])) { |
|
51 | + $this->how = $how; |
|
52 | + } |
|
53 | + if ((gettype($val) === 'integer' && strtolower($what) !== 'team') || ($val instanceof Team && strtolower($what) === 'team')) { |
|
54 | + $this->val = $val; |
|
55 | + } |
|
50 | 56 | foreach ($groups as $group) { |
51 | - if ($group instanceof Group) $this->groups[] = $group->id; |
|
57 | + if ($group instanceof Group) { |
|
58 | + $this->groups[] = $group->id; |
|
59 | + } |
|
52 | 60 | } |
53 | 61 | } |
54 | 62 | public function __toString() { |
@@ -56,14 +64,14 @@ discard block |
||
56 | 64 | } |
57 | 65 | |
58 | 66 | public function validate(Team $team, $groupsId, string $operation = 'sum', Group $from = null) { |
59 | - if (count($this->groups) > 0) $groupsId = array_unique(array_merge($this->groups, (gettype($groupsId) === 'array' ? $groupsId : [$groupsId])), SORT_REGULAR); |
|
67 | + if (count($this->groups) > 0) { |
|
68 | + $groupsId = array_unique(array_merge($this->groups, (gettype($groupsId) === 'array' ? $groupsId : [$groupsId])), SORT_REGULAR); |
|
69 | + } |
|
60 | 70 | if ($this->what == 'team') { |
61 | 71 | return $this->validateTeam($team); |
62 | - } |
|
63 | - elseif ($this->what == 'notprogressed') { |
|
72 | + } elseif ($this->what == 'notprogressed') { |
|
64 | 73 | return !$this->validateProgressed($team, $from); |
65 | - } |
|
66 | - elseif ($this->what == 'progressed') { |
|
74 | + } elseif ($this->what == 'progressed') { |
|
67 | 75 | return $this->validateProgressed($team, $from); |
68 | 76 | } |
69 | 77 | return $this->validateCalc($team, $groupsId, $operation, $from); |
@@ -72,20 +80,28 @@ discard block |
||
72 | 80 | private function validateTeam(Team $team) { |
73 | 81 | switch ($this->how) { |
74 | 82 | case '=': |
75 | - if ($this->val === $team) return true; |
|
83 | + if ($this->val === $team) { |
|
84 | + return true; |
|
85 | + } |
|
76 | 86 | break; |
77 | 87 | case '!=': |
78 | - if ($this->val !== $team) return true; |
|
88 | + if ($this->val !== $team) { |
|
89 | + return true; |
|
90 | + } |
|
79 | 91 | break; |
80 | 92 | } |
81 | 93 | return false; |
82 | 94 | } |
83 | 95 | private function validateProgressed(Team $team, Group $from = null) { |
84 | - if ($from === null) throw new \Exception('Group $from was not defined.'); |
|
96 | + if ($from === null) { |
|
97 | + throw new \Exception('Group $from was not defined.'); |
|
98 | + } |
|
85 | 99 | return $from->isProgressed($team); |
86 | 100 | } |
87 | 101 | private function validateCalc(Team $team, $groupsId, string $operation = 'sum') { |
88 | - if (gettype($groupsId) === 'array' && !in_array(strtolower($operation), ['sum', 'avg', 'max', 'min'])) throw new \Exception('Unknown operation of '.$operation.'. Only "sum", "avg", "min", "max" possible.'); |
|
102 | + if (gettype($groupsId) === 'array' && !in_array(strtolower($operation), ['sum', 'avg', 'max', 'min'])) { |
|
103 | + throw new \Exception('Unknown operation of '.$operation.'. Only "sum", "avg", "min", "max" possible.'); |
|
104 | + } |
|
89 | 105 | $comp = 0; |
90 | 106 | if (gettype($groupsId) === 'array' && count($groupsId) > 0) { |
91 | 107 | switch (strtolower($operation)) { |
@@ -94,9 +110,11 @@ discard block |
||
94 | 110 | case 'max': $comp = $this->calcMax($team, $groupsId); |
95 | 111 | case 'min': $comp = $this->calcMin($team, $groupsId); |
96 | 112 | } |
113 | + } elseif (gettype($groupsId) === 'string' && isset($team->groupResults[$groupsId])) { |
|
114 | + $comp = $team->groupResults[$groupsId][$this->what]; |
|
115 | + } else { |
|
116 | + throw new \Exception("Couldn't find group of id ".print_r($groupsId, true)); |
|
97 | 117 | } |
98 | - elseif (gettype($groupsId) === 'string' && isset($team->groupResults[$groupsId])) $comp = $team->groupResults[$groupsId][$this->what]; |
|
99 | - else throw new \Exception("Couldn't find group of id ".print_r($groupsId, true)); |
|
100 | 118 | |
101 | 119 | switch ($this->how) { |
102 | 120 | case '>': return ($comp > $this->val); |
@@ -112,7 +130,10 @@ discard block |
||
112 | 130 | private function calcSum(Team $team, $groupsId) { |
113 | 131 | $sum = 0; |
114 | 132 | foreach ($groupsId as $id) { |
115 | - if (!isset($team->groupResults[$id])) continue; // IF TEAM DIDN'T PLAY IN THAT GROUP -> SKIP |
|
133 | + if (!isset($team->groupResults[$id])) { |
|
134 | + continue; |
|
135 | + } |
|
136 | + // IF TEAM DIDN'T PLAY IN THAT GROUP -> SKIP |
|
116 | 137 | $sum += $team->groupResults[$id][$this->what]; |
117 | 138 | } |
118 | 139 | return $sum; |
@@ -120,14 +141,18 @@ discard block |
||
120 | 141 | private function calcMax(Team $team, $groupsId) { |
121 | 142 | $max = null; |
122 | 143 | foreach ($groupsId as $id) { |
123 | - if (isset($team->groupResults[$id]) && ($team->groupResults[$id][$this->what] > $max || $max === null)) $max = $team->groupResults[$id][$this->what]; |
|
144 | + if (isset($team->groupResults[$id]) && ($team->groupResults[$id][$this->what] > $max || $max === null)) { |
|
145 | + $max = $team->groupResults[$id][$this->what]; |
|
146 | + } |
|
124 | 147 | } |
125 | 148 | return $max; |
126 | 149 | } |
127 | 150 | private function calcMin(Team $team, $groupsId) { |
128 | 151 | $min = null; |
129 | 152 | foreach ($groupsId as $id) { |
130 | - if (isset($team->groupResults[$id]) && ($team->groupResults[$id][$this->what] < $min || $min === null)) $min = $team->groupResults[$id][$this->what]; |
|
153 | + if (isset($team->groupResults[$id]) && ($team->groupResults[$id][$this->what] < $min || $min === null)) { |
|
154 | + $min = $team->groupResults[$id][$this->what]; |
|
155 | + } |
|
131 | 156 | } |
132 | 157 | return $min; |
133 | 158 | } |