Merge pull request #9526 from MrPetovan/bug/9525-mastodon-emojis-tag

Restore expected implementation of JsonLD::fetchElementArray
This commit is contained in:
Michael Vogel 2020-11-12 05:47:46 +01:00 committed by GitHub
commit acae3df0a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 10 deletions

View file

@ -75,10 +75,15 @@ class Processor
*/ */
private static function replaceEmojis($body, array $emojis) private static function replaceEmojis($body, array $emojis)
{ {
foreach ($emojis as $emoji) { $body = strtr($body,
$replace = '[class=emoji mastodon][img=' . $emoji['href'] . ']' . $emoji['name'] . '[/img][/class]'; array_combine(
$body = str_replace($emoji['name'], $replace, $body); array_column($emojis, 'name'),
} array_map(function ($emoji) {
return '[class=emoji mastodon][img=' . $emoji['href'] . ']' . $emoji['name'] . '[/img][/class]';
}, $emojis)
)
);
return $body; return $body;
} }

View file

@ -1345,7 +1345,7 @@ class Receiver
$object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value'); $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
$object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment') ?? []); $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment') ?? []);
$object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag') ?? []); $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag') ?? []);
$object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji') ?? []); $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', null, '@type', 'toot:Emoji') ?? []);
$object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application'); $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
$object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value'); $object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
$object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id'); $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');

View file

@ -173,7 +173,7 @@ class JsonLD
* *
* @return array fetched element * @return array fetched element
*/ */
public static function fetchElementArray($array, $element, $key = null) public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
{ {
if (!isset($array[$element])) { if (!isset($array[$element])) {
return null; return null;
@ -187,10 +187,14 @@ class JsonLD
$elements = []; $elements = [];
foreach ($array[$element] as $entry) { foreach ($array[$element] as $entry) {
if (!is_array($entry) || (is_null($key) && is_array($entry))) { if (!is_array($entry) || is_null($key)) {
$elements[] = $entry; $item = $entry;
} elseif (!is_null($key) && isset($entry[$key])) { } elseif (isset($entry[$key])) {
$elements[] = $entry[$key]; $item = $entry[$key];
}
if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
$elements[] = $item;
} }
} }

View file

@ -62,6 +62,7 @@ class JsonLDTest extends TestCase
$data = JsonLD::fetchElementArray($object, 'field', '@id'); $data = JsonLD::fetchElementArray($object, 'field', '@id');
self::assertSame(['value3', 'value4'], $data); self::assertSame(['value3', 'value4'], $data);
} }
public function testFetchElementArrayFoundArrays() public function testFetchElementArrayFoundArrays()
{ {
$object = ['field' => [['subfield11' => 'value11', 'subfield12' => 'value12'], $object = ['field' => [['subfield11' => 'value11', 'subfield12' => 'value12'],
@ -74,6 +75,17 @@ class JsonLDTest extends TestCase
self::assertSame($expect, $data); self::assertSame($expect, $data);
} }
public function testFetchElementArrayTypeValue()
{
$object = ['field' => [['subfield11' => 'value11', 'subfield12' => 'value12'],
['subfield21' => 'value21', 'subfield22' => 'value22']]];
$expect = [['subfield11' => 'value11', 'subfield12' => 'value12']];
$data = JsonLD::fetchElementArray($object, 'field', null, 'subfield11', 'value11');
self::assertSame($expect, $data);
}
public function testFetchElementNotFound() public function testFetchElementNotFound()
{ {
$object = []; $object = [];