Hello Ori.livneh,
I'd like you to do a code review. Please visit
https://gerrit.wikimedia.org/r/180771
to review the following change.
Change subject: Allow to get JsonContent data as associative array
......................................................................
Allow to get JsonContent data as associative array
The deprecated JsonContent::getJsonData method allowed to get data as
associative array. The suggested replacement JsonContent::getData no
longer allows this. As this is causing issues for extensions as
EventLogging, we teach JsonContent::getData to allow returning
associative arrays.
Change-Id: Ifea3096d3fbc03dd81a7093fc6ee3a521df48305
---
M includes/content/JsonContent.php
M tests/phpunit/includes/content/JsonContentTest.php
2 files changed, 28 insertions(+), 1 deletion(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/71/180771/1
diff --git a/includes/content/JsonContent.php b/includes/content/JsonContent.php
index ff3b25b..d871ae7 100644
--- a/includes/content/JsonContent.php
+++ b/includes/content/JsonContent.php
@@ -43,9 +43,14 @@
/**
* Decodes the JSON string into a PHP object.
*
+ * @param bool $assoc When true, returned objects will be converted
into associative arrays.
+ *
* @return Status
*/
- public function getData() {
+ public function getData( $assoc = false ) {
+ if ( $assoc ) {
+ return FormatJson::parse( $this->getNativeData(),
FormatJson::FORCE_ASSOC );
+ }
if ( $this->jsonParse === null ) {
$this->jsonParse = FormatJson::parse(
$this->getNativeData() );
}
diff --git a/tests/phpunit/includes/content/JsonContentTest.php
b/tests/phpunit/includes/content/JsonContentTest.php
index 0ad8ecc..f2aba51 100644
--- a/tests/phpunit/includes/content/JsonContentTest.php
+++ b/tests/phpunit/includes/content/JsonContentTest.php
@@ -139,4 +139,26 @@
$this->assertInstanceOf( 'ParserOutput', $parserOutput );
$this->assertEquals( $expected, $parserOutput->getText() );
}
+
+ public function testGetDataAsArray() {
+ $obj = new JsonContent( '{ "foo": { "bar": "baz", "qux": 1 },
"quux": 2}' );
+ $dataStatus = $obj->getData( true );
+
+ $this->assertTrue( $dataStatus->isGood() );
+
+ $data = $dataStatus->getValue();
+ $expected = array( 'foo' => array( 'bar' => 'baz', 'qux' => 1
), 'quux' => 2 );
+ $this->assertEquals( $expected, $data );
+ }
+
+ public function testGetDataAsObject() {
+ $obj = new JsonContent( '{ "foo": { "bar": "baz", "qux": 1 },
"quux": 2}' );
+ $dataStatus = $obj->getData();
+
+ $this->assertTrue( $dataStatus->isGood() );
+
+ $data = $dataStatus->getValue();
+ $expected = (object) array( 'foo' => (object) array( 'bar' =>
'baz', 'qux' => 1 ), 'quux' => 2 );
+ $this->assertEquals( $expected, $data );
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/180771
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifea3096d3fbc03dd81a7093fc6ee3a521df48305
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: QChris <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits