Jforrester has submitted this change and it was merged.
Change subject: Tag: Allow appendContent and prependContent to accept an array
......................................................................
Tag: Allow appendContent and prependContent to accept an array
This is very useful when creating a dynamic group of items to append
to some element or tag, instead of having to call call_user_func_array
when these are needed.
See the usage in GroupElement.php#80 for example, as adapted for the
new usage in this commit.
Change-Id: I2903fddfae9e286922109fc5e653e0fe344912cf
---
M php/Tag.php
M php/mixins/GroupElement.php
2 files changed, 28 insertions(+), 6 deletions(-)
Approvals:
Bartosz Dziewoński: Looks good to me, approved
Jforrester: Verified; Looks good to me, approved
diff --git a/php/Tag.php b/php/Tag.php
index 95fe115..b65f5fd 100644
--- a/php/Tag.php
+++ b/php/Tag.php
@@ -167,7 +167,14 @@
/**
* Add content to the end.
*
- * Accepts variadic arguments (the $content argument can be repeated
any number of times).
+ * Accepts either variadic arguments (the $content argument can be
repeated any number of times)
+ * or an array of arguments.
+ *
+ * For example, these uses are valid:
+ * * $tag->appendContent( [ $element1, $element2 ] );
+ * * $tag->appendContent( $element1, $element2 );
+ * This, however, is not acceptable
+ * * $tag->appendContent( [ $element1, $element2 ], $element3 );
*
* @param string|Tag|HtmlSnippet $content Content to append. Strings
will be HTML-escaped
* for output, use a HtmlSnippet instance to prevent that.
@@ -175,14 +182,25 @@
*/
public function appendContent( /* $content... */ ) {
$contents = func_get_args();
- $this->content = array_merge( $this->content, $contents );
+ if ( is_array( $contents[ 0 ] ) ) {
+ $this->content = array_merge( $this->content,
$contents[ 0 ] );
+ } else {
+ $this->content = array_merge( $this->content, $contents
);
+ }
return $this;
}
/**
* Add content to the beginning.
*
- * Accepts variadic arguments (the $content argument can be repeated
any number of times).
+ * Accepts either variadic arguments (the $content argument can be
repeated any number of times)
+ * or an array of arguments.
+ *
+ * For example, these uses are valid:
+ * * $tag->prependContent( [ $element1, $element2 ] );
+ * * $tag->prependContent( $element1, $element2 );
+ * This, however, is not acceptable
+ * * $tag->prependContent( [ $element1, $element2 ], $element3 );
*
* @param string|Tag|HtmlSnippet $content Content to prepend. Strings
will be HTML-escaped
* for output, use a HtmlSnippet instance to prevent that.
@@ -190,7 +208,11 @@
*/
public function prependContent( /* $content... */ ) {
$contents = func_get_args();
- array_splice( $this->content, 0, 0, $contents );
+ if ( is_array( $contents[ 0 ] ) ) {
+ array_splice( $this->content, 0, 0, $contents[ 0 ] );
+ } else {
+ array_splice( $this->content, 0, 0, $contents );
+ }
return $this;
}
diff --git a/php/mixins/GroupElement.php b/php/mixins/GroupElement.php
index 1409c0b..93b730d 100644
--- a/php/mixins/GroupElement.php
+++ b/php/mixins/GroupElement.php
@@ -77,7 +77,7 @@
// Update actual target element contents to reflect our list
$this->target->clearContent();
- call_user_func_array( [ $this->target, 'appendContent' ],
$this->items );
+ $this->target->appendContent( $this->items );
return $this;
}
@@ -99,7 +99,7 @@
// Update actual target element contents to reflect our list
$this->target->clearContent();
- call_user_func_array( [ $this->target, 'appendContent' ],
$this->items );
+ $this->target->appendContent( $this->items );
return $this;
}
--
To view, visit https://gerrit.wikimedia.org/r/279059
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I2903fddfae9e286922109fc5e653e0fe344912cf
Gerrit-PatchSet: 5
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <[email protected]>
Gerrit-Reviewer: Bartosz Dziewoński <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits