function _flatten(&$result, $subject, $delimiter = '_', $prefix = '') { assertTrue(is_array($result) || is_object($result)); assertTrue(is_array($subject) || is_object($subject)); foreach ($subject as $key => $value) { if (is_array($value) || is_object($value)) { _flatten($result, $value, $delimiter, $prefix . ($prefix ? $delimiter : '') . $key); } else { if (is_array($result)) { $result[$prefix . ($prefix ? $delimiter : '') . $key] = $value; } else { $result->{$prefix . ($prefix ? $delimiter : '') . $key} = $value; } } } }
/** * Normalizes a JSON-LD object. * * @param input the JSON-LD object to normalize. * * @return the normalized JSON-LD object. */ public function normalize($input) { $rval = array(); // TODO: validate context if ($input !== null) { // expand input $expanded = _expand(new stdClass(), null, $input, true); // assign names to unnamed bnodes $this->nameBlankNodes($expanded); // flatten $subjects = new stdClass(); _flatten(null, null, $expanded, $subjects); // append subjects with sorted properties to array foreach ($subjects as $key => $s) { $sorted = new stdClass(); $keys = array_keys((array) $s); sort($keys); foreach ($keys as $i => $k) { $sorted->{$k} = $s->{$k}; } $rval[] = $sorted; } // canonicalize blank nodes $this->canonicalizeBlankNodes($rval); // sort output usort($rval, '_compareIris'); } return $rval; }