forked from joostdekeijzer/mw-oauth2-client-extension
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c02026a
commit b1431d4
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
class JsonHelper | ||
{ | ||
/** | ||
* @param $array "A array where value extracted from" | ||
* @param $path "Path indicates the path to extract value" | ||
* @return mixed "the extracted value, maybe null" | ||
*/ | ||
public static function extractValue($array, $path) | ||
{ | ||
foreach (explode('.', $path) as $key) { | ||
if (is_null($array)) break; | ||
try { | ||
$array = $array[$key]; | ||
} catch (Exception $e) { | ||
$array = null; | ||
} | ||
} | ||
return $array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../../JsonHelper.php'; | ||
|
||
class JsonHelperTest extends MediaWikiTestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function testExtractValue() | ||
{ | ||
$json_array = array( | ||
"user" => array( | ||
"username" => "username", | ||
"email" => "[email protected]" | ||
) | ||
); | ||
self::assertEquals("[email protected]", JsonHelper::extractValue($json_array, "user.email")); | ||
self::assertEquals("username", JsonHelper::extractValue($json_array, "user.username")); | ||
self::assertEquals("username", JsonHelper::extractValue($json_array, "user")["username"]); | ||
self::assertNull(JsonHelper::extractValue($json_array, "user.emails")); | ||
self::assertNull(JsonHelper::extractValue($json_array, "$.user.email")); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
parent::tearDown(); | ||
} | ||
} |