Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / htmlform / HTMLForm.php
index acf64db..58c1602 100644 (file)
  * @file
  */
 
-use Wikimedia\ObjectFactory;
-
 /**
  * Object handling generic submission, CSRF protection, layout and
- * other logic for UI forms. in a reusable manner.
+ * other logic for UI forms in a reusable manner.
  *
  * In order to generate the form, the HTMLForm object takes an array
  * structure detailing the form fields available. Each element of the
@@ -182,11 +180,11 @@ class HTMLForm extends ContextSource {
        protected $mMessagePrefix;
 
        /** @var HTMLFormField[] */
-       protected $mFlatFields;
-
-       protected $mFieldTree;
+       protected $mFlatFields = [];
+       protected $mFieldTree = [];
        protected $mShowReset = false;
        protected $mShowSubmit = true;
+       /** @var string[] */
        protected $mSubmitFlags = [ 'primary', 'progressive' ];
        protected $mShowCancel = false;
        protected $mCancelTarget;
@@ -244,6 +242,10 @@ class HTMLForm extends ContextSource {
 
        protected $mUseMultipart = false;
        protected $mHiddenFields = [];
+       /**
+        * @var array[]
+        * @phan-var array<array{name:string,value:string,label-message?:string,label?:string,label-raw?:string,id?:string,attribs?:array,flags?:string|string[],framed?:bool}>
+        */
        protected $mButtons = [];
 
        protected $mWrapperLegend = false;
@@ -296,6 +298,7 @@ class HTMLForm extends ContextSource {
         *
         * @param string $displayFormat
         * @param mixed $arguments,... Additional arguments to pass to the constructor.
+        * @suppress PhanCommentParamWithoutRealParam HHVM bug T228695#5450847
         * @return HTMLForm
         */
        public static function factory( $displayFormat/*, $arguments...*/ ) {
@@ -304,12 +307,11 @@ class HTMLForm extends ContextSource {
 
                switch ( $displayFormat ) {
                        case 'vform':
-                               return ObjectFactory::constructClassInstance( VFormHTMLForm::class, $arguments );
+                               return new VFormHTMLForm( ...$arguments );
                        case 'ooui':
-                               return ObjectFactory::constructClassInstance( OOUIHTMLForm::class, $arguments );
+                               return new OOUIHTMLForm( ...$arguments );
                        default:
-                               /** @var HTMLForm $form */
-                               $form = ObjectFactory::constructClassInstance( self::class, $arguments );
+                               $form = new self( ...$arguments );
                                $form->setDisplayFormat( $displayFormat );
                                return $form;
                }
@@ -318,7 +320,8 @@ class HTMLForm extends ContextSource {
        /**
         * Build a new HTMLForm from an array of field attributes
         *
-        * @param array $descriptor Array of Field constructs, as described above
+        * @param array $descriptor Array of Field constructs, as described
+        *      in the class documentation
         * @param IContextSource|null $context Available since 1.18, will become compulsory in 1.18.
         *     Obviates the need to call $form->setTitle()
         * @param string $messagePrefix A prefix to go in front of default messages
@@ -346,11 +349,23 @@ class HTMLForm extends ContextSource {
                        $this->displayFormat = 'div';
                }
 
-               // Expand out into a tree.
+               $this->addFields( $descriptor );
+       }
+
+       /**
+        * Add fields to the form
+        *
+        * @since 1.34
+        *
+        * @param array $descriptor Array of Field constructs, as described
+        *      in the class documentation
+        * @return HTMLForm
+        */
+       public function addFields( $descriptor ) {
                $loadedDescriptor = [];
-               $this->mFlatFields = [];
 
                foreach ( $descriptor as $fieldname => $info ) {
+
                        $section = $info['section'] ?? '';
 
                        if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
@@ -374,7 +389,9 @@ class HTMLForm extends ContextSource {
                        $this->mFlatFields[$fieldname] = $field;
                }
 
-               $this->mFieldTree = $loadedDescriptor;
+               $this->mFieldTree = array_merge( $this->mFieldTree, $loadedDescriptor );
+
+               return $this;
        }
 
        /**
@@ -457,7 +474,8 @@ class HTMLForm extends ContextSource {
         * @since 1.23
         *
         * @param string $fieldname Name of the field
-        * @param array &$descriptor Input Descriptor, as described above
+        * @param array &$descriptor Input Descriptor, as described
+        *      in the class documentation
         *
         * @throws MWException
         * @return string Name of a HTMLFormField subclass
@@ -484,7 +502,8 @@ class HTMLForm extends ContextSource {
         * Initialise a new Object for the field
         *
         * @param string $fieldname Name of the field
-        * @param array $descriptor Input Descriptor, as described above
+        * @param array $descriptor Input Descriptor, as described
+        *      in the class documentation
         * @param HTMLForm|null $parent Parent instance of HTMLForm
         *
         * @throws MWException
@@ -968,6 +987,9 @@ class HTMLForm extends ContextSource {
         *  - attribs: (array, optional) Additional HTML attributes.
         *  - flags: (string|string[], optional) OOUI flags.
         *  - framed: (boolean=true, optional) OOUI framed attribute.
+        * @codingStandardsIgnoreStart
+        * @phan-param array{name:string,value:string,label-message?:string,label?:string,label-raw?:string,id?:string,attribs?:array,flags?:string|string[],framed?:bool} $data
+        * @codingStandardsIgnoreEnd
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
        public function addButton( $data ) {
@@ -1307,7 +1329,7 @@ class HTMLForm extends ContextSource {
                }
 
                return $elementstr
-                       ? Html::rawElement( 'div', [ 'class' => $elementsType ], $elementstr )
+                       ? Html::rawElement( 'div', [ 'class' => $elementsType . 'box' ], $elementstr )
                        : '';
        }