Skip to content

Instantly share code, notes, and snippets.

@ryandemmer
Created June 30, 2020 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryandemmer/735b14e894c8aea3037dd77c4fc6eb0d to your computer and use it in GitHub Desktop.
Save ryandemmer/735b14e894c8aea3037dd77c4fc6eb0d to your computer and use it in GitHub Desktop.
buildListFromJson
private static function buildListFromJson($nodes)
{
$data = array();
$nodes = isset($nodes['@graph']) ? $nodes['@graph'] : array();
$properties = array_map(function ($node) {
if ($node['@type'] == 'rdf:Property') {
$id = str_replace('http://schema.org/', '', $node['@id']);
$comment = '';
if (isset($node['rdfs:comment'])) {
$comment = str_replace('http://schema.org/', '', $node['rdfs:comment']);
}
$entry = array(
'label' => $id,
'comment' => $comment,
);
foreach (['domainIncludes', 'rangeIncludes'] as $prop) {
$key = 'http://schema.org/' . $prop;
if (isset($node[$key])) {
$val = $node[$key];
if (!is_array($val)) {
$subclass = str_replace('http://schema.org/', '', $val['@id']);
return array(
$subclass => array(
$prop => array($entry),
),
);
} else {
foreach ($val as $subclass) {
if (!is_string($subclass)) {
$subclass = $subclass['@id'];
}
$subclass = str_replace('http://schema.org/', '', $subclass);
return array(
$subclass => array(
$prop => array($entry),
),
);
}
}
}
}
}
return false;
}, $nodes);
foreach ($nodes as $node) {
$id = str_replace('http://schema.org/', '', $node['@id']);
if ($node['@type'] != 'rdfs:Class') {
continue;
}
if (isset($node['rdfs:subClassOf'])) {
continue;
}
$comment = '';
if (isset($node['rdfs:comment'])) {
$comment = str_replace('http://schema.org/', '', $node['rdfs:comment']);
}
$values = array(
'resource' => $id,
'comment' => $comment,
);
$data[] = $values;
}
$count = count($nodes);
do {
foreach ($nodes as $node) {
$id = str_replace('http://schema.org/', '', $node['@id']);
if ($node['@type'] != 'rdfs:Class') {
$count--;
continue;
}
if (!isset($node['rdfs:subClassOf'])) {
$count--;
continue;
}
if (self::array_search_position($id, $data)) {
$count--;
continue;
}
$comment = '';
if (isset($node['rdfs:comment'])) {
$comment = str_replace('http://schema.org/', '', $node['rdfs:comment']);
}
$values = array(
'resource' => $id,
'comment' => $comment,
'subClassOf' => array(),
);
$items = (array) $node['rdfs:subClassOf'];
foreach ($items as $item) {
if (is_string($item)) {
$values['subClassOf'][] = str_replace('http://schema.org/', '', $item);
} else {
$values['subClassOf'][] = str_replace('http://schema.org/', '', $item['@id']);
}
}
$pos = false;
foreach($values['subClassOf'] as $cls) {
$pos = self::array_search_position($cls, $data);
if ($pos !== false) {
self::array_insert($data, $pos + 1, array($id => $values));
}
}
if ($pos) {
$count--;
}
}
} while ($count > 0);
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment