示例#1
0
 /** Get or create a resource stored in a graph
  *
  * If the resource did not previously exist, then a new resource will
  * be created. If you provide an RDF type and that type is registered
  * with the EasyRdf_TypeMapper, then the resource will be an instance
  * of the class registered.
  *
  * @param  string  $uri    The URI of the resource
  * @param  mixed   $types  RDF type of a new resouce (e.g. foaf:Person)
  * @return object EasyRdf_Resouce
  */
 public function resource($uri, $types = array())
 {
     if (!is_string($uri) or $uri == null or $uri == '') {
         throw new InvalidArgumentException("\$uri should be a string and cannot be null or empty");
     }
     // Expand the URI if it is shortened
     $uri = EasyRdf_Namespace::expand($uri);
     // Convert types to an array if it isn't one
     if (!$types) {
         $types = array();
     } else {
         if (!is_array($types)) {
             $types = array($types);
         }
     }
     // Create resource object if it doesn't already exist
     if (!array_key_exists($uri, $this->_resources)) {
         $resClass = 'EasyRdf_Resource';
         foreach ($types as $type) {
             $class = EasyRdf_TypeMapper::get($type);
             if ($class != null) {
                 $resClass = $class;
                 break;
             }
         }
         $this->_resources[$uri] = new $resClass($uri);
     }
     // Add the rdf:type triples
     foreach ($types as $type) {
         $type = $this->resource($type);
         $this->_resources[$uri]->add('rdf:type', $type);
     }
     return $this->_resources[$uri];
 }
示例#2
0
 /** Work out the class to instantiate a resource as
  *  @ignore
  */
 protected function classForResource($uri)
 {
     $resClass = 'EasyRdf_Resource';
     $rdfType = EasyRdf_Namespace::expand('rdf:type');
     if (isset($this->_index[$uri][$rdfType])) {
         foreach ($this->_index[$uri][$rdfType] as $type) {
             if ($type['type'] == 'uri' or $type['type'] == 'bnode') {
                 $class = EasyRdf_TypeMapper::get($type['value']);
                 if ($class != null) {
                     $resClass = $class;
                     break;
                 }
             }
         }
     }
     return $resClass;
 }
 public function testDelete()
 {
     $this->assertSame('MyType_Class', EasyRdf_TypeMapper::get('rdf:mytype'));
     EasyRdf_TypeMapper::delete('rdf:mytype');
     $this->assertSame(NULL, EasyRdf_TypeMapper::get('rdf:mytype'));
 }
示例#4
0
文件: Graph.php 项目: aWEBoLabs/taxi
 /** Work out the class to instantiate a resource as
  *  @ignore
  */
 protected function classForResource($uri)
 {
     $rdfType = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
     if (isset($this->index[$uri][$rdfType])) {
         foreach ($this->index[$uri][$rdfType] as $type) {
             if ($type['type'] == 'uri' or $type['type'] == 'bnode') {
                 $class = EasyRdf_TypeMapper::get($type['value']);
                 if ($class != null) {
                     return $class;
                 }
             }
         }
     }
     // Parsers don't typically add a rdf:type to rdf:List, so we have to
     // do a bit of 'inference' here using properties.
     if ($uri == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil' or isset($this->index[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#first']) or isset($this->index[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'])) {
         return 'EasyRdf_Collection';
     }
     return 'EasyRdf_Resource';
 }
 public function testDelete()
 {
     $this->assertEquals('MyType_Class', EasyRdf_TypeMapper::get('rdf:mytype'));
     EasyRdf_TypeMapper::delete('rdf:mytype');
     $this->assertEquals(null, EasyRdf_TypeMapper::get('rdf:mytype'));
 }