Create ApiBase::requireAtLeastOneParameter helper
authorTyler Anthony Romeo <tylerromeo@gmail.com>
Tue, 21 May 2013 00:51:23 +0000 (00:51 +0000)
committerReedy <reedy@wikimedia.org>
Thu, 23 Jan 2014 21:49:49 +0000 (21:49 +0000)
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

includes/api/ApiBase.php

index 40edf4e..c1a4cd3 100644 (file)
@@ -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: