@@ -43,11 +43,11 @@ |
||
43 | 43 | |
44 | 44 | private $groups = []; |
45 | 45 | |
46 | - function __construct(string $what = 'points', string $how = '>', $val = 0, $groups = []){ |
|
46 | + function __construct(string $what = 'points', string $how = '>', $val = 0, $groups = []) { |
|
47 | 47 | if (in_array(strtolower($what), ['points', 'score', 'wins', 'draws', 'losses', 'second', 'third', 'team', 'notprogressed', 'progressed'])) $this->what = strtolower($what); |
48 | 48 | if (in_array($how, ['>', '<', '>=', '<=', '=', '!='])) $this->how = $how; |
49 | 49 | if ((gettype($val) === 'integer' && strtolower($what) !== 'team') || ($val instanceof Team && strtolower($what) === 'team')) $this->val = $val; |
50 | - $this->groups = array_map(function($a) { return $a->id; }, array_filter($groups, function($a) {return ($a instanceof Group);})); |
|
50 | + $this->groups = array_map(function($a) { return $a->id; }, array_filter($groups, function($a) {return ($a instanceof Group); })); |
|
51 | 51 | } |
52 | 52 | public function __toString() { |
53 | 53 | return 'Filter: '.$this->what.' '.($this->what !== 'notprogressed' && $this->what !== 'progressed' ? $this->how.' '.$this->val : ''); |
@@ -44,9 +44,15 @@ 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 | $this->groups = array_map(function($a) { return $a->id; }, array_filter($groups, function($a) {return ($a instanceof Group);})); |
51 | 57 | } |
52 | 58 | public function __toString() { |
@@ -54,11 +60,17 @@ discard block |
||
54 | 60 | } |
55 | 61 | |
56 | 62 | public function validate(Team $team, $groupsId, string $operation = 'sum', Group $from = null) { |
57 | - if (count($this->groups) > 0) $groupsId = array_unique(array_merge($this->groups, (gettype($groupsId) === 'array' ? $groupsId : [$groupsId])), SORT_REGULAR); |
|
63 | + if (count($this->groups) > 0) { |
|
64 | + $groupsId = array_unique(array_merge($this->groups, (gettype($groupsId) === 'array' ? $groupsId : [$groupsId])), SORT_REGULAR); |
|
65 | + } |
|
58 | 66 | |
59 | - if ($this->what == 'team') return ($this->how === '!=' ? !$this->validateTeam($team) : $this->validateTeam($team)); |
|
60 | - elseif ($this->what == 'notprogressed') return !$this->validateProgressed($team, $from); |
|
61 | - elseif ($this->what == 'progressed') return $this->validateProgressed($team, $from); |
|
67 | + if ($this->what == 'team') { |
|
68 | + return ($this->how === '!=' ? !$this->validateTeam($team) : $this->validateTeam($team)); |
|
69 | + } elseif ($this->what == 'notprogressed') { |
|
70 | + return !$this->validateProgressed($team, $from); |
|
71 | + } elseif ($this->what == 'progressed') { |
|
72 | + return $this->validateProgressed($team, $from); |
|
73 | + } |
|
62 | 74 | |
63 | 75 | return $this->validateCalc($team, $groupsId, $operation); |
64 | 76 | } |
@@ -67,13 +79,19 @@ discard block |
||
67 | 79 | return $this->val === $team; |
68 | 80 | } |
69 | 81 | private function validateProgressed(Team $team, Group $from = null) { |
70 | - if ($from === null) throw new \Exception('Group $from was not defined.'); |
|
82 | + if ($from === null) { |
|
83 | + throw new \Exception('Group $from was not defined.'); |
|
84 | + } |
|
71 | 85 | return $from->isProgressed($team); |
72 | 86 | } |
73 | 87 | private function validateCalc(Team $team, $groupsId, string $operation = 'sum') { |
74 | - 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.'); |
|
88 | + if (gettype($groupsId) === 'array' && !in_array(strtolower($operation), ['sum', 'avg', 'max', 'min'])) { |
|
89 | + throw new \Exception('Unknown operation of '.$operation.'. Only "sum", "avg", "min", "max" possible.'); |
|
90 | + } |
|
75 | 91 | $comp = 0; |
76 | - if (gettype($groupsId) === 'string') $groupsId = [$groupsId]; |
|
92 | + if (gettype($groupsId) === 'string') { |
|
93 | + $groupsId = [$groupsId]; |
|
94 | + } |
|
77 | 95 | switch (strtolower($operation)) { |
78 | 96 | case 'sum': $comp = $this->calcSum($team, $groupsId); break; |
79 | 97 | case 'avg': $comp = $this->calcSum($team, $groupsId)/count($groupsId); break; |
@@ -95,21 +113,27 @@ discard block |
||
95 | 113 | private function calcSum(Team $team, $groupsId) { |
96 | 114 | $sum = 0; |
97 | 115 | foreach ($groupsId as $id) { |
98 | - if (isset($team->groupResults[$id])) $sum += $team->groupResults[$id][$this->what]; |
|
116 | + if (isset($team->groupResults[$id])) { |
|
117 | + $sum += $team->groupResults[$id][$this->what]; |
|
118 | + } |
|
99 | 119 | } |
100 | 120 | return $sum; |
101 | 121 | } |
102 | 122 | private function calcMax(Team $team, $groupsId) { |
103 | 123 | $max = null; |
104 | 124 | foreach ($groupsId as $id) { |
105 | - if (isset($team->groupResults[$id]) && ($team->groupResults[$id][$this->what] > $max || $max === null)) $max = $team->groupResults[$id][$this->what]; |
|
125 | + if (isset($team->groupResults[$id]) && ($team->groupResults[$id][$this->what] > $max || $max === null)) { |
|
126 | + $max = $team->groupResults[$id][$this->what]; |
|
127 | + } |
|
106 | 128 | } |
107 | 129 | return $max; |
108 | 130 | } |
109 | 131 | private function calcMin(Team $team, $groupsId) { |
110 | 132 | $min = null; |
111 | 133 | foreach ($groupsId as $id) { |
112 | - if (isset($team->groupResults[$id]) && ($team->groupResults[$id][$this->what] < $min || $min === null)) $min = $team->groupResults[$id][$this->what]; |
|
134 | + if (isset($team->groupResults[$id]) && ($team->groupResults[$id][$this->what] < $min || $min === null)) { |
|
135 | + $min = $team->groupResults[$id][$this->what]; |
|
136 | + } |
|
113 | 137 | } |
114 | 138 | return $min; |
115 | 139 | } |