示例#1
0
 /**
  * Tests the serialization of graphs
  */
 public function testSerializeGraph()
 {
     // This is the expanded and flattened version of the test document
     // (the blank node labels have been renamed from _:t... to _:b...)
     $expected = Processor::loadDocument('[{
            "@id": "_:b0",
            "http://vocab.com/nested": [{
               "@value": "1.1"
            }]
         }, {
            "@id": "_:b1",
            "http://vocab.com/nested": [{
               "@value": "2.1"
            }]
         }, {
            "@id": "_:b2",
            "http://vocab.com/nested": [{
               "@value": "2.2"
            }]
         }, {
            "@id": "_:b3",
            "http://vocab.com/nested": [{
               "@value": "3.1"
            }]
         }, {
            "@id": "http://example.com/node/1",
            "@type": ["http://vocab.com/type/node"],
            "http://vocab.com/contains": [{
               "@id": "_:b0"
            }],
            "http://vocab.com/link": [{
               "@id": "http://example.com/node/2"
            }],
            "http://vocab.com/name": [{
               "@value": "1"
            }]
         }, {
            "@id": "http://example.com/node/2",
            "@type": ["http://vocab.com/type/nodeWithAliases"],
            "http://vocab.com/aliases": [{
               "@value": "node2"
            }, {
               "@value": 2,
               "@type": "http://www.w3.org/2001/XMLSchema#integer"
            }],
            "http://vocab.com/contains": [{
               "@id": "_:b1"
            }, {
               "@id": "_:b2"
            }],
            "http://vocab.com/lang": [{
               "@language": "en",
               "@value": "language-tagged string"
            }],
            "http://vocab.com/link": [{
               "@id": "http://example.com/node/3"
            }],
            "http://vocab.com/name": [{
               "@value": "2"
            }],
            "http://vocab.com/typed": [{
               "@type": "http://vocab.com/type/datatype",
               "@value": "typed value"
            }]
         }, {
            "@id": "http://example.com/node/3",
            "@type": ["http://vocab.com/type/node"],
            "http://vocab.com/contains": [{
               "@id": "_:b3"
            }],
            "http://vocab.com/lang": [{
               "@language": "en",
               "@value": "language-tagged string: en"
            }, {
               "@language": "de",
               "@value": "language-tagged string: de"
            }],
            "http://vocab.com/link": [{
               "@id": "http://example.com/node/1"
            }],
            "http://vocab.com/name": [{
               "@value": "3"
            }],
            "http://vocab.com/typed": [{
               "@type": "http://vocab.com/type/datatype",
               "@value": "typed value"
            }, {
               "@language": "ex:/type/otherDataType",
               "@value": "typed value"
            }, {
               "@language": "ex:/type/datatype",
               "@value": "typed value"
            }]
         }, {
            "@id": "http://vocab.com/type/node"
         }, {
            "@id": "http://vocab.com/type/nodeWithAliases"
         }]');
     $this->assertEquals($expected, $this->graph->toJsonLd(false), 'Serialize graph');
 }
示例#2
0
文件: Graph.php 项目: RdeWilde/JsonLD
 /**
  * {@inheritdoc}
  */
 public function merge(GraphInterface $graph)
 {
     $nodes = $graph->getNodes();
     $bnodeMap = array();
     foreach ($nodes as $node) {
         if ($node->isBlankNode()) {
             if (false === isset($bnodeMap[$node->getId()])) {
                 $bnodeMap[$node->getId()] = $this->createNode();
             }
             $n = $bnodeMap[$node->getId()];
         } else {
             $n = $this->createNode($node->getId());
         }
         foreach ($node->getProperties() as $property => $values) {
             if (false === is_array($values)) {
                 $values = array($values);
             }
             foreach ($values as $val) {
                 if ($val instanceof NodeInterface) {
                     // If the value is another node, we just need to
                     // create a reference to the corresponding node
                     // in this graph. The properties will be merged
                     // in the outer loop
                     if ($val->isBlankNode()) {
                         if (false === isset($bnodeMap[$val->getId()])) {
                             $bnodeMap[$val->getId()] = $this->createNode();
                         }
                         $val = $bnodeMap[$val->getId()];
                     } else {
                         $val = $this->createNode($val->getId());
                     }
                 } elseif (is_object($val)) {
                     // Clone typed values and language-tagged strings
                     $val = clone $val;
                 }
                 $n->addPropertyValue($property, $val);
             }
         }
     }
     return $this;
 }