diff options
author | emkael <emkael@tlen.pl> | 2025-02-13 22:53:33 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2025-02-13 22:53:33 +0100 |
commit | c009e1827871e9dad29697e390f7c441dc34c0ea (patch) | |
tree | 9c6b8b03ace8058a08f1bc02bedcb4f73985b84d | |
parent | 479ca8d010303ce51439b3dd387c64c2fa6688a8 (diff) |
Refactoring: getting rid of relying on $this->parameters throughout validation and parsing.
Passing parameters as method arguments instead
-rw-r--r-- | http/api-inc.php | 161 |
1 files changed, 91 insertions, 70 deletions
diff --git a/http/api-inc.php b/http/api-inc.php index 1f90c07..f4fa01d 100644 --- a/http/api-inc.php +++ b/http/api-inc.php @@ -155,29 +155,28 @@ class ApiPkl { protected $parameters; function __construct($parameters) { - $this->parameters = $parameters; - $this->check_parameters($parameters); + $parameters = $this->check_parameters($parameters); $this->parameters = $this->parse_parameters($parameters); } - function ensure_parameters($params) { - foreach ($params as $param) { - if (!isset($this->parameters[$param])) { + function ensure_parameters($parameters, $mandatory) { + foreach ($mandatory as $param) { + if (!isset($parameters[$param])) { throw new ParametersException( sprintf('Missing parameter: %s', $param) ); } - if (!is_numeric($this->parameters[$param])) { + if (!is_numeric($parameters[$param])) { throw new ParametersException( sprintf('Parameter: %s is not a numeric value (%s)', - $param, $this->parameters[$param]) + $param, $parameters[$param]) ); } } } - function check_values($parameters, $params) { - foreach ($params as $param => $test) { + function check_values($parameters, $tests) { + foreach ($tests as $param => $test) { if (!$test($parameters[$param])) { throw new ParametersException( sprintf('Parameter: %s has incorrect value (%s)', @@ -187,9 +186,9 @@ class ApiPkl { } } - function check_parameters() { - $this->ensure_parameters(array('type', 'contestants')); - $this->check_values($this->parameters, array( + function check_parameters($parameters) { + $this->ensure_parameters($parameters, array('type', 'contestants')); + $this->check_values($parameters, array( 'type' => function($r) { return is_integer_like($r) && intval($r) > 0; }, @@ -197,27 +196,28 @@ class ApiPkl { return is_integer_like($r) && intval($r) > 0; } )); - if (isset($this->parameters['players'])) { - $this->check_values($this->parameters, array( + if (isset($parameters['players'])) { + $this->check_values($parameters, array( 'players' => function($r) { return is_integer_like($r) && intval($r) > 0; } )); } - if (!isset($this->parameters['manual']) - || !isset($this->parameters['manual']['min_points'])) { - $this->ensure_parameters(array('title_sum')); - $this->check_values($this->parameters, array( + if (!isset($parameters['manual']) + || !isset($parameters['manual']['min_points'])) { + $this->ensure_parameters($parameters, array('title_sum')); + $this->check_values($parameters, array( 'title_sum' => function($r) { return floatval($r) >= 0; } )); - if (!isset($this->parameters['manual']) - || !isset($this->parameters['manual']['tournament_weight'])) { + if (!isset($parameters['manual']) + || !isset($parameters['manual']['tournament_weight'])) { $this->ensure_parameters( + $parameters, array('tournament_rank', 'over39_boards') ); - $this->check_values($this->parameters, array( + $this->check_values($parameters, array( 'tournament_rank' => function($r) { return is_integer_like($r) && in_array(intval($r), array( @@ -238,32 +238,33 @@ class ApiPkl { } )); } else { - $this->check_values($this->parameters['manual'], array( + $this->check_values($parameters['manual'], array( 'tournament_weight' => function($r) { return is_integer_like($r) && intval($r) > 0; } )); } } else { - $this->check_values($this->parameters['manual'], array( + $this->check_values($parameters['manual'], array( 'min_points' => function($r) { return is_integer_like($r) && intval($r) >= 0; } )); } - if (isset($this->parameters['manual']) - && isset($this->parameters['manual']['players_coefficient'])) { - $this->check_values($this->parameters['manual'], array( + if (isset($parameters['manual']) + && isset($parameters['manual']['players_coefficient'])) { + $this->check_values($parameters['manual'], array( 'players_coefficient' => function($r) { return floatval($r) >= 0; } )); } + return $parameters; } - function parse_parameters() { + function parse_parameters($parameters) { $return = array(); - $return['type'] = intval($this->parameters['type']); + $return['type'] = intval($parameters['type']); if (!in_array($return['type'], array( ApiPkl::TYPE_INDIVIDUAL, ApiPkl::TYPE_PAIRS, ApiPkl::TYPE_TEAMS ))) { @@ -273,8 +274,8 @@ class ApiPkl { ); } if (($return['type'] != ApiPkl::TYPE_PAIRS) - && isset($this->parameters['tournament_rank']) - && $this->parameters['tournament_rank'] == ApiPkl::RANK_KMP) { + && isset($parameters['tournament_rank']) + && $parameters['tournament_rank'] == ApiPkl::RANK_KMP) { throw new ParametersException( sprintf('Parameter: type has incorrect value (%s) for KMP tournament', $return['type']) @@ -283,12 +284,12 @@ class ApiPkl { if (isset($this->parameters['boards'])) { $return['boards'] = intval($this->parameters['boards']); } - $return['contestants'] = intval($this->parameters['contestants']); - $return['players'] = isset($this->parameters['players']) - ? intval($this->parameters['players']) - : intval($this->parameters['contestants']) * $return['type']; - $return['title_sum'] = isset($this->parameters['title_sum']) - ? floatval($this->parameters['title_sum']) + $return['contestants'] = intval($parameters['contestants']); + $return['players'] = isset($parameters['players']) + ? intval($parameters['players']) + : intval($parameters['contestants']) * $return['type']; + $return['title_sum'] = isset($parameters['title_sum']) + ? floatval($parameters['title_sum']) : 0.0; $weights = array( array( @@ -317,13 +318,13 @@ class ApiPkl { ) ); $return['tournament_weight'] = 0; - if (isset($this->parameters['manual']) - && isset($this->parameters['manual']['tournament_weight'])) { - $return['tournament_weight'] = intval($this->parameters['manual']['tournament_weight']); + if (isset($parameters['manual']) + && isset($parameters['manual']['tournament_weight'])) { + $return['tournament_weight'] = intval($parameters['manual']['tournament_weight']); } else { - if (isset($this->parameters['over39_boards']) - && isset($this->parameters['tournament_rank'])) { - $return['tournament_weight'] = $weights[intval($this->parameters['over39_boards'])][intval($this->parameters['tournament_rank'])]; + if (isset($parameters['over39_boards']) + && isset($parameters['tournament_rank'])) { + $return['tournament_weight'] = $weights[intval($parameters['over39_boards'])][intval($parameters['tournament_rank'])]; } } $min_points = array( @@ -352,15 +353,15 @@ class ApiPkl { ApiPkl::RANK_BNET => static::POINTS_BNET_OVER40 ) ); - $return['min_points'] = $min_points[intval($this->parameters['over39_boards'])][intval($this->parameters['tournament_rank'])]; - if (isset($this->parameters['manual']) - && isset($this->parameters['manual']['min_points'])) { - $return['min_points'] = intval($this->parameters['manual']['min_points']); + $return['min_points'] = $min_points[intval($parameters['over39_boards'])][intval($parameters['tournament_rank'])]; + if (isset($parameters['manual']) + && isset($parameters['manual']['min_points'])) { + $return['min_points'] = intval($parameters['manual']['min_points']); } $return['players_coefficient'] = static::PLAYERS_COEFFICIENT; - if (isset($this->parameters['manual']) - && isset($this->parameters['manual']['players_coefficient'])) { - $return['players_coefficient'] = floatval($this->parameters['manual']['players_coefficient']); + if (isset($parameters['manual']) + && isset($parameters['manual']['players_coefficient'])) { + $return['players_coefficient'] = floatval($parameters['manual']['players_coefficient']); } $return['points_cutoffs'] = array( array(0.0, 1.0), @@ -368,10 +369,10 @@ class ApiPkl { array(static::POINTS_CUTOFF_2, static::POINTS_CUTOFF_2_VALUE), array(static::POINTS_CUTOFF_3, static::POINTS_CUTOFF_3_VALUE) ); - if (isset($this->parameters['manual']) - && isset($this->parameters['manual']['points_cutoffs']) - && is_array($this->parameters['manual']['points_cutoffs'])) { - $return['points_cutoffs'] = $this->parameters['manual']['points_cutoffs']; + if (isset($parameters['manual']) + && isset($parameters['manual']['points_cutoffs']) + && is_array($parameters['manual']['points_cutoffs'])) { + $return['points_cutoffs'] = $parameters['manual']['points_cutoffs']; } recursive_ksort($return['points_cutoffs']); if ($return['points_cutoffs'][0][0] != 0.0) { @@ -555,26 +556,39 @@ class ApiPklV3 extends ApiPklV2 { const BNET_POINTS_FACTOR_QUOTIENT = 27.0; const BNET_POINTS_FACTOR_CAP = 1.0; - protected function _bridgenet_parameters() { - $this->ensure_parameters(array('boards', 'type')); - if ($this->parameters['type'] == ApiPkl::TYPE_TEAMS) { + protected function _bridgenet_parameters($parameters) { + $this->ensure_parameters($parameters, array('boards', 'type')); + if ($parameters['type'] == ApiPkl::TYPE_TEAMS) { throw new ParametersException( sprintf('Parameter: type has incorrect value (%s) for BridgeNET tournament', - $this->parameters['type']) + $parameters['type']) ); } + return $parameters; } - function check_parameters() { - if (isset($this->parameters['tournament_rank']) - && $this->parameters['tournament_rank'] == ApiPkl::RANK_BNET) { - $this->_bridgenet_parameters(); - if (isset($this->parameters['manual'])) { - unset($this->parameters['manual']); + function check_parameters($parameters) { + // Checks for BridgeNET parameters if type == BridgeNET + if (isset($parameters['tournament_rank']) + && $parameters['tournament_rank'] == ApiPkl::RANK_BNET) { + $this->_bridgenet_parameters($parameters); + if (isset($parameters['manual'])) { + unset($parameters['manual']); } } - $this->_set_board_count(); - return parent::check_parameters(); + // Newly introduced 'boards' parameter overrides 'over39_boards' + $parameters = $this->_set_board_count($parameters); + return parent::check_parameters($parameters); + } + + function parse_parameters($parameters) { + $return = parent::parse_parameters($parameters); + // 'boards' parameter needs to be maintained in $this->parameters + // as it's used in BridgeNET calculations + if (isset($parameters['boards'])) { + $return['boards'] = intval($parameters['boards']); + } + return $return; } function calculate_bridgenet_points() { @@ -592,16 +606,23 @@ class ApiPklV3_1 extends ApiPklV3 { const BNET_MINIMUM_BOARD_COUNT = 20; - protected function _bridgenet_parameters() { - $this->ensure_parameters(array('boards')); - $this->check_values($this->parameters, array( + protected function _bridgenet_parameters($parameters) { + // 'boards' are mandatory for BridgeNET + $this->ensure_parameters($parameters, array('boards')); + $this->check_values($parameters, array( 'boards' => function($r) { return is_integer_like($r); } )); - if (intval($this->parameters['boards']) < static::BNET_MINIMUM_BOARD_COUNT) { + // There's no longer a check against type == TEAMS + + // But there's one for minimum board count + if (intval($parameters['boards']) < static::BNET_MINIMUM_BOARD_COUNT) { throw new ParametersException( - 'At least 20 boards must be played in BridgeNET tournaments' + sprintf( + 'At least %d boards must be played in BridgeNET tournaments', + static::BNET_MINIMUM_BOARD_COUNT + ) ); } } |