3 * @file src/Util/JsonLD.php
5 namespace Friendica\Util;
7 use Friendica\Core\Cache;
8 use digitalbazaar\jsonld as DBJsonLD;
11 * @brief This class contain methods to work with JsonLD data
15 public static function documentLoader($url)
19 $x = debug_backtrace();
22 if ($n['function'] === __FUNCTION__) {
29 logger('jsonld bomb detected at: ' . $url);
33 $result = Cache::get('documentLoader:' . $url);
34 if (!is_null($result)) {
38 $data = jsonld_default_document_loader($url);
39 Cache::set('documentLoader:' . $url, $data, CACHE_DAY);
43 private static function objectify($element)
45 if (is_array($element)) {
46 $keys = array_keys($element);
47 if (is_int(array_pop($keys))) {
48 return array_map('objectify', $element);
50 return (object)array_map('objectify', $element);
57 public static function normalize($json)
59 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
61 // $jsonobj = array_map('Friendica\Util\JsonLD::objectify', $json);
62 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
64 return jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
67 public static function compact($json)
69 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
71 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams',
72 'w3sec' => 'https://w3id.org/security',
73 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
74 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
75 'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
77 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
79 $compacted = jsonld_compact($jsonobj, $context);
81 return json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
84 public static function fetchElement($array, $element, $key, $type = null, $type_value = null)
90 if (empty($array[$element])) {
94 if (is_string($array[$element])) {
95 return $array[$element];
98 if (is_null($type_value)) {
99 if (!empty($array[$element][$key])) {
100 return $array[$element][$key];
103 if (!empty($array[$element][0][$key])) {
104 return $array[$element][0][$key];
110 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
111 return $array[$element][$key];
114 /// @todo Add array search