Fix expected implementation of JsonLD::fetchElementArray

- Added expected field value matching
This commit is contained in:
Hypolite Petovan 2020-11-11 11:19:08 -05:00
parent 61dcd62921
commit fa003eaa84
2 changed files with 10 additions and 6 deletions

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;
} }
} }