public function setUp()
 {
     $graph = new \EasyRdf_Graph();
     $myEnum = new \EasyRdf_Resource('http://schema.org/MyEnum', $graph);
     $myEnum->add('rdfs:subClassOf', ['type' => 'uri', 'value' => TypesGenerator::SCHEMA_ORG_ENUMERATION]);
     $this->generator = new ApiPlatformCoreAnnotationGenerator(new NullLogger(), [], [], [], ['Res' => ['resource' => new \EasyRdf_Resource('http://schema.org/Res', $graph), 'fields' => ['prop' => ['isCustom' => false], 'customProp' => ['isCustom' => true]]], 'MyEnum' => ['resource' => $myEnum]]);
 }
 /**
  * Extracts the cardinality of a property.
  *
  * Based on [Geraint Luff work](https://github.com/geraintluff/schema-org-gen).
  *
  * @param \EasyRdf_Resource $property
  *
  * @return string The cardinality
  */
 private function extractForProperty(\EasyRdf_Resource $property)
 {
     $localName = $property->localName();
     $fromGoodRelations = $this->goodRelationsBridge->extractCardinality($localName);
     if ($fromGoodRelations) {
         return $fromGoodRelations;
     }
     $comment = $property->get('rdfs:comment')->getValue();
     if (preg_match('/\\(s\\)/', $comment) || preg_match('/^The most generic uni-directional social relation./', $comment) || preg_match('/one or more/i', $comment)) {
         return self::CARDINALITY_0_N;
     }
     if (preg_match('/^is/', $localName) || preg_match('/^The /', $comment)) {
         return self::CARDINALITY_0_1;
     }
     return self::CARDINALITY_UNKNOWN;
 }
Пример #3
0
 public function testSetInverse()
 {
     $this->setupTestGraph();
     $homepage1 = new EasyRdf_Resource('http://example.com/1', $this->graph);
     $homepage2 = new EasyRdf_Resource('http://example.com/2', $this->graph);
     $count = $this->resource->set('foaf:homepage', $homepage1);
     $this->assertSame(1, $count);
     $this->assertSame($this->resource, $homepage1->get('^foaf:homepage'));
     $this->assertSame(null, $homepage2->get('^foaf:homepage'));
     $count = $this->resource->set('foaf:homepage', $homepage2);
     $this->assertSame(1, $count);
     $this->assertSame(null, $homepage1->get('^foaf:homepage'));
     $this->assertSame($this->resource, $homepage2->get('^foaf:homepage'));
 }
Пример #4
0
 public function testDumpWithNoProperties()
 {
     $resource = new EasyRdf_Resource("http://example.com/empty");
     $this->assertEquals('', $resource->dump(false));
     $this->assertEquals('', $resource->dump(true));
 }
Пример #5
0
 /**
  * Updates generated $class with given field config.
  *
  * @param array                  $config
  * @param array                  $class
  * @param \EasyRdf_Resource      $type
  * @param string                 $propertyName
  * @param \EasyRdf_Resource|null $property
  *
  * @return array $class
  */
 private function generateField(array $config, array $class, \EasyRdf_Resource $type, $propertyName, \EasyRdf_Resource $property = null)
 {
     $typeConfig = isset($config['types'][$type->localName()]) ? $config['types'][$type->localName()] : null;
     $typesDefined = !empty($config['types']);
     // Warn when property are not part of GoodRelations
     if ($config['checkIsGoodRelations']) {
         if (!$this->goodRelationsBridge->exist($propertyName)) {
             $this->logger->warning(sprintf('The property "%s" (type "%s") is not part of GoodRelations.', $propertyName, $type->localName()));
         }
     }
     // Ignore or warn when properties are legacy
     if (!empty($property) && preg_match('/legacy spelling/', $property->get('rdfs:comment'))) {
         if (isset($typeConfig['properties'])) {
             $this->logger->warning(sprintf('The property "%s" (type "%s") is legacy.', $propertyName, $type->localName()));
         } else {
             $this->logger->info(sprintf('The property "%s" (type "%s") is legacy. Ignoring.', $propertyName, $type->localName()));
             return $class;
         }
     }
     $ranges = [];
     if (isset($typeConfig['properties'][$propertyName]['range']) && $typeConfig['properties'][$propertyName]['range']) {
         $ranges[] = $typeConfig['properties'][$propertyName]['range'];
     } elseif (!empty($property)) {
         foreach ($property->all(self::SCHEMA_ORG_RANGE) as $range) {
             if (!$typesDefined || $this->isDatatype($range->localName()) || isset($config['types'][$range->localName()])) {
                 $ranges[] = $range->localName();
             }
         }
     }
     $numberOfRanges = count($ranges);
     if (0 === $numberOfRanges) {
         $this->logger->error(sprintf('The property "%s" (type "%s") has an unknown type. Add its type to the config file.', $propertyName, $type->localName()));
     } else {
         if ($numberOfRanges > 1) {
             $this->logger->error(sprintf('The property "%s" (type "%s") has several types. Using the first one.', $propertyName, $type->localName()));
         }
         $cardinality = isset($typeConfig['properties'][$propertyName]['cardinality']) ? $typeConfig['properties'][$propertyName]['cardinality'] : false;
         if (!$cardinality || $cardinality === CardinalitiesExtractor::CARDINALITY_UNKNOWN) {
             $cardinality = $property ? $this->cardinalities[$propertyName] : CardinalitiesExtractor::CARDINALITY_1_1;
         }
         $isArray = in_array($cardinality, [CardinalitiesExtractor::CARDINALITY_1_N, CardinalitiesExtractor::CARDINALITY_N_N]);
         if (false === $typeConfig['properties'][$propertyName]['nullable']) {
             $isNullable = false;
         } else {
             $isNullable = !in_array($cardinality, [CardinalitiesExtractor::CARDINALITY_1_1, CardinalitiesExtractor::CARDINALITY_1_N]);
         }
         $class['fields'][$propertyName] = ['name' => $propertyName, 'resource' => $property, 'range' => $ranges[0], 'cardinality' => $cardinality, 'isArray' => $isArray, 'isNullable' => $isNullable, 'isUnique' => $typeConfig['properties'][$propertyName]['unique'], 'isCustom' => empty($property), 'isId' => false];
         if ($isArray) {
             $class['hasConstructor'] = true;
             if ($config['doctrine']['useCollection'] && !in_array(self::DOCTRINE_COLLECTION_USE, $class['uses'])) {
                 $class['uses'][] = self::DOCTRINE_COLLECTION_USE;
             }
         }
     }
     return $class;
 }
Пример #6
0
 /**
  * Get the label for a resource, preferring 1. the given language 2. configured languages 3. any language.
  * @param EasyRdf_Resource $res resource whose label to return
  * @param string $lang preferred language
  * @return EasyRdf_Literal label as an EasyRdf_Literal object, or null if not found
  */
 public function getResourceLabel($res, $lang)
 {
     $langs = array_merge(array($lang), array_keys($this->config->getLanguages()));
     foreach ($langs as $l) {
         $label = $res->label($l);
         if ($label !== null) {
             return $label;
         }
     }
     return $res->label();
     // desperate check for label in any language; will return null if even this fails
 }
Пример #7
0
 /**
  * Adds Type label to list.
  *
  * @param \EasyRdf_Resource $type
  *   An EasyRdf_Resource which is a type.
  */
 private function addType(\EasyRdf_Resource $type) {
   if ($type != NULL) {
     // Omit deprecated types.
     if ($type->get("schema:supersededBy")) {
       return;
     }
     $this->listTypes[$type->shorten()] = $type->label();
   }
 }
Пример #8
0
 /** Create a new container - do not use this directly
  *
  * @ignore
  */
 public function __construct($uri, $graph)
 {
     $this->position = 1;
     parent::__construct($uri, $graph);
 }
Пример #9
0
 /**
  * Tests if a type is an enum.
  *
  * @param \EasyRdf_Resource $type
  *
  * @return bool
  */
 private function isEnum(\EasyRdf_Resource $type)
 {
     $subClassOf = $type->get('rdfs:subClassOf');
     return $subClassOf && $subClassOf->getUri() === self::SCHEMA_ORG_ENUMERATION;
 }
Пример #10
0
 /**
  * Transform the concept search query results into the skosmos desired return format.
  * @param EasyRdf_Sparql_Result $results
  * @param array $vocabs array of Vocabulary objects to search; empty for global search
  * @return array query result object
  */
 private function transformConceptSearchResults($results, $vocabs)
 {
     $ret = array();
     $qnamecache = array();
     // optimization to avoid expensive shorten() calls
     foreach ($results as $row) {
         if (!isset($row->s)) {
             continue;
         }
         // don't break if query returns a single dummy result
         $hit = array();
         $hit['uri'] = $row->s->getUri();
         if (isset($row->graph)) {
             $hit['graph'] = $row->graph->getUri();
         }
         foreach (explode(" ", $row->types->getValue()) as $typeuri) {
             if (!array_key_exists($typeuri, $qnamecache)) {
                 $res = new EasyRdf_Resource($typeuri);
                 $qname = $res->shorten();
                 // returns null on failure
                 $qnamecache[$typeuri] = $qname !== null ? $qname : $typeuri;
             }
             $hit['type'][] = $qnamecache[$typeuri];
         }
         if (isset($row->broaders)) {
             foreach (explode("\n", $row->broaders->getValue()) as $line) {
                 $brdata = str_getcsv($line, ',', '"', '"');
                 $broader = array('uri' => $brdata[0]);
                 if ($brdata[1] != '') {
                     $broader['prefLabel'] = $brdata[1];
                 }
                 $hit['broader'][] = $broader;
             }
         }
         foreach ($vocabs as $vocab) {
             // looping the vocabulary objects and asking these for a localname for the concept.
             $localname = $vocab->getLocalName($hit['uri']);
             if ($localname !== $hit['uri']) {
                 // only passing the result forward if the uri didn't boomerang right back.
                 $hit['localname'] = $localname;
                 break;
                 // stopping the search when we find one that returns something valid.
             }
         }
         if (isset($row->label)) {
             $hit['prefLabel'] = $row->label->getValue();
         }
         if (isset($row->label)) {
             $hit['lang'] = $row->label->getLang();
         }
         if (isset($row->plabel)) {
             $hit['matchedPrefLabel'] = $row->plabel->getValue();
             $hit['lang'] = $row->plabel->getLang();
         } elseif (isset($row->alabel)) {
             $hit['altLabel'] = $row->alabel->getValue();
             $hit['lang'] = $row->alabel->getLang();
         } elseif (isset($row->hlabel)) {
             $hit['hiddenLabel'] = $row->hlabel->getValue();
             $hit['lang'] = $row->hlabel->getLang();
         }
         $ret[] = $hit;
     }
     return $ret;
 }
Пример #11
0
 /**
  * Iterates over all the properties of the concept and returns those in an array.
  * @return array
  */
 public function getProperties()
 {
     $properties = array();
     $narrowers_by_uri = array();
     $in_a_collection = array();
     $members_array = array();
     $long_uris = $this->resource->propertyUris();
     $duplicates = array();
     $ret = array();
     // looking for collections and linking those with their narrower concepts
     if ($this->vocab->getConfig()->getArrayClassURI() !== null) {
         $collections = $this->graph->allOfType($this->vocab->getConfig()->getArrayClassURI());
         if (sizeof($collections) > 0) {
             // indexing the narrowers once to avoid iterating all of them with every collection
             foreach ($this->resource->allResources('skos:narrower') as $narrower) {
                 $narrowers_by_uri[$narrower->getUri()] = $narrower;
             }
             foreach ($collections as $coll) {
                 $curr_coll_members = $this->getCollectionMembers($coll, $narrowers_by_uri);
                 foreach ($curr_coll_members as $collection) {
                     if ($collection->getSubMembers()) {
                         $submembers = $collection->getSubMembers();
                         foreach ($submembers as $member) {
                             $in_a_collection[$member->getUri()] = true;
                         }
                     }
                 }
                 if (isset($collection) && $collection->getSubMembers()) {
                     $members_array = array_merge($curr_coll_members, $members_array);
                 }
             }
             $properties['skos:narrower'] = $members_array;
         }
     }
     foreach ($long_uris as &$prop) {
         if (EasyRdf_Namespace::shorten($prop) !== null) {
             // shortening property labels if possible
             $prop = $sprop = EasyRdf_Namespace::shorten($prop);
         } else {
             $sprop = "<{$prop}>";
         }
         // EasyRdf requires full URIs to be in angle brackets
         if (!in_array($prop, $this->DELETED_PROPERTIES)) {
             $propres = new EasyRdf_Resource($prop, $this->graph);
             $proplabel = $propres->label($this->getEnvLang()) ? $propres->label($this->getEnvLang()) : $propres->label();
             $propobj = new ConceptProperty($prop, $proplabel);
             if ($propobj->getLabel() !== null) {
                 // only display properties for which we have a label
                 $ret[$prop] = $propobj;
             }
             // searching for subproperties of literals too
             foreach ($this->graph->allResources($prop, 'rdfs:subPropertyOf') as $subi) {
                 $suburi = EasyRdf_Namespace::shorten($subi->getUri()) ? EasyRdf_Namespace::shorten($subi->getUri()) : $subi->getUri();
                 $duplicates[$suburi] = $prop;
             }
             // Iterating through every literal and adding these to the data object.
             foreach ($this->resource->allLiterals($sprop) as $val) {
                 $literal = new ConceptPropertyValueLiteral($val, $prop);
                 // only add literals when they match the content/hit language or have no language defined
                 if (isset($ret[$prop]) && ($literal->getLang() === $this->clang || $literal->getLang() === null)) {
                     $ret[$prop]->addValue($literal);
                 }
             }
             // Iterating through every resource and adding these to the data object.
             foreach ($this->resource->allResources($sprop) as $val) {
                 // skipping narrower concepts which are already shown in a collection
                 if ($sprop === 'skos:narrower' && array_key_exists($val->getUri(), $in_a_collection)) {
                     continue;
                 }
                 // hiding rdf:type property if it's just skos:Concept
                 if ($sprop === 'rdf:type' && $val->shorten() === 'skos:Concept') {
                     continue;
                 }
                 // handled by getMappingProperties()
                 if (in_array($sprop, $this->MAPPING_PROPERTIES)) {
                     continue;
                 }
                 if (isset($ret[$prop])) {
                     $ret[$prop]->addValue(new ConceptPropertyValue($this->model, $this->vocab, $val, $prop, $this->clang), $this->clang);
                 }
             }
         }
     }
     // adding narrowers part of a collection
     foreach ($properties as $prop => $values) {
         foreach ($values as $value) {
             $ret[$prop]->addValue($value, $this->clang);
         }
     }
     foreach ($ret as $key => $prop) {
         if (sizeof($prop->getValues()) === 0) {
             unset($ret[$key]);
         }
     }
     $ret = $this->removeDuplicatePropertyValues($ret, $duplicates);
     // sorting the properties to the order preferred in the Skosmos concept page.
     $ret = $this->arbitrarySort($ret);
     return $ret;
 }
Пример #12
0
 public static function createFromRDF(EasyRdf_Resource $annoResource)
 {
     $textAnno = new TextAnnotation($annoResource->getUri(), $annoResource->getLiteral("stanbol:start")->getValue(), $annoResource->getLiteral("stanbol:end")->getValue(), $annoResource->getLiteral("stanbol:confidence")->getValue());
     $textAnno->setSelectedText($annoResource->getLiteral("stanbol:selected-text")->getValue());
     return $textAnno;
 }