| @@ -13,7 +13,9 @@ discard block | ||
| 13 | 13 | |
| 14 | 14 | $countTeams = count($this->getTeams()); | 
| 15 | 15 | |
| 16 | -		if ($countTeams < 3) throw new \Exception('Double elimination is possible for minimum of 3 teams - '.$countTeams.' teams given.'); | |
| 16 | +		if ($countTeams < 3) { | |
| 17 | +			throw new \Exception('Double elimination is possible for minimum of 3 teams - '.$countTeams.' teams given.'); | |
| 18 | + } | |
| 17 | 19 | |
| 18 | 20 | |
| 19 | 21 | // CALCULATE BYES | 
| @@ -131,15 +133,19 @@ discard block | ||
| 131 | 133 |  				if ($r === 2) { // FIRST LOSING ROUND | 
| 132 | 134 | $previousGroups[2*($g-1)]->progression($group, 1, 1); // PROGRESS FROM STARTING GROUP | 
| 133 | 135 | $previousGroups[(2*($g-1))+1]->progression($group, 1, 1); // PROGREESS FROM STARTING GROUP | 
| 134 | - } | |
| 135 | -				elseif ($losingGroupTeamsCount >= 2) { | |
| 136 | +				} elseif ($losingGroupTeamsCount >= 2) { | |
| 136 | 137 | $previousLosingGroups[$g-1]->progression($group, 0, 1); // PROGRESS FROM LOSING GROUP BEFORE | 
| 137 | - if (isset(array_reverse($previousGroups)[$g-1])) array_reverse($previousGroups)[$g-1]->progression($group, 1, 1); // PROGREESS FROM WINNING GROUP BEFORE | |
| 138 | - else $previousLosingGroups[$g]->progression($group, 0, 1); // PROGRESS OTHER TEAM FROM LOSING GROUP BEEFORE | |
| 138 | +					if (isset(array_reverse($previousGroups)[$g-1])) { | |
| 139 | + array_reverse($previousGroups)[$g-1]->progression($group, 1, 1); | |
| 140 | + } | |
| 141 | + // PROGREESS FROM WINNING GROUP BEFORE | |
| 142 | +					else { | |
| 143 | + $previousLosingGroups[$g]->progression($group, 0, 1); | |
| 144 | + } | |
| 145 | + // PROGRESS OTHER TEAM FROM LOSING GROUP BEEFORE | |
| 139 | 146 | } | 
| 140 | 147 | } | 
| 141 | - } | |
| 142 | -		else { // IF THE NUMBER OF TEAMS IS NOT A POWER OF 2, GENERATE GROUPS WITH BYES | |
| 148 | +		} else { // IF THE NUMBER OF TEAMS IS NOT A POWER OF 2, GENERATE GROUPS WITH BYES | |
| 143 | 149 | // LOOK FOR THE CLOSEST LOWER POWER OF 2 | 
| 144 | 150 | $losingByes = $losingGroupTeamsCount-bindec(str_pad(1, strlen(decbin($losingGroupTeamsCount)), 0, STR_PAD_RIGHT)); | 
| 145 | 151 | $n = (floor(count($previousLosingGroups)/2)+$losingByes); | 
| @@ -163,10 +169,12 @@ discard block | ||
| 163 | 169 |  				if (in_array($g, $byesGroupsNums) && isset($previousGroups[$byesProgressed])) { // EMPTY GROUP FROM BYE | 
| 164 | 170 | $previousGroups[$byesProgressed]->progression($group, 1, 1); // PROGRESS FROM WINNING GROUP BEFORE | 
| 165 | 171 | $byesProgressed++; | 
| 166 | - } | |
| 167 | -				else { | |
| 172 | +				} else { | |
| 168 | 173 | $previousLosingGroups[$lastGroup]->progression($group, 0, 1); // PROGRESS FROM LOSING GROUP BEFORE | 
| 169 | - if (isset($previousLosingGroups[$lastGroup + 1])) $previousLosingGroups[$lastGroup + 1]->progression($group, 0, 1); // PROGREESS FROM LOSING GROUP BEFORE | |
| 174 | +					if (isset($previousLosingGroups[$lastGroup + 1])) { | |
| 175 | + $previousLosingGroups[$lastGroup + 1]->progression($group, 0, 1); | |
| 176 | + } | |
| 177 | + // PROGREESS FROM LOSING GROUP BEFORE | |
| 170 | 178 | $lastGroup += 2; | 
| 171 | 179 | } | 
| 172 | 180 | } | 
| @@ -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,30 +80,45 @@ 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) { | 
| 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', Group $from = null) { | 
| 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 | $sum = 0; | 
| 92 | 108 | $max = null; | 
| 93 | 109 | $min = null; | 
| 94 | 110 |  			foreach ($groupsId as $id) { | 
| 95 | - if (!isset($team->groupResults[$id])) continue; // IF TEAM DIDN'T PLAY IN THAT GROUP -> SKIP | |
| 111 | +				if (!isset($team->groupResults[$id])) { | |
| 112 | + continue; | |
| 113 | + } | |
| 114 | + // IF TEAM DIDN'T PLAY IN THAT GROUP -> SKIP | |
| 96 | 115 | $sum += $team->groupResults[$id][$this->what]; | 
| 97 | - if ($team->groupResults[$id][$this->what] > $max || $max === null) $max = $team->groupResults[$id][$this->what]; | |
| 98 | - if ($team->groupResults[$id][$this->what] < $min || $min === null) $min = $team->groupResults[$id][$this->what]; | |
| 116 | +				if ($team->groupResults[$id][$this->what] > $max || $max === null) { | |
| 117 | + $max = $team->groupResults[$id][$this->what]; | |
| 118 | + } | |
| 119 | +				if ($team->groupResults[$id][$this->what] < $min || $min === null) { | |
| 120 | + $min = $team->groupResults[$id][$this->what]; | |
| 121 | + } | |
| 99 | 122 | } | 
| 100 | 123 |  			switch (strtolower($operation)) { | 
| 101 | 124 | case 'sum': | 
| @@ -111,9 +134,11 @@ discard block | ||
| 111 | 134 | $comp = $min; | 
| 112 | 135 | break; | 
| 113 | 136 | } | 
| 137 | +		} elseif (gettype($groupsId) === 'string' && isset($team->groupResults[$groupsId])) { | |
| 138 | + $comp = $team->groupResults[$groupsId][$this->what]; | |
| 139 | +		} else { | |
| 140 | +			throw new \Exception("Couldn't find group of id ".print_r($groupsId, true)); | |
| 114 | 141 | } | 
| 115 | - elseif (gettype($groupsId) === 'string' && isset($team->groupResults[$groupsId])) $comp = $team->groupResults[$groupsId][$this->what]; | |
| 116 | -		else throw new \Exception("Couldn't find group of id ".print_r($groupsId, true)); | |
| 117 | 142 | |
| 118 | 143 |  		switch ($this->how) { | 
| 119 | 144 | case '>': return ($comp > $this->val); |