From: Tyler Anthony Romeo Date: Tue, 21 May 2013 00:51:23 +0000 (+0000) Subject: Create ApiBase::requireAtLeastOneParameter helper X-Git-Tag: 1.31.0-rc.0~17184 X-Git-Url: http://git.cyclocoop.org/%7B%7B%20url_for%28%27admin_vote_add%27%29%20%7D%7D?a=commitdiff_plain;h=5e7a9c6f8e66de344e2e2f10c7a03d9152f37b77;p=lhc%2Fweb%2Fwiklou.git Create ApiBase::requireAtLeastOneParameter helper Added new ApiBase::requireAtLeastOneParameter() helper method for modules that need one or more of a combination of parameters. The associated help message function has also been added. Change-Id: Iae6649ed503fdbf14b313d0be1a82f3dca8d7162 --- diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 40edf4ecce..c1a4cd353e 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -801,6 +801,38 @@ abstract class ApiBase extends ContextSource { ); } + /** + * Die if none of a certain set of parameters is set and not false. + * @param array $params of parameter names + */ + public function requireAtLeastOneParameter( $params ) { + $required = func_get_args(); + array_shift( $required ); + $p = $this->getModulePrefix(); + + $intersection = array_intersect( array_keys( array_filter( $params, + array( $this, "parameterNotEmpty" ) ) ), $required ); + + if ( count( $intersection ) == 0 ) { + $this->dieUsage( "At least one of the parameters {$p}" . implode( ", {$p}", $required ) . ' is required', "{$p}missingparam" ); + } + } + + /** + * Generates the possible errors requireAtLeastOneParameter() can die with + * + * @param $params array + * @return array + */ + public function getRequireAtLeastOneParameterErrorMessages( $params ) { + $p = $this->getModulePrefix(); + $params = implode( ", {$p}", $params ); + + return array( + array( 'code' => "{$p}missingparam", 'info' => "At least one of the parameters {$p}{$params} is required" ), + ); + } + /** * @param $params array * @param bool|string $load Whether load the object's state from the database: