/** * 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; }
public function testLocalnameWithNoPath() { $res = new EasyRdf_Resource('http://example.com/'); $this->assertSame(null, $res->localName()); }
/** * 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; }
/** * Guess which vocabulary a URI originates from, based on the declared * vocabulary URI spaces. * * @param $uri string URI to search * @return Vocabulary vocabulary of this URI, or null if not found */ public function guessVocabularyFromURI($uri) { if ($this->vocabs_by_urispace === null) { // initialize cache $this->vocabs_by_urispace = array(); foreach ($this->getVocabularies() as $voc) { $this->vocabs_by_urispace[$voc->getUriSpace()] = $voc; } } // try to guess the URI space and look it up in the cache $res = new EasyRdf_Resource($uri); $namespace = substr($uri, 0, -strlen($res->localName())); if (array_key_exists($namespace, $this->vocabs_by_urispace)) { return $this->vocabs_by_urispace[$namespace]; } // didn't work, try to match with each URI space separately foreach ($this->vocabs_by_urispace as $urispace => $voc) { if (strpos($uri, $urispace) === 0) { return $voc; } } // not found return null; }